为什么mymysqli删除按钮不起作用?

时间:2014-09-25 08:07:51

标签: php html mysql mysqli

我正在尝试从表中删除某个产品,但它不起作用。 脚本错了吗?我该如何纠正?

include_once('connection.php');
include_once('functions.php');      
if(isset($_POST['delete'])){
    mysqli_query( $conDB, "DELETE FROM products WHERE  name='$_POST[hidden]'");
};              
$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
if ($page <= 0) $page = 1;           
    $per_page = 10; // Set how many records do you want to display per page.             
$startpoint = ($page * $per_page) - $per_page;                  
$statement = "products ORDER BY id DESC";
$results = mysqli_query($conDB, "SELECT * FROM {$statement} LIMIT {$startpoint} , {$per_page}");    
if (mysqli_num_rows($results) != 0) {        
    while ($row = mysqli_fetch_array($results)) {
    $name = $row['name'];
?>
<tr>
    <td align="left" colspan='2' ><big style="color:red" ><b><?php echo $row['name']?></b></big><td>    
</tr>
<tr>
    <td align="left" colspan='2'><big style="color:333333"><b><?php echo $row['category']?></b> / <?php echo $row['subcategory']?></big></td>
</tr>   
<tr>    
    <td style="vertical-align:top" >
       <?php echo "<img border=3 width= 300px  height =250px src=\"products/"  . $row['picture']>?>"<br /> 
</td>
     <td  style="vertical-align:top">   
            <?php echo  nl2br($row['description'])?>
    </td>
</tr>
<tr>
    <td> <input type="hidden" name="hidden" value="$name" ><input type="submit" name="delete" onClick='return confirmDelete()' value='Delete Product'></td>         

当我删除&#34;删除产品&#34;确认将显示我是否将删除它。但确认后。它不会从表中删除选定的行。

确认后,php没有显示错误。为什么会这样? 我该如何解决?

3 个答案:

答案 0 :(得分:2)

查看隐藏值,似乎在标记中没有正确写入:

<input type="hidden" name="hidden" value="$name" >
                                    <!--  ^ literal string, not a variable value -->

应该是:

<input type="hidden" name="hidden" value="<?php echo $name; ?>" />

旁注:由于您已经在使用mysqli,为什么不使用预准备语句。

if(isset($_POST['delete'])) {
    $delete = $_POST['delete'];
    $del = $conDB->prepare('DELETE FROM products WHERE name = ?');
    $del->bind_param('s', $delete);
    $del->execute();
}

我建议这样做,删除隐藏的输入并改为使用它:

<button type="submit" name="delete" onClick='return confirmDelete()' value="<?php echo $row['id']; ?>">Delete Product</button>

答案 1 :(得分:0)

只需更正输入隐藏字段:

<input type="hidden" name="hidden" value="<?php echo $name; ?>" >

答案 2 :(得分:0)

尝试

echo "DELETE FROM products WHERE  name='$_POST[hidden]'";

然后尝试执行sql或修改

"DELETE FROM products WHERE  name='".$_POST[hidden]."'"