doutuo7156 2015-11-05 13:22 采纳率: 100%
浏览 60
已采纳

删除无效的数据库条目

I have two pages, the first shows all items from a particular field in a MySQL database:

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>"; 

} 
?>

the second page contains the delete function:

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");
 }

?>

I get the following error when trying to delete an item from the database:

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

Can anyone tell me why? and what to do to fix it?

Thanks

  • 写回答

1条回答 默认 最新

  • duandi4238 2015-11-05 13:24
    关注

    Add quotes around the $email

    DELETE FROM names WHERE email='$email'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址