您好我是PHP的新手,请帮助我了解我哪里出错了。下面是我所拥有的冗长代码的提取,我试图将SQL数据库值放到一个表中,并在前面添加复选框,允许用户选择和删除他们想要的记录。我的问题是,一旦通过复选框选择记录并点击删除,记录就不会被删除。
Validation bit
是我在代码中删除复选框选中值的内容。 Populating checkboxes for each data row extracted from the database:
会告诉您我是如何通过循环获取复选框的。
如果提供的信息不充分,请突出显示。谢谢。 验证位:
if(isset($_POST['deletez']))
{
foreach($_POST['deletez'] as $wec)
{
$db = mysqli_connect('localhost', 'root', '', 'regional_data');
if(mysqli_connect_errno()) {die('The connection to the database could not be established.');}
$rW = mysqli_query($db,"DELETE * FROM brands WHERE bname ='$wec'");
}
}
填充从数据库中提取的每个数据行的复选框:
while ($reK = mysqli_fetch_array($runBrands))
{
$wec = $reK['bname'];
echo "<tr class='brndTab'>";
echo "<tr>"
."<td><input type='submit' name='deleteR' id='deleteR' value='Delete.'></td>".
"</tr>";
echo "</table>";
echo "<td class='brndTab'>".$reK["bname"]."</td>";
echo "<td class='brndTab'>".$reK["bvariant"]."</td>";
echo "<td class='brndTab1'>".$reK["bsku"]."</td>";
echo "<td class='brndTa'><input type='checkbox' name='deletez[]' value='$wec'></td>";
echo "</tr>";
}
答案 0 :(得分:0)
首先你不应该连接到循环内的数据库,移动行$ db = mysqli_connect('localhost','root','','regional_data');在foreach循环之外。否则,您为每个条目打开一个新连接。接下来,你在mysql语法中有一个错误,正确的语句是DELETE FROM brands WHERE bname ='$ wec'“。因为你要删除行而不是行的内容。要把它们放在一起,修改你的代码像这样:
$db = mysqli_connect('localhost', 'root', '', 'regional_data');
if(mysqli_connect_errno()) {die('The connection to the database could not be established.');}
foreach($_POST['deletez'] as $wec) {
$rW = mysqli_query($db,"DELETE FROM brands WHERE bname ='$wec'");
}