繁体   English   中英

PHP MySQL删除行不起作用

[英]PHP MySQL delete row not working

我真的似乎无法弄清楚为什么此代码在“删除”按钮代码的相关部分中被破坏了。 谁能在我这里做错的事情上大放异彩?

我希望它只是语法,但是如果是基本的理解,我将不胜感激,因为我还需要使此表单执行更多这些更新功能。

<html>
<head>
<title>Call Log System</title>
</head>

<body>
<?php
$db_host = 'localhost';
$db_user = '*********';
$db_pwd = '********';
$database = 'call_log';
$table = 'Project_Submissions';





if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");
if (!mysql_select_db($database))
    die("Can't select database");

//Display all fields
$result = mysql_query("SELECT * FROM {$table}");

if (!$result) 
{
    die("Query to show fields from table failed");
}


//Main Table 
echo "<table border='1px' width='100%'>
<tr>
<th style='font-size:18px;width:20px;'>ID</th>
<th style='font-size:18px;'>Customer Name</th>
<th style='font-size:18px;'>Phone #</th>
<th style='font-size:18px;'>Address</th>
<th style='font-size:18px;'>Time Zone</th>
<th style='font-size:18px;'>E-mail</th>
<th style='font-size:18px;'>Alt Phone</th>
<th style='font-size:18px;'>Vehicle</th>
<th style='font-size:18px;'>Project Start</th>
<th style='font-size:18px;'>Project Description</th>
<th style='font-size:18px;'>RDM</th>
<th style='font-size:18px;'>Phone Attempt 1</th>
<th style='font-size:18px;'>Phone Attempt 2</th>
<th style='font-size:18px;'>Phone Attempt 3</th>
<th style='font-size:18px;'>Email Attempt</th>
<th style='font-size:18px;'>Notes</th>
<th style='font-size:18px;'>Received Date</th>
</tr>";


while ($row = mysql_fetch_array($result)) 
{
    echo "<tr>
    <td style='font-size:12px;'><center>{$row['ID']}</center></td>
    <td style='font-size:12px;'>{$row['First_Name']} {$row['Last_Name']}</td>
    <td style='font-size:12px;'><center><a href=\"tel:{$row['Phone']}\">{$row['Phone']}</a></center></td>
    <td style='font-size:12px;'><center>{$row['Street']} {$row['City']} {$row['State_Country']}</center></td>
    <td style='font-size:12px;'><center><div style=\"width:150px\">{$row['Time_Zone']}</div></center></td>
    <td style='font-size:12px;'><center><a href=\"mailto:{$row['Email']}?Subject=FantomWorks\" target=\"_top\">{$row['Email']}</a></center></td>
    <td style='font-size:12px;'><center>{$row['Alt_Phone']}</center></td>
    <td style='font-size:12px;'><center>{$row['Year']} {$row['Make']} {$row['Model']}</center></td>
    <td style='font-size:12px;'><center>{$row['Project_Start']}</center></td>
    <td style='font-size:12px;width:500px;'><div style=\"overflow-x:auto; max-height:100px\">{$row['Project_Description']}</div></td>
    <td style='font-size:12px;'><center>{$row['Restoration_Decision_Matrix']}</center></td>
    <td style='font-size:12px;'><center>



        <form action='".$_SERVER['PHP_SELF']."' method='post'>
        <input type='hidden' id='ID' name='ID' value='{$row['ID']}' />
        <input type='submit' name='formDelete' id='formDelete' value='Delete' />
        </form>




    </center></td>
    <td style='font-size:12px;'><center><button type=\"submit\" form=\"form1\" value=\"Submit\">Called</button></center></td>
    <td style='font-size:12px;'><center><button type=\"submit\" form=\"form1\" value=\"Submit\">Called</button></center></td>
    <td style='font-size:12px;'><center><button type=\"submit\" form=\"form1\" value=\"Submit\">Emailed</button></center></td>
    <td style='font-size:12px;'><center>Text Area</center></td>
    <td style='font-size:12px;'><center>{$row['Received_Date']}</center></td>
    </tr>";

}

//Check to see if delete button is pressed
if(isset($_POST['formDelete']))
{
    if(isset($_POST['ID']) && !empty($_POST['ID']))
    {
        $ID = $_POST['ID'];
        $result = mysql_query("DELETE FROM Project_Submissions WHERE ID ='".$ID."'";);
    }
}   

?>
</body>
</html>

您的代码有错别字-字符串末尾有多余的分号。 ID ='".$ID."'";);应该为ID ='".$ID."'");

除此之外,您的代码还包含一个严重的安全漏洞,它可以进行SQL注入 如果有人张贴3') OR TRUE; -- 3') OR TRUE; --作为ID字段...您刚刚丢失了该表中的每条记录,因为您的删除查询现在适用于每条记录。 因此,对来自用户的任何信息进行清理绝对至关重要。 大多数用户不会尝试破坏您的网站,但有些会。

