I am trying to build a simple web application where students can book a driving lesson (for college project) I done the insert and view... now I need to add the update and delete... I am having problems with the update.... It does not seem to update the rows I want... I want the row to update where the Active column is equal to true/1 The information is passed through apart from the Firstname... The last Firstname in the list is added no other.... Simple problem I can imagine, can anyone help?
<!DOCTYPE html>
<head>
<title>Edit Students</title>
</head>
<?php
$user = 'root'; //Database username ("Root for xampp")
$pass = ''; //Database password ("empty for exampp")
$db = 'dragondrivingschooldb'; //Name of database
$con = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect"); //Create new data connection ('name of host/server', user, password, database name)
$sql = mysqli_query($con, "SELECT * FROM booking");
echo "<table border='1'> //Creating table to store data
<tr> //Table headers
<th>Active</th>
<th>Booking ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Driving Experience</th>
<th>Street</th>
<th>PostCode</th>
<th>City</th>
<th>County</th>
<th>Mobile Number</th>
<th>House Number</th>
<th>Email</th>
<th>Course</th>
<th>Package Type</th>
<th>Length Of Lesson</th>
<th>Driving Instrutor</th>
<th>Date</th>
<th>Time</th>
<th>Name On Bank Card</th>
<th>Card Holder Address</th>
<th>Card Holder Postcode</th>
<th>Card Number</th>
<th>Three Digit Card Number</th>
</tr>";
//Show Edit Form///////////////////////////////////////////////////////////////////////////////////////////////////
while($row = mysqli_fetch_array($sql)) { //Run sql code till there are no more rows to import
echo "<form action=\"UpdateStudents.php\" method=\"post\">"; //create form
echo "<tr>";
echo "<td> <input type=\"radio\" \" name=\"radActive\"> </td>"; //when this equals true, then the row will be updated
echo "<td> <input value=" . $row['BookingID'] . " name=\"txtid\"> </td>";
echo "<td> <input value=" . $row['FirstName'] . " name=\"txtfirstname\"> </td>";
echo "<td> <input value=" . $row['LastName'] . "> </td>";
echo "<td> <input value=" . $row['Age'] . "> </td>";
echo "<td> <input value=" . $row['DrivingExpereince'] . "> </td>";
echo "<td> <input value=" . $row['Street'] . "> </td>";
echo "<td> <input value=" . $row['PostCode'] . "> </td>";
echo "<td> <input value=" . $row['City'] . "> </td>";
echo "<td> <input value=" . $row['County'] . "> </td>";
echo "<td> <input value=" . $row['MobileNumber'] . "> </td>";
echo "<td> <input value=" . $row['HouseNumber'] . "> </td>";
echo "<td> <input value=" . $row['EMail'] . "> </td>";
echo "<td> <input value=" . $row['Course'] . "> </td>";
echo "<td> <input value=" . $row['PackageType'] . "> </td>";
echo "<td> <input value=" . $row['LengthOfLesson'] . "> </td>";
echo "<td> <input value=" . $row['DrivingInstrutor'] . "> </td>";
echo "<td> <input value=" . $row['Date'] . "> </td>";
echo "<td> <input value=" . $row['Time'] . "> </td>";
echo "<td> <input value=" . $row['BankName'] . "> </td>";
echo "<td> <input value=" . $row['BankAddress'] . "> </td>";
echo "<td> <input value=" . $row['BankPostCode'] . "> </td>";
echo "<td> <input value=" . $row['BankCardNo'] . "> </td>";
echo "<td> <input value=" . $row['BankSecurityNo'] . "> </td>";
echo "</tr>";
}
echo "</table>"; //close table
echo "<input name=\"btnUpdate \" type=\"submit\" value=\"Update\" />"; //Create buton to update
echo "</form>";
mysqli_close($con); //close database connection
?>
</html>
SECOND where the update takes place
<?php
$user = 'root'; //Database username ("Root for xampp")
$pass = ''; //Database password ("empty for exampp")
$db = 'dragondrivingschooldb'; //Name of database
$con = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect"); //Create new data connection ('name of host/server', user, password, database name)
$sql = mysqli_query($con, "SELECT * FROM booking"); //Select all data from booking table
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con)); //if the sql does not run, then kill it
}
//Create escape variables for security
//Details
//$id = mysqli_real_escape_string($con, $_POST['txtid']);
$Active = mysqli_real_escape_string($con, $_POST['radActive']); //bring data in from prevoius page Active
$FirstName = mysqli_real_escape_string($con, $_POST['txtfirstname']); //Bring data in from prevoius page Firstname
$sqlupdate=("UPDATE booking SET FirstName='$FirstName' Where Active = '$Active' "); //Update row where Active = "true/1"
if (!mysqli_query($con,$sqlupdate)) {
die('Error: ' . mysqli_error($con)); //if the query does not run, then kill it
}
echo " Information Updated";
echo " FirstName = $FirstName <br>"; //show the value of first name (for debugging purposes
echo "$Active"; //show the value of active for debugging purposes
mysqli_close($con); //Close Connection
?>