duanhao4156 2018-05-12 20:05
浏览 83
已采纳

通过检索id PHP foreach来删除mysql行

I've created a foreach statement that echos out some stuff about all users registered, how do I make a button that deletes that user from the row? I've tried this:

$query = "
SELECT id
     , name
     , bname
     , email
     , address
     , agent
     , status
     , notes 
  FROM prospects
";

        try {
            $stmt = $db->prepare($query);
            $stmt ->execute();
        }
        catch(PDOException $ex) {
            die("Failed to run query. Tell the website owner!");
        }
        $rows = $stmt->fetchAll();
        foreach($rows as $row) {
            echo "<tbody>
            <th>".$row['name']."</th>";
            echo "BLAH BLAH BLAH!";
            echo "<form method='post'><th><button type='submit' name='delete' class='btn btn-white btn-round btn-just-icon'>
                <i class='material-icons'>remove_circle_outline</i>
                <div class='ripple-container'></div>
            </button></th></tbody></form>";
        } $id = $db->prepare("SELECT id FROM example");
        $id->execute();

        $result = $id->fetch(PDO::FETCH_ASSOC);

        if(isset($_POST['delete'])) {
                try {
                    $sql = "DELETE FROM example WHERE id='".$result."'";
                    $db->exec($sql);
                }
                catch(PDOException $e) {
                    echo $sql . "<br />". $e->getMessage();
                }
            }

It returns this error:

Notice: Array to string conversion....

I know this is because I've grabbed the id and there is an array of ids that I've grabbed from the database? So how do I do delete a specific row when i click on the button?

  • 写回答

1条回答 默认 最新

  • douyan1970 2018-05-13 00:09
    关注

    You have to specify which element in array holds the id value.

    Try:

    $sql = "DELETE FROM example WHERE id='".$result['id']."'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办