如何从SQL表中删除行?

时间:2014-12-07 17:36:50

标签: php sql delete-row

我有一行需要通过PHP从SQL表中删除。该表有3列:id,Symbol和Shares:

foreach ($rows as $row)
{
    if($row["Symbol"] === $_POST["symbol"])
    {
     // I tried these combinations, but none of them works:
     // $sql = ("DELETE FROM stocks WHERE id = ? $_SESSION[id]");        
     // $sql = ("DELETE row FROM stocks WHERE id = $_SESSION[id]");
     // $sql = ("DELETE * FROM stocks WHERE id = $_SESSION[id]");
     // $sql = ("DELETE id, Symbol, Shares FROM stocks WHERE id = $_SESSION[id]");
      // DELETE FROM stocks WHERE id = $_SESSION["id"];
    }
}

我的程序输入if语句,我检查过。条件已满足,但DELETE未执行。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:11)

此语法应适用于任何数据库:

DELETE FROM stocks WHERE id = XXX

我不知道? $_SESSION[id]应该做什么。尝试不使用?

DELETE FROM stocks WHERE id = $_SESSION[id]

也就是说,您应该参数化您的查询,而不是将参数放入查询字符串。