问题从显示的表中删除行 [英] Issue deleting rows from displayed table

查看:67
本文介绍了问题从显示的表中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我遇到了使用以下代码删除行的问题。该表正确显示了信息,并在url中将正确的ID发送到delete.php页面,但是我无法获取它来完成命令。在 delete.php 上略微更改代码,我可以显示以下任何一个:

So I have an issue getting rows to be deleted using the following code. The table displays the information correctly, plus sends the correct id in the url to the delete.php page, but I cannot get it to complete the command. Changing the code slightly on the delete.php I can get it to show either:


无法删除索引。

Couldn't delete the index.

或:


绑定参数失败:0

Binding parameters failed: 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();

它必须很简单,但我无法弄清楚。

It has to be something simple but I cannot figure it out.

推荐答案

您在SQL中使用了错误的引号类型。要转义包含保留字的表或列名,请使用反引号。如果使用的是 bind_param ,则必须在要替换参数的查询中放入。 / p>

You're using the wrong type of quotes in your SQL. To escape a table or column name that contains a reserved word, you use backticks. And if you're using bind_param, you have to put ? in the query where the parameter will be substituted.

$sql = "DELETE FROM mytable WHERE `index` = ?";

这篇关于问题从显示的表中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