0

Let me break down this page for you guys. Its called driver.php and the objective of this page is for a driver to pick up pending pick up requests from passengers (requests will show in options tags as their emails). THE ISSUE is that the DELETE QUERY is not working for some reason. I want to be able to delete the existing pickup request for table when a driver gets it. YES I've searched on this website for answers and google for help that is why I am posting here. Thank you all for your help.

<? 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>
  • Data protection issues aside, it's really unusual to want to do this. Normally you would just have some kind of marker for inactive rows. – Strawberry Dec 24 '15 at 19:29
  • http://php.net/manual/en/function.mysql-error.php would have helped you here on your delete query or any other for that matter. – Funk Forty Niner Dec 24 '15 at 19:38
  • as an observation, I would agree with never really tossing data. Inactive marking will allow you to reactivate it in the case where your customer said 'opps' i want it again – DaveTheRave Dec 24 '15 at 19:44
  • Good point, but in this case I need to delete the record because I do not want it to appear in the option tags. I am not familiar with what you mean by inactive markings. And Fred thanks for the manual, tons of good information in there thank you. –  Dec 24 '15 at 19:51

3 Answers3

3

There are small bugs in your code. Remove * from the delete query and surround your strings inside quotes(' '), like this:

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

Check for errors on your queries when testing:

Sidenote: Please don't use mysql_ database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. Use mysqli or PDO extensions instead. And this is why you shouldn't use mysql_ functions

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Thank you for the feedback! It worked when I added the single quotes and took away the * sign. Also, I will look into mysqli. Happy holidays! –  Dec 24 '15 at 19:46
0
$delete_query = "DELETE * FROM pickuprequests WHERE email = $email";

should be?

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

Single Quotes?

DaveTheRave
  • 463
  • 2
  • 5
  • quotes, yes; star/asterisk, no. – Funk Forty Niner Dec 24 '15 at 19:37
  • Yes it worked when I took away * and added single quotes. Why is that? –  Dec 24 '15 at 19:47
  • * is reserved for a shorthand notation to take every column of the final composite table from a select statement. Vs listing each field out in the SELECT .... clause – DaveTheRave Dec 24 '15 at 19:50
  • INSERT, UPDATE, DELETE work on rows.. no results coming back.. so no * is ever needed and is not allowed. You need the single-quotes because you are matching a char/varchar in your where.. without them .. sql will think thats part of the structure syntax.. vs an actual text string value.. – DaveTheRave Dec 24 '15 at 19:52
0
$delete_query = "DELETE * FROM pickuprequests WHERE email = $email";

It should be..

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

Note... You dont have to declare that you want to delete all from that row, if you find it with id, email or something (WHERE clausule) it will automaticly delete that row. So no need for * symbol.

fugitive
  • 357
  • 2
  • 8
  • 26