删除行mysql php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11212224/
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-24 23:59:14  来源:igfitidea点击:

Delete row mysql php

phpmysqlsql-delete

提问by dwstein

Using the following code:

使用以下代码:

if (isset($_POST['delete']) && isset($_POST['id']))
{
    $first = get_post('first');
    $query = "DELETE FROM user_master WHERE id='$id'";
    if(!mysql_query($query, $db_server))
        echo "DELETE failed: $query<br />" . 
        mysql_error() . "<br /><br />";
}

$query="SELECT * FROM user_master";
$result= mysql_query($query);

if(!result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);

for ($j=0 ; $j<$rows ; ++$j)
{
    $row = mysql_fetch_row($result);
    echo <<<_END
    <pre>
    ID      $row[0]
    First   $row[1]
    Last    $row[2]
    Email   $row[3]
    User    $row[4]
    </pre>
    <form action="willingLog.html" method="post">
    <input type="hidden" name="delete" value="yes" />
    <input type="hidden" name="id" value="$row[0]" />
    <input type="submit" value="DELETE RECORD" /></form>
_END;

I'm unable to delete a record form the table.

我无法从表格中删除记录。

All the records form the table are printing on the screen, including the "DELETE RECORD" button. When I hit the button, however, nothing happens. I've checked the actual table on phpmyadmin, and the table is unaffected as well.

表格中的所有记录都打印在屏幕上,包括“删除记录”按钮。然而,当我按下按钮时,什么也没有发生。我已经检查了 phpmyadmin 上的实际表,该表也不受影响。

I'm pulling this stuff from a book, so I don't understand why it's not working.

我正在从书中提取这些东西,所以我不明白为什么它不起作用。

回答by Nick Rolando

I do not see where you set the $idvariable.

我没有看到你在哪里设置$id变量。

if (isset($_POST['delete']) && isset($_POST['id']))
{
    //$first = get_post('first');   #not sure what this does
    $id = sanitize($_POST['id'], 'int');  //protect from sql injection
    $query = "DELETE FROM user_master WHERE id=$id";
    if(!mysql_query($query, $db_server))
        echo "DELETE failed: $query<br />" . 
        mysql_error() . "<br /><br />";
}

Please be sure to sanitize any data sent across the network, i.e. $_POST. Also, if idis of a number type, do not surround the value in quotes or mysql will interpret it as a string.

请务必清理通过网络发送的任何数据,即$_POST。此外,如果id是数字类型,请不要将值括在引号中,否则 mysql 会将其解释为字符串。

Lastly, look into using MySqlior PDO, as MySql API has been deprecated.

最后,考虑使用MySqliPDO,因为 MySql API 已被弃用。

回答by sudhakar

Here i defined a variable from that variable i delete the values

在这里,我从该变量中定义了一个变量,我删除了这些值

$id = 2;

$id = 2;

    $query = mysql_query("DELETE FROM table_name WHERE id="'.$id.'"");

note: Delete query id is important otherwise it delete whole rows defaulty.

注意:删除查询 id 很重要,否则它会默认删除整行。

回答by rajesh

$query = mysql_query("DELETE FROM table_name WHERE id=1");

$query = mysql_query("DELETE FROM table_name WHERE id=1");

回答by dwstein

code should read:

代码应为:

$id = get_post('id');

in the first line of the curly braces under the first "if" statement.

在第一个“if”语句下的大括号的第一行中。

回答by andrewsi

$query = "DELETE FROM user_master WHERE id='$id'";

This should probably be:

这应该是:

$query = "DELETE FROM user_master WHERE id='" . $_POST['id'] "'";

But please do some research into SQL injection, and why you should never pass information directly from the user into the database.

但是请对 SQL 注入进行一些研究,以及为什么永远不应该将信息直接从用户传递到数据库中。