MySQL DELETE查询未执行

时间:2015-12-24 19:22:43

标签: php html mysql

让我为你们打破这个页面。它叫做driver.php,这个页面的目的是让司机接收来自乘客的待处理接听请求(请求将在选项标签中显示为他们的电子邮件)。问题是DELETE QUERY由于某种原因不起作用。我希望能够在驱动程序获取表时删除现有的表的拾取请求。是的我已经在这个网站上搜索了答案并谷歌寻求帮助,这就是我在这里发帖的原因。谢谢大家的帮助。

<? session_start(); ?>
<?php

if(!isset($_SESSION['driver'])) {
    header('Location: mobile.php');
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<script>
<!--This redirects the user to our mobile view-->
if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {

} else {
  window.location = "http://tripozipo.com/index.php"; 
}
</script>
<!--This is for mobile JQuery-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/jquery.timeentry.css"> 
<script type="text/javascript" src="js/jquery.plugin.js"></script> 
<script type="text/javascript" src="js/jquery.timeentry.js"></script>

<?php
$host="localhost"; // Host name 
$username="fakename"; // Mysql username 
$password="fakepass"; // Mysql password 
$db_name="fakedbname"; // Database name 
$tbl_name="pickuprequests"; // Table name 

// Connect to server and select database.
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name", $link)or die("cannot select DB");

$email = $_POST['email'];

//this query gets all the records from pickuprequests so we can use mysql fetch array to ouput
//the emails (of ppl asking for rides) in the html option tags
$search_for_email="SELECT * FROM $tbl_name";
$result_for_email=mysql_query($search_for_locations, $link);

//get all the data in the table assoc with the email that the driver chooses
//this is so we can get the location assoc with the email for mailing purposes ect
$search_for_loc="SELECT * FROM $tbl_name WHERE email = '$email'";
$result_for_loc=mysql_query($search_for_loc, $link);

//get the loc assoc with the email and set it to loc variable for mailing purposes ect
$loc = mysql_fetch_array($result_for_loc);
$loc = $loc['location'];

//find how many rows are returned with that email assoc with that loc
//because if there is already a duplicate pickup request we want to delete it
$query="SELECT * FROM $tbl_name WHERE email = '$email' AND location = '$loc'";
$queryresult = mysql_query($query, $link);
$count=mysql_num_rows($queryresult);

//this query is so we can delete the records from pickup assoc with email (person that request ride)
$delete_query = "DELETE * FROM pickuprequests WHERE email = $email";

/* THIS IS CODE THAT HAS BEEN COMMENTED OUT
$altloc = $_POST['altloc'];
$time = $_POST['time'];
*/
$msg = "Your driver is on the way! You will receive one last email when the driver arrives.";
$subject = "Pickup Request";
$mailheaders = "From: TripoZipo <tripozipo.com> \r\n";

$driver = $_SESSION['driver'];

//When the driver chooses a pickup request and hits submit then delete the pick up request from the table
//'pickuprequests' so that another driver cant choose it, because its already been accepted

if(isset($_POST['submit']) && isset($_POST['email'])) {
    //if we find data in row return then there must already be a pick request
    //if there is then we want to delete that request but its not working
    if($count) {
    mysql_query($delete_query, $link);
    /* THIS IS CODE THAT HAS BEEN COMMENTED OUT
    mail($email, $subject, $msg, $mailheaders);
    mail($driver, "Pickup Address", "Here is the location: ".$loc, $mailheaders);
    echo "<script type='text/javascript'> document.location = 'http://mobile.tripozipo.com/confirmation.php'; </script>";
    */
    }
    else
    /* THIS IS CODE THAT HAS BEEN COMMENTED OUT
    mail($email, $subject, $msg, $mailheaders);
    mail($driver, "Pickup Address", "Here is the location: ".$loc, $mailheaders);
    echo "<script type='text/javascript'> document.location = 'http://mobile.tripozipo.com/confirmation.php'; </script>";
    */
    }

?>

</head>
<body>

<div data-role="page">
  <div  class="ui-content" align="center">
  <img src="images/logo.png" style="height:100px; width: 202px;">
    <h2>Hello <?php echo("{$_SESSION['driver']}"); ?>, welcome to your dashboard</h2>

    <form method="post" action="driver.php" data-ajax="false">
      <label for="location">Here are some pending pickups</label>
      <select name="email">
        <?php while ($row1=mysql_fetch_array($result_for_email)) {
            $title = $row1['email'];
            echo "<option>$title</option>";
        }
        ?>
      </select>
      <?php 
        if(isset($_POST['submit'])) {
        if(!$count) {
        echo "<b style='color: red;'>Oops! It looks like someone else took it</b>";
        }}?>
      <input type="submit" name="submit" value="Take">
      <p align="center">Or</p>
      <a href="logout.php"><input type="button" value="Log Out"></a>
      <a href="mobile.php"><input type="button" value="Back"></a>

    </form>
  </div>
</div>

</body>
</html>

3 个答案:

答案 0 :(得分:3)

您的代码中存在小错误。从删除查询中删除*并将字符串括在引号(' ')中,如下所示:

$delete_query = "DELETE FROM pickuprequests WHERE email = '$email'";

测试时检查查询错误:

旁注:请不要使用mysql_数据库扩展,它们在PHP 5.5.0中已弃用,并已在PHP 7.0.0中删除。请改用mysqliPDO扩展程序。这是why you shouldn't use mysql_ functions

答案 1 :(得分:0)

$delete_query = "DELETE * FROM pickuprequests WHERE email = $email";

应该是?

$delete_query = "DELETE * FROM pickuprequests WHERE email = '$email'";

单引号?

答案 2 :(得分:0)

$delete_query = "DELETE * FROM pickuprequests WHERE email = $email";

应该是......

$delete_query = "DELETE FROM pickuprequests WHERE email = '{$email}'";

注意......您不必声明要从该行中删除所有内容,如果您使用id,email或其他内容(WHERE clausule)找到它,它将自动删除该行。 所以不需要*符号。