0

The $time variable here displays as null when I use var_dump. Yet in the db the time shows as 9:00 Am. I am trying to email the time that the passenger wants to be picked up. Why is $time null and how do I fix this?

<?php
    error_reporting(E_ALL);
    ini_set('display_errors',1);
    $host="localhost"; // Host name 
    $username="user"; // Mysql username 
    $password="password"; // Mysql password 
    $db_name="mydb"; // 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");

    //names of the passengers in option tags
    $name = $_POST['name'];

    //this query gets all the records from pickuprequests of the passenger selected
    $query="SELECT * FROM $tbl_name WHERE name = '$name'";
    $result=mysql_query($query, $link);

    // get the location of the passenger from $result
    $search_loc = mysql_fetch_array($result);
    $loc = $search_loc['location'];

    //get time of pickup 
    $search_time = mysql_fetch_array($result);
    $time = $search_time['time'];

    //find how many rows are returned with the passenger name
    $search_row = mysql_query($query, $link);
    $count = mysql_num_rows($search_row);

    //deletes a record assoc with passenger name
    $delete_query = "DELETE FROM pickuprequests WHERE name = '$name'";

    //message for the passenger
    $msg_passenger = "Your driver is on the way! You will receive one last email when the driver arrives.";
    $subject_passenger = "Driver on the way";
    $mailheaders = "MIME-Version: 1.0"."\r\n";
    $mailheaders .= "Content-type: text/html; charset=iso-8859-1"."\r\n";
    $mailheaders .= "From: TripoZipo <tripozipo.com>"."\r\n";

    //passengers email in diff table assoc with name selected from option tags
    $query_email = "SELECT * FROM tzmember WHERE name = '$name'";
    $get_email = mysql_query($query_email,$link);
    $search_email = mysql_fetch_array($get_email);
    $email = $search_email['email']; 

    //message for the driver
    $msg_driver = sprintf( 
    "
    <html>
    <body>
    <table border='1'>
    <tr>
    <td>Passenger</td><td>Location</td><td>Time</td>
    </tr>
    <tr>
    <td>%s</td><td>%s</td><td>%s</td>
    </tr>
    </table>
    </body>
    </html>
    ",
    $name,
    $loc, $time);

    $subject_driver = "Passenger Location";

    //drivers email is the session name for driver.php
    $driver = $_SESSION['driver'];

    //on submit and if a passenger is selected run code
    if(isset($_POST['submit']) && isset($_POST['name'])) {

        //$count returns all the rows assoc with passenger name
        if($count) {
        //mail($email, $subject_passenger, $msg_passenger, $mailheaders); // email passenger your ride is on the way
        mail($driver, $subject_driver, $msg_driver, $mailheaders); // email driver the location
        //mysql_query($delete_query, $link); // finally delete the request as its not pending anymore
        //echo "<script type='text/javascript'> document.location = 'http://mobile.tripozipo.com/confirmation2.php'; </script>";
        }
        else {  //else if no rows are returned

        }
    }
    ?>
crafter
  • 6,246
  • 1
  • 34
  • 46
alexwl813
  • 15
  • 3
  • `$search_email = mysql_fetch_array($get_email);` - should this be `$search_email = mysql_fetch_assoc($get_email);`? Also, no question with `mysql_` should go without a **strong suggestion** to move to mysqli or PDO: http://stackoverflow.com/a/14110189/870729 – random_user_name Jan 01 '16 at 18:14
  • It's also worth mentioning that you are open to sql injection, please read: [**How can I prevent SQL-injection in PHP?**](http://stackoverflow.com/q/60174/4577762). – FirstOne Jan 01 '16 at 18:16
  • @cale_b, can you explain why that should be the change? I agree with the `mysql_*` part... – FirstOne Jan 01 '16 at 18:44

1 Answers1

2

A call to mysql_fetch_array returns a complete ROW from the result set, not just a single column. So you only need to call it once instead of evert time you try and get a value from the row.

<?php
    error_reporting(E_ALL);
    ini_set('display_errors',1);
    $host="localhost"; // Host name 
    $username="username"; // Mysql username 
    $password="password"; // Mysql password 
    $db_name="dbname"; // 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");

    //names of the passengers in option tags
    $name = $_POST['name'];

    //this query gets all the records from pickuprequests of the passenger selected
    $query="SELECT * FROM $tbl_name WHERE name = '$name'";
    $result=mysql_query($query, $link);

    // get the location of the passenger from $result
    $row = mysql_fetch_array($result);
    $loc = $row['location'];

    //get time of pickup 
    //$search_time = mysql_fetch_array($result);
    $time = $row['time'];

    //find how many rows are returned with the passenger name
    //$search_row = mysql_query($query, $link);
    $count = mysql_num_rows($result);





    //deletes a record assoc with passenger name
    $delete_query = "DELETE FROM pickuprequests WHERE name = '$name'";

    //message for the passenger
    $msg_passenger = "Your driver is on the way! You will receive one last email when the driver arrives.";
    $subject_passenger = "Driver on the way";
    $mailheaders = "MIME-Version: 1.0"."\r\n";
    $mailheaders .= "Content-type: text/html; charset=iso-8859-1"."\r\n";
    $mailheaders .= "From: TripoZipo <tripozipo.com>"."\r\n";

    //passengers email in diff table assoc with name selected from option tags
    $query_email = "SELECT * FROM tzmember WHERE name = '$name'";
    $get_email = mysql_query($query_email,$link);
    $search_email = mysql_fetch_array($get_email);
    $email = $search_email['email']; 

    //message for the driver
    $msg_driver = sprintf( 
    "
    <html>
    <body>
    <table border='1'>
    <tr>
    <td>Passenger</td><td>Location</td><td>Time</td>
    </tr>
    <tr>
    <td>%s</td><td>%s</td><td>%s</td>
    </tr>
    </table>
    </body>
    </html>
    ",
    $name,
    $loc, $time);

    $subject_driver = "Passenger Location";

    //drivers email is the session name for driver.php
    $driver = $_SESSION['driver'];

    //on submit and if a passenger is selected run code
    if(isset($_POST['submit']) && isset($_POST['name'])) {

        //$count returns all the rows assoc with passenger name
        if($count) {
        //mail($email, $subject_passenger, $msg_passenger, $mailheaders); // email passenger your ride is on the way
        mail($driver, $subject_driver, $msg_driver, $mailheaders); // email driver the location
        //mysql_query($delete_query, $link); // finally delete the request as its not pending anymore
        //echo "<script type='text/javascript'> document.location = 'http://mobile.tripozipo.com/confirmation2.php'; </script>";
        }
        else {  //else if no rows are returned

        }
    }
?>

Please dont use the mysql_ database extensions, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • I was intrigued by the fact that php was not throwing an error while trying to access key of null variable, so I decided to look around. Turns out that it's the expected behavior: [Key of null variable equals null not error](http://stackoverflow.com/q/32650058/4577762)...huh – FirstOne Jan 01 '16 at 19:36
  • Thank you for post. This helped resolve my issue. Why did $time show as null? Was it because I was using mysql_fetch_array more than once? – alexwl813 Jan 02 '16 at 00:24
  • Yes. I assume the query ony returned one row, so the seconds `mysql_query()` would have failed and `$row` would not be filled. – RiggsFolly Jan 02 '16 at 00:27