正式 mysql_*系列函数(请参见红色通知?)。 这意味着它不会被更新,任何新代码都应停止使用它。 备选方案是PDO或Mysqli,两者都与您正在使用的非常相似。 您唯一了解的新知识就是参数化语句!

这是如何用PDO替换不推荐使用的mysql_ *函数,以及如何检查查询中的错误。

首先,我们将建立数据库连接。 这代替了mysql_connect东西:

$pdo = new PDO('mysql:host='.$db_host.';dbname='.$database , $db_user, $db_pass);

接下来,我们将使用参数准备语句:

$statement = $pdo->prepare('
    DELETE FROM 
        Project_Submissions 
    WHERE 
        ID = :id
');

查询的:id部分称为参数。 在执行该语句之前,您必须告诉PDO那里应该有什么值。 您可以通过将值绑定到参数来实现。 这是说“告诉PDO那里发生了什么”的一种奇特的方式-PDO将负责清理价值。

因此,接下来,我们将立即执行语句并绑定参数:

$statement->execute(array(
    'id'=>$_POST['ID']
));

现在,如果要确保确实删除了某些内容,可以使用$statement->rowCount() -它会告诉您受影响的行数。

放在一起:

$pdo = new PDO('mysql:host='.$db_host.';dbname='.$database , $db_user, $db_pass);
$statement = $pdo->prepare('
    DELETE FROM 
        Project_Submissions 
    WHERE 
        ID = :id
');
$statement->execute(array(
    'id'=>$_POST['ID']
));

无论您是在学习,还是试图弄清楚这些无聊的孩子正在谈论的话题,查询安全都是一个重要的主题。 继续前进,在编写SQL查询时务必牢记这一点至关重要。

文献资料

$result = $conn->query("DELETE FROM Project_Submissions WHERE ID =$row['ID']");

我看不到$ row ['ID']的位置。 看起来就像在该语句的上方,您将要删除的行的ID设置为$ quoteid。

如果是这种情况,您将要说:

 $result = $conn->query("DELETE FROM Project_Submissions WHERE ID = $quoteid");

修复。 来呀!

while ($row = mysql_fetch_array($result)) 
{
    echo "<tr>
    <td style='font-size:12px;'><center>{$row['ID']}</center></td>
    <td style='font-size:12px;'>{$row['First_Name']} {$row['Last_Name']}</td>
    <td style='font-size:12px;'><center><a href=\"tel:{$row['Phone']}\">{$row['Phone']}</a></center></td>
    <td style='font-size:12px;'><center>{$row['Street']} {$row['City']} {$row['State_Country']}</center></td>
    <td style='font-size:12px;'><center><div style=\"width:150px\">{$row['Time_Zone']}</div></center></td>
    <td style='font-size:12px;'><center><a href=\"mailto:{$row['Email']}?Subject=FantomWorks\" target=\"_top\">{$row['Email']}</a></center></td>
    <td style='font-size:12px;'><center>{$row['Alt_Phone']}</center></td>
    <td style='font-size:12px;'><center>{$row['Year']} {$row['Make']} {$row['Model']}</center></td>
    <td style='font-size:12px;'><center>{$row['Project_Start']}</center></td>
    <td style='font-size:12px;width:500px;'><div style=\"overflow-x:auto; max-height:100px\">{$row['Project_Description']}</div></td>
    <td style='font-size:12px;'><center>{$row['Restoration_Decision_Matrix']}</center></td>
    <td style='font-size:12px;'><center>



        <form action='".$_SERVER['PHP_SELF']."' method='post'>
        <input type='hidden' id='ID' name='ID' value='{$row['ID']}' />
        <input type='submit' name='formDelete' id='formDelete' value='Delete' />
        </form>




    </center></td>
    <td style='font-size:12px;'><center><button type=\"submit\" form=\"form1\" value=\"Submit\">Called</button></center></td>
    <td style='font-size:12px;'><center><button type=\"submit\" form=\"form1\" value=\"Submit\">Called</button></center></td>
    <td style='font-size:12px;'><center><button type=\"submit\" form=\"form1\" value=\"Submit\">Emailed</button></center></td>
    <td style='font-size:12px;'><center>Text Area</center></td>
    <td style='font-size:12px;'><center>{$row['Received_Date']}</center></td>
    </tr>";

}

//Check to see if delete button is pressed
if(isset($_POST['formDelete']))
{
    if(isset($_POST['ID']) && !empty($_POST['ID']))
    {
        $ID = $_POST['ID'];
        $result = mysql_query("DELETE FROM Project_Submissions WHERE ID ='".$ID."'");
    }
}   

?>
</body>
</html>

暂无
暂无

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

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