删除无效的数据库条目

时间:2015-11-05 13:22:36

标签: php

我有两个页面,第一个显示MySQL数据库中特定字段的所有项目:

DatabaseEntries.php

<?php
include('connect.php');

$result = mysqli_query($db, "SELECT * FROM names") 
    or die(mysqli_error($db));  


echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Firstname</th> <th>lastname</th> <th>Email</th><th></th> ";


while($row = mysqli_fetch_array( $result )) {

    // echo out the contents of each row into a table

    echo "<tr>";
    echo '<td>' . $row['firstname'] . '</td>';
    echo '<td>' . $row['lastname'] . '</td>';
    echo '<td>' . $row['email'] . '</td>';
    echo '<td><a href="delete.php?email=' . $row['email'] . '">Delete</a></td>';
    echo "</tr>"; 

} 
?>

第二页包含删除功能:

Delete.php

 <?php

 include('connect.php');

 // check if the 'id' variable is set in URL, and check that it is valid
 if (isset($_GET['email']) )
 {
 // get id value
 $email = $_GET['email'];

 // delete the entry
 $result = mysqli_query($db, "DELETE FROM names WHERE email=$email")
 or die(mysqli_error($db)); 

 // redirect back to the view page
 header("Location: DatabaseEntries.php");
 }
 else
 // if id isn't set, or isn't valid, redirect back to view page
 {
 header("Location: Error.php");
 }

?>

尝试从数据库中删除项目时出现以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com' at line 1

谁能告诉我为什么?以及如何解决它?

由于

1 个答案:

答案 0 :(得分:0)

quotes

周围添加$email
DELETE FROM names WHERE email='$email'