J'essaye de construire une application web simple où les étudiants peuvent réserver une leçon de conduite (pour le projet d'université) j'ai fait l'insertion et la vue ... maintenant je dois ajouter la mise à jour et supprimer ... J'ai des problèmes avec la mise à jour .... Il ne semble pas mettre à jour les lignes que je veux ... Je veux mettre à jour la rangée où la colonne active est égale à vrai/1 L'information est passée à travers indépendamment du prénom. .. Le dernier prénom dans la liste est ajouté aucun autre .... Simple problème que je peux imaginer, quelqu'un peut-il aider?PHP MySQL (UPDATE)
<!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 où la mise à jour a lieu
<?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
?>
A noter: votre code est vulnérable ble à l'injection SQL, apprenez comment y faire face http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – bodi0
Je ne sais pas ce que vous essayez de fais ici, il me semble que la forme utilise active comme toutes les différentes colonnes sur la db donc en fonction de ce que vous choisissez, il va essayer de mettre à jour où Active = $ row ['HouseNumber'] qui ne sonne pas bien, sûrement vous voulez mettre à jour où un ID = quelque chose – Anigel
Vous ne pouvez pas espérer mettre à jour les lignes entières à mettre à jour en fonction uniquement du bouton radio. Vous devez nommer vos autres entrées, puis affecter des variables à celles-ci également. MySQL ne sait pas ce que vous voulez mettre à jour, "vous" devez le dire. –