所以我在使用以下代码删除行时遇到问题。该表正确显示信息,并在url中将正确的id发送到delete.php页面,但我无法完成命令。在delete.php
稍微更改代码我可以让它显示:
无法删除索引。
或:
绑定参数失败:0
<?php
$con = mysqli_connect("localhost","user","pass","database");
// Check connection
if (mysqli_connect_errno())
{
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
if (!$result = mysqli_query($con,"SELECT * FROM mytable ORDER BY `server_name`;"))
{
die("Error: " . mysqli_error($con));
}
?>
<table border='1'>
<tr>
<th><b>Server Name</b></th>
<th><center><b>Port</b></center></th>
<th><center><b>Mod</b></center></th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['server_name']; ?></td>
<td><center><?php echo $row['server_port']; ?></center></td>
<td><center><?php echo $row['mod']; ?></center></td>
<td><center><a href="delete.php?id=<?php echo $row['index']; ?>"><img src="images/remove.png" width="16" height="16"></center></img></a></td>
</tr>
<?php
}
mysqli_close($con);
?>
</table>
我的删除文件是
<?php
// Your database info
$db_host = 'localhost';
$db_user = 'user';
$db_pass = 'pass';
$db_name = 'database';
if (!isset($_GET['id']))
{
echo 'No ID was given...';
exit;
}
$con = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($con->connect_error)
{
die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
}
$sql = "DELETE FROM mytable WHERE 'index' = " . $_GET['id'];
if (!$result = $con->prepare($sql))
{
die('Query failed: (' . $con->errno . ') ' . $con->error);
}
if (!$result->bind_param('i', $_GET['ID']))
{
die('Binding parameters failed: (' . $result->errno . ') ' . $result->error);
}
if (!$result->execute())
{
die('Execute failed: (' . $result->errno . ') ' . $result->error);
}
if ($result->affected_rows > 0)
{
echo "The ID was deleted with success.";
}
else
{
echo "Couldn't delete the index.";
}
$result->close();
$con->close();
它必须是简单的东西,但我无法弄明白。
答案 0 :(得分:1)
您在SQL中使用了错误的引号类型。要转义包含保留字的表或列名称,请使用反引号。如果您使用的是bind_param
,则必须将?
放入查询中,以替换参数。
$sql = "DELETE FROM mytable WHERE `index` = ?";
答案 1 :(得分:0)
替换delete.php
文件中的以下行:
$sql = "DELETE FROM mytable WHERE 'index' = " . $_GET['id'];
使用:
$sql = "DELETE FROM mytable WHERE `index` = ?";
建议的替换声明中的更改包括:
index
周围的单引号。请注意,MySQL转义字符是一个反引号而不是单引号。 delete
查询中没有包含任何参数占位符,但是您正在尝试绑定整数参数。