I'm a beginner in php and mysqli. I would like to write a program that shows a list of names in the database table. Users are allowed to click on the names and clicking on the name should display a picture of that person.
My code is below. I can add a name and person to the db and retrieve all the names in the db, but I don't know how I can make the names "clickable" so users can click on them and display a picture. Can someone take a look and give me some hints? Thanks for your help!
Please see below for part of my code -
Here is my main php file:
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysqli("blah blah", "username","pw", "blah");
if($mysqli->connect_errno){
echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script>$(document).ready(function() {$("form").validate();});</script>
<title>CS302</title>
<meta charset="UTF-8">
<p></p>
</head>
<body>
<div>This is a homework</div>
<p></p>
<div>
<form method="post" action="index.php" enctype="multipart/form-data">
Celebrity Name: <input type="text" name="c_name">
Celebrity Photo: <input type="file" name="c_picture">
<input type="submit" name="add" value="Upload">
</form>
</div>
<br>
<div>
<table>
<tr>
<td>See below for a list of celebrities in our database</td>
</tr>
<?php
//Display names in the celebrity database
if(!($stmt = $mysqli->prepare("SELECT c_name FROM celebrity"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($c_name)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo "<tr>\n<td>\n" . $c_name . "\n</td>\n</tr>";
}
$stmt->close();
?>
</table>
</div>
</body>
</html>
Here is the php file for retrieve the image:
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysqli("blah", "blah","blah", "blah");
if($mysqli->connect_errno){
echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//retrieve the blob
if(!($stmt = $mysqli->prepare("SELECT c_picture FROM celebrity where c_name = ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("i",$_POST['c_id']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($c_picture)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
$stmt->store_result();
while($stmt->fetch()){
echo "<tr>\n<td>\n" . $c_picture . "\n</td>\n</tr>";
}
$stmt->close();
?>
Try this one
$mysqli->prepare("SELECT c_id,c_name FROM celebrity")
// make sure your $c_id contains the id of that name record
while($stmt->fetch()){
echo "<tr><td><a href='your_picture.php?c_id=".$c_id."'>" . $c_name . "</a></td></tr>";
}
And in your_picture.php use $_GET['c_id']
$stmt->bind_param("i",$_GET['c_id'])
It kind of depends on what you're storing in c_picture. If you're storing a string of the image's web location, you can simply change your query to return c_name and c_picture.
then your php could use this:
echo "<tr>\n<td>\n".$c_name."\n</td>\n</tr>";
Related
//Connects to the database
$mysqli = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_DB);
if($mysqli->connect_errno){
echo "Connection error " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if (isset($_POST['points']) & isset($_POST['user'])) {
if(!($stmt = $mysqli->prepare("INSERT INTO penalty (user_id, first_name, last_name, name, value) VALUES (?, ?, ?, ?, ?);"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("sssss",$_POST['points'],$_POST['user']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
$stmt->close();
header('Location: GivePoints1.php');
echo "Added successfully";
exit();
}
$stmt->close();
}
<form class="form-horizontal" action="GivePoints1.php" method="post">
<div class="form-group">
<label class="control-label col-sm-2">Penalty:</label>
<select name="points[]" id="points">
<?php
//Prepare SELECT statement for user's name
if(!($stmt = $mysqli->prepare("SELECT name, value FROM points"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
//Execute the SELECT statement
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
//Bind values to variables
if(!$stmt->bind_result($name, $value)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
//Output name
while($stmt->fetch()){
echo'<option value=" '.$name.' "> '.$name.' - ' . $value .' Pts </option>';
}
$stmt->close();
?>
</select>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Give To:</label>
<select name="user[]" id="user">
<?php
//Prepare SELECT statement for user's name
if(!($stmt = $mysqli->prepare("SELECT id, first_name, last_name FROM award_user"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
//Execute the SELECT statement
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
//Bind values to variables
if(!$stmt->bind_result($id, $first_name, $last_name)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
//Output name
while($stmt->fetch()){
echo'<option value=" '.$id.' "> '.$first_name.' ' . $last_name .' </option>';
}
$stmt->close();
?>
</select>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-lg btn-primary ">Submit</button>
</div>
</div>
</form>
Can someone guide me to the right road?
I need to add those values into new table where id would be user_id (id extracted from award_user table change into user_id in penalty table)
I need to inser id - as user_id, first_name, last_name, name and value into table penalty. it prints select no problem but wouldn't add anything to new table
im trying to do it for a good while now and no luck. i will appriciate some explanation
I am having an issue deleting the row from the table. When I click on the 'delete' button it does take me to the next page and it says 'Removed 0 rows from player'. Basically, it is executing correctly, but I am unable to delete the selected row. I have been able to display and add to the table.
Player.php
<table id="table table-bordered">
<tr>
<th>Id#</th>
<th>Player(s)</th>
<th>Position</th>
</tr>
if(!($stmt = $mysqli->prepare("SELECT id_Player, name_Player, position_Player FROM player s ORDER BY position_Player ASC"))){
echo "Prepare failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->execute())
{
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($id_Player, $name_Player, $position_Player))
{
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo "<tr><td> $id_Player </td> <td> $name_Player </td><td> $position_Player </td>";
?>
<td>
<form id="delete" method="post" action="deletePlayers.php">
<input type="submit" name="id_Player" value="Delete!"/>
</form>
</td>
</tr>
deletePlayers.php
if(!($stmt = $mysqli->prepare("DELETE FROM player WHERE id_Player = ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;}
if(!($stmt->bind_param("s",$_POST['id_Player']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;}
else {
echo "Removed " . $stmt->affected_rows . " row from player. <br/><br/><strong> Returning to 'Add Players'</strong>";}
The problem lies in this line of code,
if(!($stmt->bind_param("s",$_POST['id_Player']))){}
Here the value of $POST['id_player'] is Delete! because you are passing the name of the input type submit in your HTML code and I think that you don't have any id which equals to Delete! in your database.
The Solution
What you need to do is that you need to use a hidden input which will hold the id_Player value like this,
if(!($stmt = $mysqli->prepare("SELECT id_Player, name_Player, position_Player FROM player s ORDER BY position_Player ASC"))){
echo "Prepare failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->execute())
{
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($id_Player, $name_Player, $position_Player))
{
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo "<tr><td> $id_Player </td> <td> $name_Player </td><td> $position_Player </td>";
?>
<td>
<form id="delete" method="post" action="deletePlayers.php">
<input type="hidden" name="id_Player" value="<?= $id_Player ?>"/>
<input type="submit" value="Delete!"/>
</form>
</td>
</tr>
I am having an issue with my php/mysql UPDATE query not reaching my database. I know this probably has a simple fix I just cannot seem to find where my mistake is, here is my code:
This is the form I am using to send the data:
<?php
if(!($stmt = $mysqli->prepare("SELECT bowl_games.id, bowl_games.name, stadiums.name, bowl_games.inaugural_year FROM bowl_games
INNER JOIN stadiums ON stadiums.id = bowl_games.stadium_id
WHERE bowl_games.id = ? "))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("i", $_POST['bowl_game']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($id, $name, $stadium, $inauguralyear)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
}
?>
<div class="container">
<form method="post" action="update_bowl_game_2.php">
<fieldset> <legend>Update Bowl Game</legend>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text", class="form-control", name="Name", value="<?php echo $name?>"/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Stadium</label>
<div class="col-sm-10">
<select name="Stadium">
<?php
if(!($stmt = $mysqli->prepare("SELECT id, name FROM stadiums ORDER BY name"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($id, $sname)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
if($sname === $stadium){
echo "<option value=\"" . $id . "\" selected>" . $sname . "</option>";
} else {
echo "<option value=\"" . $id . "\">" . $sname . "</option>";
}
}
$stmt->close();
?>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Inaugural Year</label>
<div class="col-sm-10">
<input type="number", class="form-control", name="InauguralYear", value="<?php echo $inauguralyear?>"/>
</div>
</div>
<input type="hidden" name="id" value="<?php echo $id?>"/>
<div class="form-group row">
<div class="offset-sm-2 col-sm-10">
<button type="submit" class="btn btn-primary">Update Bowl Game</button>
</div>
</div>
</fieldset>
</form>
</div>
<?php
$mysqli = "SELECT bowl_games.id, bowl_games.name, stadiums.name, bowl_games.inaugural_year FROM bowl_games
INNER JOIN stadiums ON stadiums.id = bowl_games.stadium_id"
?>
And here is the php code that should update the entry in the database:
<?php
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysqli("oniddb.cws.oregonstate.edu","dejarnen-db","*hidden*","dejarnen-db");
if(!$mysqli || $mysqli->connect_errno){
echo "Connection error " . $mysqli->connect_errno . " " . $mysqli- >connect_error;
}
if(!($stmt = $mysqli->prepare("UPDATE bowl_games SET name=?, stadium_id=?, inaugural_year=? WHERE id= ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt- >bind_param("siii",$_POST['Name'],$_POST['Stadium'],$_POST['InauguralYear'],$_POST['id']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "Updated " . $stmt->affected_rows . " rows in bowl games.";
}
?>
When I submit the form, if the selected entry has been successfully updated, I should see the message "Updated 1 rows in bowl games." Instead, I get the message "Updated 0 rows in bowl games."
Can anyone point me in the right direction with this issue that I am having? Thanks
In the form, you use a variable named $id for two different purposes:
For the id of the game
For the id of the stadium
First you retrieve the id for the game into the variable, then you retrieve the ids of the stadiums into the variable, then you use the variable to create the hidden input for the game id.
By the time you write the hidden input, the $id variable contains an id of a stadium.
One possible solution: When listing the stadiums, use a separate variable name:
<select name="Stadium">
<?php
if(!($stmt = $mysqli->prepare("SELECT id, name FROM stadiums ORDER BY name"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($stadium_id, $sname)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
if($sname === $stadium){
echo "<option value=\"" . $stadium_id . "\" selected>" . $sname . "</option>";
} else {
echo "<option value=\"" . $stadium_id . "\">" . $sname . "</option>";
}
}
$stmt->close();
?>
</select>
Generally, it is a good idea to use variable names that are specific. So, instead of "$name", use "$stadium_name" and so on. The only exception to that rule is when you have local variables in a function that is very short.
Another possible solution would be to write the hidden input earlier, before filling the select with stadiums.
Firstly check $_POST data not null then use bind param like sssi.
//example
if(!($stmt- >bind_param("sssi", $_POST['Name'], $_POST['Stadium'], $_POST['InauguralYear'], $_POST['id']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
I have the following code which should return all of the teams within a certain region. I have a database of football teams that contains tables for Teams and States. The teams table has a foreign key reference to a states table, and the states table has an attribute for regions (north, south, east, west).
I have the following html/php code on my main page:
<div>
<form method="post" action="regions_filter.php">
<fieldset>
<legend>Filter Teams By Region</legend>
<select name="Region">
<?php
if(!($stmt = $mysqli->prepare("SELECT DISTINCT region FROM states"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($region)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo '<option value=" ' . $region . ' "> ' . $region . '</option>\n';
}
$stmt->close();
?>
</select>
<input type="submit" value="Run Filter"/>
</fieldset>
</form>
</div>
Below is the regions_filter.php file code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<body>
<div>
<table>
<tr>
<td>Teams By Region</td>
</tr>
<tr>
<td>School Name</td>
<td>State Name</td>
<td>State Capital</td>
<td>State Population</td>
<td>Region</td>
</tr>
<?php
if(!($stmt = $mysqli->prepare("SELECT teams.school_name, states.name, states.capital, states.population, states.region FROM teams
INNER JOIN states ON states.id = teams.state_id
WHERE states.region = ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("s",$_POST['Region']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli- >connect_error;
}
if(!$stmt->bind_result($school, $state, $capital, $population, $region)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli- >connect_error;
}
while($stmt->fetch()){
echo "<tr>\n<td>" . $school . "\n</td>\n<td>" . $state . "\n</td>\n<td>" . $capital . "\n</td>\n</td>"
. $population . "\n</td>\n<td>" . $region . "\n</td>\n</tr>";
}
$stmt->close();
?>
</table>
</div>
</body>
</html>
When I go to run the filter on my main page, I am taken to the regions_filter.php page with no results. The only thing that shows up is the pre-coded html table at the top of the regions_filter.php page.
I believe the error is somewhere in the code snippet below. I have tried different variations with the option value but can't seem to crack it:
while($stmt->fetch()){
echo '<option value=" ' . $region . ' "> ' . $region . '</option>\n';
}
Any pointers in the right direction would be greatly appreciated.
There is an error populating the regions select there is an extra white space before and after the region name. It should be like:
while($stmt->fetch()){
echo '<option value="' . $region . '">' . $region . '</option>\n';
}
I believe that the states.id on your where statement is not region id. If you are filtering $region as a parameter from the tag to the select statement state.id = ? (state.id = $region) it will not give any results. because your are filtering via states.
Note: This is just my first understanding on the above code
I don't think you have any errors on this...it's just that you might be filtering the wrong attribute ^_^
I'm very new to PHP and SQL. For a school assignment, I need to create a form for users to update customer data. However, I notice that the update function only updates the customer with the last ID in the data set. For example, if I have 4 customers in the data set. I used a drop down box t list the customer id. When I select id, 1 ~ 3, it says that 0 row is updated. It only works when I select id, 4. So I can only update the 4th one. Can someone take a look at my code and give me some tips on what the issue is and how to fix it? Thank you!
Here is the "updatecustomer.php" code:
<?php
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysql(SERVER_NAME, USERNAME,PASSWORD, DATABASE);
if($mysqli->connect_errno){
echo "Connection error " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!($stmt = $mysqli->prepare("UPDATE customer SET fName=?, lName=?, email=?, phone_number=?, address_no=?, address_street1=?,
address_street2=?, address_city=?, address_state=?, address_zip=? WHERE customer_id = ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("isssiissssi",$_POST['customer_id'],$_POST['fName'],$_POST['lName'],$_POST['email'],$_POST['phone_number'], $_POST['address_no'],
$_POST['address_street1'],$_POST['address_street2'],$_POST['address_city'],$_POST['address_state'], $_POST['address_zip']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "Updated " . $stmt->affected_rows . " rows to customer.";
}
?>
Here is part of the code in my form:
<div>
<form method="post" action="updatecustomer.php">
<fieldset>
<legend>Update Existing Customer</legend>
<li>Customer ID:
<select name="customer_id">
<?php
if(!($stmt = $mysqli->prepare("SELECT customer_id, customer_id FROM customer"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($customer_id, $customer_id)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo '<option value=" '. $customer_id . ' "> ' . $customer_id . '</option>\n';
}
$stmt->close();
?>
</select>
</li>
<li>First Name: <input type="text" name="fName"> Last Name: <input type="text" name="lName"</li>
<li>Email Address: <input type="text" name="email"></li>
<li>Phone Number: <input type="text" name="phone_number"></li>
<li>Street Number: <input type="text" name="address_no"> Street Line 1: <input type="text" name="address_street1"></li>
<li>Street Line 2 (Apt or Unit Number): <input type="text" name="address_street2"></li>
<li>City: <input type="text" name="address_city"> State: <input type="text" name="address_state"> Zip: <input type="text" name="address_zip"> </li>
</fieldset>
<input type="submit" name="update" value="Update Customer">
<input type="submit" name="delete" value="Delete Customer">
</div>
Check the order of variables in your $stmt->bind_param line.
Try this: I have made the code neater, and easier to understand, and reordered the customer ID in the bind_param() method.
$stmt = $mysqli->prepare("
UPDATE customer
SET fName=?,
lName=?,
email=?,
phone_number=?,
address_no=?,
address_street1=?,
address_street2=?,
address_city=?,
address_state=?,
address_zip=?
WHERE customer_id = ?
");
if(!$stmt){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
$paramBinding = $stmt->bind_param("sssiissssii",
$_POST['fName'],
$_POST['lName'],
$_POST['email'],
$_POST['phone_number'],
$_POST['address_no'],
$_POST['address_street1'],
$_POST['address_street2'],
$_POST['address_city'],
$_POST['address_state'],
$_POST['address_zip'],
$_POST['customer_id']
);
if(!$paramBinding){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
EDIT
Then when looping through the options:
foreach($stmt->fetch() AS $row){
echo '<option value="'. $row['customer_id'] . '"> ' . $row['customer_id'] . '</option>\n';
}