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>
$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>
您的代码有错字 -- 字符串末尾有一个多余的分号。 ID ='".$ID."'";);
应该是 ID ='".$ID."'");
除此之外,您的代码包含一个严重的安全漏洞,它是开放给 SQL injection。如果有人将 post 3') OR TRUE; --
作为 ID 字段...您只是丢失了 table 中的每条记录,因为您的删除查询现在适用于每条记录。这就是为什么对来自用户的任何信息进行清理是绝对重要的。大多数用户不会尝试破坏您的网站,但有些用户会。
mysql_*
函数族是 officially deprecated(看到红色通知了吗?)。这意味着它没有被更新,任何新代码都应该停止使用它。备选方案是 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 那里应该有什么值。您可以通过将值绑定到参数来实现。这是一种奇特的说法 "tell PDO what goes there" -- 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 查询时牢记这一点至关重要。
文档
PDO
- http://us1.php.net/manual/en/pdo.errorinfo.php
PDO::prepare
- http://us1.php.net/manual/en/pdo.prepare.php
PDOStatement::execute
- http://php.net/manual/en/pdostatement.execute.php
我似乎真的无法弄清楚为什么这段代码在删除按钮代码的相关部分出现问题。任何人都可以对我在这里做错的事情采取行动吗?
我希望这只是语法,但如果它是基本的理解,我将不胜感激,因为我还需要让此表单执行更多这些更新功能。
<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>
$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>
您的代码有错字 -- 字符串末尾有一个多余的分号。 ID ='".$ID."'";);
应该是 ID ='".$ID."'");
除此之外,您的代码包含一个严重的安全漏洞,它是开放给 SQL injection。如果有人将 post 3') OR TRUE; --
作为 ID 字段...您只是丢失了 table 中的每条记录,因为您的删除查询现在适用于每条记录。这就是为什么对来自用户的任何信息进行清理是绝对重要的。大多数用户不会尝试破坏您的网站,但有些用户会。
mysql_*
函数族是 officially deprecated(看到红色通知了吗?)。这意味着它没有被更新,任何新代码都应该停止使用它。备选方案是 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 那里应该有什么值。您可以通过将值绑定到参数来实现。这是一种奇特的说法 "tell PDO what goes there" -- 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 查询时牢记这一点至关重要。
文档
PDO
- http://us1.php.net/manual/en/pdo.errorinfo.phpPDO::prepare
- http://us1.php.net/manual/en/pdo.prepare.phpPDOStatement::execute
- http://php.net/manual/en/pdostatement.execute.php