php 如何从 SQL 表中删除一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27345614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:19:04  来源:igfitidea点击:

How to delete a row from a SQL table?

phpsqldelete-row

提问by user3706178

I have a row that needs to be deleted from a SQL table via PHP. The table has 3 columns: id, Symbol, and Shares:

我有一行需要通过 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"];
    }
}

My program enters an if statement, which I checked. The condition is fulfilled, but the DELETE is not executed. How can I fix this?

我的程序输入一个 if 语句,我检查了它。满足条件,但不执行 DELETE。我怎样才能解决这个问题?

回答by Gordon Linoff

This syntax should work in any database:

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

DELETE FROM stocks WHERE id = XXX

I don't know what ? $_SESSION[id]is supposed to be doing. Try without the ?:

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

DELETE FROM stocks WHERE id = $_SESSION[id]

That said, you should parameterize your queries rather than putting arguments into the query string.

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