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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器