我只是無法理解爲什麼行不會被刪除! 請注意,我正在獲取PHP頁面中更正複選框的登錄值。 從我的角度來看,最有可能的錯誤應該是在我正在使用 'DELETE FROM'查詢的php頁面中。無法通過php刪除行,HTML
<?php
session_start();
?>
<html>
<head>
<form id="delete_customers" action="deletecustomers.php" method="post">
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("car_rental_system", $con);
$result = mysql_query("SELECT * FROM customer");
echo "<table border='1' align='center'>
<tr>
<th>first_name</th>
<th>Last_name</th>
<th>login</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['login'] . "</td>";
echo "<td>"."<input type='checkbox' name='deletingcustomers[]'
value=$row['login']}"."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<p class='submit'>
<button type='submit' name='dscustomer'>Delete selected</button>
</p>
</head>
</html>
//現在deletecustomers.php
<?php
session_start();
$_SESSION['deletingcustomers'] = $_POST['deletingcustomers'];
$N = count($_SESSION['deletingcustomers']);
$con = mysql_connect("localhost","root","");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db("car_rental_system", $con);
if(empty($_SESSION['deletingcustomers'])) {
echo("No customers selected");
} else {
for ($i=0; $i<$N; $i++) {
$sql1="delete from `customer`
where login='{$_SESSION[deletingcustomers][$i]}'";
if(mysql_query($sql1,$con))
echo 'executed';
}
}
?>
歡迎堆棧溢出!請不要使用'mysql_ *'函數來編寫新的代碼。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/q0gwD)。看到[紅色框](http://goo.gl/OWwr2)?相反,您應該瞭解[準備好的語句](http://goo.gl/orrj0)並使用[PDO](http://goo.gl/TD3xh)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定哪些,[這篇文章](http://goo.gl/YXyWL)會幫助你。如果你選擇PDO,[這裏是很好的教程](http://goo.gl/b2ATO)。另請參閱[爲什麼不應該在PHP中使用mysql函數?](http://goo.gl/J5jAo) – Daedalus
更不用說您的代碼容易受到注入攻擊。 – Daedalus
另外,PDO非常容易。我想我可能會把它扔到那裏。 – Ben