1

Hi I'm a newbie to PHP and please help me understand where I've gone wrong. Below is an extraction of the lengthy code I have where I'm trying to get SQL database values to a table with checkboxes adding in-front, allowing users to pick and delete the record they want. My issue is once a record is picked through the checkbox and hit delete, the record isn't getting deleted.

The Validation bit is what I have in the code to delete the checkbox picked values. Populating checkboxes for each data row extracted from the database: will tell you how I'm getting the checkboxes via the loop.

If the information provided isn't sufficient please highlight. Thank you. Validation bit :

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'");
    } 
  }

Populating checkboxes for each data row extracted from the database:

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>";
}
4

1 回答 1

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'");
} 
于 2013-01-26T15:34:45.300 回答