简体   繁体   English

删除行mysql php

[英]Delete row mysql php

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. 表格中的所有记录都将打印在屏幕上,包括“ DELETE RECORD”按钮。 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. 我正在从书中提取这些东西,所以我不明白为什么它不起作用。

I do not see where you set the $id variable. 我看不到您在哪里设置$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, ie $_POST . 请确保清理通过网络发送的所有数据,即$_POST Also, if id is of a number type, do not surround the value in quotes or mysql will interpret it as a string. 另外,如果id是数字类型,请不要在引号中使用该值,否则mysql会将其解释为字符串。

Lastly, look into using MySqli or PDO , as MySql API has been deprecated. 最后,考虑使用MySqliPDO ,因为MySql API已被弃用。

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很重要,否则默认会删除整行。

$ query = mysql_query(“从表名删除,其中id = 1”);

$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注入进行一些研究,以及为什么永远不要将信息直接从用户传递到数据库。

code should read: 代码应为:

$id = get_post('id');

in the first line of the curly braces under the first "if" statement. 在第一个“ if”语句下大括号的第一行中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM