我正在嘗試構建一個簡單的Web應用程序,學生可以在其中預定駕駛課程(適用於大學項目)我已經完成了插入操作並查看了......現在我需要添加更新並刪除...我遇到更新問題....它似乎沒有更新我想要的行...我想要更新Active列等於true/1的行。信息通過與Firstname不同的地方傳遞。 ..列表中的最後一個名字是沒有其他添加....簡單的問題,我可以想象,任何人都可以幫忙嗎?PHP MySQL(更新)
<!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哪裏發生更新
<?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
?>
附註:您的代碼是漏洞ble到SQL注入,學習如何處理它http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – bodi0
不知道你正在嘗試在這裏做,看起來像表格使用主動作爲所有不同的列上的數據庫,所以根據你選擇它將嘗試更新哪裏Active = $ row ['HouseNumber']這聽起來不正確,當然你想要更新id = something的位置 – Anigel
您不能期望基於單選按鈕更新要更新的整個行。你需要命名你的其他輸入,然後給這些輸入分配變量。 MySQL不知道你想要更新什麼,「你」需要告訴它。 –