我正在尝试从mysql中的users表中删除记录,
代码是这样的。
if(isset($_GET['id'])) {
//create query to delete the record
$query = "DELETE FROM users WHERE id =" . int($_GET['id']) or die(mysql_error());
//execute query
if($mysqli->query($query)) {
//print number of affected rows
echo $mysqli->affected_rows. " row(s) affected";
}
else {
//print error message
echo "Error in query : $query " . $mysqli->error;
}
}
else {
echo "Could not Execute the Delete query";
}
同时我正在迭代数据库中users表中的记录,就像这样。
//query to get records
$query = "SELECT * FROM users";
//execute query
if($result = $mysqli->query($query)) {
// see if any rows were returned
if($result->num_rows > 0) {
// if yes then print one after another
echo "<table cellpadding=10 border=1>";
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>" .$row[0] . "</td>";
echo "<td>" .$row[1] . "</td>";
echo "<td>" .$row[2] . "</td>";
echo "<td><a href =" . $_SERVER['PHP_SELF']. "?id" .$row[0]. ">Delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
$result->close();
}
问题是,我能够从数据库中获取记录并在浏览器中显示它,但是当我尝试删除记录时,第一个条件没有通过,即if(isset($ _ GET ['id']) )它转到else条件并打印消息“无法执行删除查询”,我猜它无法获取$ _GET ['id']所以只有它拒绝输入if条件,
P.S:如果有人用简单的话语解释我,我会很感激,我是编程的新手,谢谢......答案 0 :(得分:6)
您错过了=
:
echo "<td><a href =" . $_SERVER['PHP_SELF']. "?id=" .$row[0]. ">Delete</a></td>";
HERE -------------------^
答案 1 :(得分:1)
"DELETE FROM users WHERE id =" . int($_GET['id']) or die(mysql_error());
不应该是intval
吗? PHP中没有函数int
。还有(不太优选)对int的强制转换,如下所示:(int) $_GET['id']
)。