我正在制作一个应用程序,其中有以下表格:
<?php
require_once 'Connect2db3.php';
?>
<form>
<fieldset>
<article class="rondehoeken">
<header>
<div class="streep1"></div>
<div class="streep2"></div>
<div class="streep3"></div>
<div class="streep4"></div>
<div class="streep5"></div>
<h1 id="artikel-titel" >Op Vooraad</h1>
</header>
<div id="artikel-container">
<table class="table 1">
<thead>
<title>Inventory Grid.html</title>
<meta charset = "UTF-8" />
<style type = "text/css">
table, td, th {
border: 1px solid black;
}
</style>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost","root","admin","inventarisdb");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM BCD");
echo "<table border='0'>
<tr>
<th>Categorie</th>
<th>SerieNummer</th>
<th>MacAdress</th>
<th>ProductCode</th>
<th>Prijs</th>
<th>RekNummer</th>
<th>PaletNummer</th>
<th>Hoeveelheid</th>
<th>Aantekeningen</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Categorie'] . "</td>";
echo "<td>" . $row['SerieNummer'] . "</td>";
echo "<td>" . $row['MacAdress'] . "</td>";
echo "<td>" . $row['ProductCode'] . "</td>";
echo "<td>" . $row['Prijs'] . "</td>";
echo "<td>" . $row['RekNummer'] . "</td>";
echo "<td>" . $row['PaletNummer'] . "</td>";
echo "<td>" . $row['Hoeveelheid'] . "</td>";
echo "<td>" . $row['Aantekeningen'] . "</td>";
echo '<td><input type="hidden" class="entryid" name="id" value='.$row['ID'].' /><a href="#" class="delete">delete</a></td>';
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</article>
</fieldset>
</form>
在标题中:
删除表单:
<?php
$db = array (
'host' => 'localhost',
'user' => 'root',
'pass' => 'admin',
'dbname' => 'inventarisdb'
);
if(!mysql_connect($db['host'], $db['user'], $db['pass']))
{
trigger_error('Fout bij verbinden: '.mysql_error());
}
elseif(!mysql_select_db($db['dbname']))
{
trigger_error('Fout bij selecteren database: '.mysql_error());
}
else
{
$sql = "SET SESSION sql_mode = 'ANSI,ONLY_FULL_GROUP_BY'";
if(!mysql_query($sql))
{
trigger_error('MySQL in ANSI niet mogelijk');
}
}
$id = $_GET['id'];
$sql="DELETE FROM BCD WHERE id='$Categorie', '$SerieNummer', '$MacAdress', '$ProductCode', '$Prijs', '$RekNummer','$PaletNummer' ,'$Hoeveelheid', '$Aantekeningen'";
?>
该表查找另一个表中的数据,还提供了从数据库中删除该表中的行的选项。
除了从数据库中实际删除的数据外,我刚才说的这个表所做的一切都适用于这个脚本。按下delete后,执行delete操作,并提示一个all,表示您将删除一行。它将它从表中删除,但当你签入数据库或简单地用表刷新页面时,该行仍然存在,尚未删除
有什么想法为什么会发生这种情况或该怎么办吗?或者也许怎样做更容易?
我认为这不适用于
$sql="DELETE FROM BCD WHERE id='$Categorie', '$SerieNummer', '$MacAdress', '$ProductCode', '$Prijs', '$RekNummer','$PaletNummer' ,'$Hoeveelheid', '$Aantekeningen'";
您应该使用AND或or来分隔参数,这取决于您的成功删除条件。不需要逗号,还会在每次时提供列名。
类似的东西
$sql="DELETE FROM BCD WHERE id='$Categorie' AND serial_number='$SerieNummer', //etc
最好根据主键而不是这样的值范围进行删除
也不要使用mysql_函数,将mysqli或PDO与参数化查询一起使用
在您的代码中很明显,您不执行查询。
您的隐藏输入名为id
,并且您正在调用$id = $_GET['id']
,查询中的所有其他变量都是未定义的,因为如果您已经定义了它们,那么您还没有显示它们的定义位置。
替换:
$id = $_GET['id'];
$sql="DELETE FROM BCD WHERE id='$Categorie', '$SerieNummer', '$MacAdress', '$ProductCode', '$Prijs', '$RekNummer','$PaletNummer' ,'$Hoeveelheid', '$Aantekeningen'";
带有:
$id = intval($_GET['id']); // assuming your id column is integer type.
$sql="DELETE FROM BCD WHERE id=$id";
还要注意,mysql_
函数已弃用。您应该使用mysqli
或pdo
。
根据您的示例代码所展示的内容,我得出了与@BabakBandpay相同的结论您从未实际运行过DELETE查询。
不管这是真是假,虽然转移到MySQLi库是一个很好的步骤,但您仍然使用非常易受SQL注入影响的代码。请尝试使用准备好的语句:http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php
下面是一个基于您的代码(以及API文档)的示例:
<?php
$mysqli = new mysqli('localhost', 'root', 'admin', 'inventarisdb');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s'n", mysqli_connect_error());
exit();
}
// ... the rest of your code
$id = $_GET['id'];
$stmt = $mysqli->prepare("DELETE FROM BCD WHERE id=?");
$stmt->bind_param("i", $id);
$success = $stmt->execute();
$mysqli->close();
?>
这段代码虽然未经测试,也不是真正的生产质量,但由于绑定语句的性质,它相当安全:
绑定变量将由服务器自动转义。
当我还在用PHP编码时,我更喜欢PDO,但使用MySQLi的OOP版本是一个不错的折衷方案,所以我强烈建议阅读文档中的示例。
EDIT:作为附录,我还建议通过过滤器模块运行您的输入变量(http://www.php.net/manual/en/book.filter.php)。假设$id
应该是一个整数,我会将赋值更新为:
$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);
这将从$_GET['id']
中删除所有不是数字或+/-号的内容。