Retrieve data from database and set to a variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    Retrieve data from database and set to a variable

    I am having trouble getting this code to work, and was wondering if
    someone could tell me what I am doing wrong.

    --------------------------------------
    CODE--------------------------------------
    if (isset($_POST['submitted'])) {

    require_once("d atabase.php");
    $appt_date = $_POST['appt_month'] . '/' . $_POST['appt_day'] . '/' .
    $_POST['appt_year'];
    $phone = $_POST['phone1'] . '-' . $_POST['phone2'] . '-' .
    $_POST['phone3'];
    $appt_time = $_POST['appt_time'];

    $query = "SELECT appt_date, appt_time FROM pest_control WHERE
    appt_date = " . "'" . $appt_date . "' and appt_time =" . "'" .
    $appt_time . "'";
    $result = mysql_query ($query);
    $rDate = $row['appt_date'];
    $rTime = $row['appt_time'];
    print $rDate;
    print $rTime;
    $num = mysql_num_rows( $result);
    print $num;
    //echo $num . " Number of rows returned<br>\n" ;
    //if ($row != mysql_fetch_arr ay ($result)) {
    //while ($row != mysql_fetch_arr ay ($result)) {
    if ($num == 0)
    {
    $query = "INSERT INTO pest_control (appt_date, appt_time,
    appt_type, name, address1, address2, city, state, zip, phone,
    cross_streets)
    VALUES ("."'" . $appt_date . "', "."'" .
    $_POST['appt_time'] . "', "."'" . $_POST['appt_type'] . "', "."'" .
    $_POST['name'] . "',
    "."'" . $_POST['address1'] . "', "."'" .
    $_POST['address2'] . "', "."'" . $_POST['city'] . "',
    "."'" . $_POST['state'] . "', "."'" . $_POST['zip'] . "',
    "."'" . $phone . "',
    "."'" . $_POST['cross_streets'] . "')";

    $result = mysql_query ($query); // Execute the query.
    mysql_close($co nn); // Close the database connection.
    exit;
    }
    elseif ($appt_date == $rDate) //Check for conflicting
    appointment date
    {
    echo "We're sorry that appointment date is unavailable, please
    select another date.";
    user_form_resub mit();
    }
    elseif ($appt_date == $rTime) //Check for conflicting appoitment
    time
    {
    echo "We're sorry that appointment time is unavailable, please
    select another time.";
    user_form_resub mit();
    }
    elseif ($appt_date == $rDate || $appt_time == $rTime) //Check for
    conflicting date and time
    {
    echo "<h3 align='center'> We're sorry that appointment date and time
    are already scheduled, please select another date and time.</h3>";
    user_form_resub mit();
    }
    }
    }

    ------------------------------END
    CODE---------------------------------------

    What I am trying to do is run a query against my database to see if a
    appointment time and date are already scheduled, then set the
    variables $rDate and $rTime from the query I ran to check those
    values.

    When I print the number of rows it returns 1, telling me that this
    date and time are already scheduled, but then my forms loads again but
    doesn't display my message about the conflicting date and time.

    I don't get anything returned when I try to print the $rDate and
    $rTime to screen.

    Any help is appreciated.

    Thank you in advance for your assistance.

    ~John
  • Jerry Stuckle

    #2
    Re: Retrieve data from database and set to a variable

    John wrote:
    I am having trouble getting this code to work, and was wondering if
    someone could tell me what I am doing wrong.
    >
    --------------------------------------
    CODE--------------------------------------
    if (isset($_POST['submitted'])) {
    >
    require_once("d atabase.php");
    $appt_date = $_POST['appt_month'] . '/' . $_POST['appt_day'] . '/' .
    $_POST['appt_year'];
    $phone = $_POST['phone1'] . '-' . $_POST['phone2'] . '-' .
    $_POST['phone3'];
    $appt_time = $_POST['appt_time'];
    >
    $query = "SELECT appt_date, appt_time FROM pest_control WHERE
    appt_date = " . "'" . $appt_date . "' and appt_time =" . "'" .
    $appt_time . "'";
    $result = mysql_query ($query);
    $rDate = $row['appt_date'];
    $rTime = $row['appt_time'];
    print $rDate;
    print $rTime;
    $num = mysql_num_rows( $result);
    print $num;
    //echo $num . " Number of rows returned<br>\n" ;
    //if ($row != mysql_fetch_arr ay ($result)) {
    //while ($row != mysql_fetch_arr ay ($result)) {
    if ($num == 0)
    {
    $query = "INSERT INTO pest_control (appt_date, appt_time,
    appt_type, name, address1, address2, city, state, zip, phone,
    cross_streets)
    VALUES ("."'" . $appt_date . "', "."'" .
    $_POST['appt_time'] . "', "."'" . $_POST['appt_type'] . "', "."'" .
    $_POST['name'] . "',
    "."'" . $_POST['address1'] . "', "."'" .
    $_POST['address2'] . "', "."'" . $_POST['city'] . "',
    "."'" . $_POST['state'] . "', "."'" . $_POST['zip'] . "',
    "."'" . $phone . "',
    "."'" . $_POST['cross_streets'] . "')";
    >
    $result = mysql_query ($query); // Execute the query.
    mysql_close($co nn); // Close the database connection.
    exit;
    }
    elseif ($appt_date == $rDate) //Check for conflicting
    appointment date
    {
    echo "We're sorry that appointment date is unavailable, please
    select another date.";
    user_form_resub mit();
    }
    elseif ($appt_date == $rTime) //Check for conflicting appoitment
    time
    {
    echo "We're sorry that appointment time is unavailable, please
    select another time.";
    user_form_resub mit();
    }
    elseif ($appt_date == $rDate || $appt_time == $rTime) //Check for
    conflicting date and time
    {
    echo "<h3 align='center'> We're sorry that appointment date and time
    are already scheduled, please select another date and time.</h3>";
    user_form_resub mit();
    }
    }
    }
    >
    ------------------------------END
    CODE---------------------------------------
    >
    What I am trying to do is run a query against my database to see if a
    appointment time and date are already scheduled, then set the
    variables $rDate and $rTime from the query I ran to check those
    values.
    >
    When I print the number of rows it returns 1, telling me that this
    date and time are already scheduled, but then my forms loads again but
    doesn't display my message about the conflicting date and time.
    >
    I don't get anything returned when I try to print the $rDate and
    $rTime to screen.
    >
    Any help is appreciated.
    >
    Thank you in advance for your assistance.
    >
    ~John
    >
    Hi, John,

    Well, to start with, where does the $row array come from, i.e.

    $rDate = $row['appt_date'];
    $rTime = $row['appt_time'];

    You need to call mysql_fetch_arr ay() BEFORE using these to retrieve the
    data returned.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • John

      #3
      Re: Retrieve data from database and set to a variable

      On Apr 19, 8:32 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      John wrote:
      I am having trouble getting this code to work, and was wondering if
      someone could tell me what I am doing wrong.
      >
      --------------------------------------
      CODE--------------------------------------
      if (isset($_POST['submitted'])) {
      >
         require_once("d atabase.php");
         $appt_date = $_POST['appt_month'] . '/' . $_POST['appt_day'] . '/' .
      $_POST['appt_year'];
         $phone = $_POST['phone1'] . '-' . $_POST['phone2'] . '-' .
      $_POST['phone3'];
         $appt_time = $_POST['appt_time'];
      >
         $query = "SELECT appt_date, appt_time FROM pest_control WHERE
      appt_date = " . "'" . $appt_date . "' and appt_time =" . "'" .
      $appt_time . "'";
         $result = mysql_query ($query);
         $rDate = $row['appt_date'];
         $rTime = $row['appt_time'];
         print $rDate;
         print $rTime;
         $num = mysql_num_rows( $result);
         print $num;
         //echo $num  . " Number of rows returned<br>\n" ;
                 //if ($row != mysql_fetch_arr ay ($result)) {
                 //while ($row != mysql_fetch_arr ay ($result)) {
                 if ($num == 0)
                 {
                                         $query = "INSERT INTO pest_control (appt_date, appt_time,
      appt_type, name, address1, address2, city, state, zip, phone,
      cross_streets)
                                                           VALUES ("."'" . $appt_date . "', "."'" .
      $_POST['appt_time'] . "', "."'" . $_POST['appt_type'] . "', "."'" .
      $_POST['name'] . "',
                                                                                 "."'" . $_POST['address1'] . "', "."'" .
      $_POST['address2'] . "', "."'" . $_POST['city'] . "',
                                                                                 "."'" . $_POST['state'] . "', "."'" . $_POST['zip'] . "',
      "."'" . $phone . "',
                                                                                 "."'" . $_POST['cross_streets'] . "')";
      >
                                         $result = mysql_query ($query);                                // Execute the query.
                                         mysql_close($co nn);                                                     // Close the database connection.
                                         exit;
                 }
                 elseif ($appt_date == $rDate)                                  //Check for conflicting
      appointment date
                 {
                         echo "We're sorry that appointment date is unavailable, please
      select another date.";
                         user_form_resub mit();
                 }
                 elseif ($appt_date == $rTime)                                  //Check for conflictingappo itment
      time
                 {
                         echo "We're sorry that appointment time is unavailable, please
      select another time.";
                         user_form_resub mit();
                 }
                 elseif ($appt_date == $rDate ||  $appt_time== $rTime)          //Check for
      conflicting date and time
                 {
                         echo "<h3 align='center'> We're sorry that appointment date and time
      are already scheduled, please select another date and time.</h3>";
                         user_form_resub mit();
                 }
         }
      }
      >
      ------------------------------END
      CODE---------------------------------------
      >
      What I am trying to do is run a query against my database to see if a
      appointment time and date are already scheduled, then set the
      variables $rDate and $rTime from the query I ran to check those
      values.
      >
      When I print the number of rows it returns 1, telling me that this
      date and time are already scheduled, but then my forms loads again but
      doesn't display my message about the conflicting date and time.
      >
      I don't get anything returned when I try to print the $rDate and
      $rTime to screen.
      >
      Any help is appreciated.
      >
      Thank you in advance for your assistance.
      >
      ~John
      >
      Hi, John,
      >
      Well, to start with, where does the $row array come from, i.e.
      >
              $rDate = $row['appt_date'];
              $rTime = $row['appt_time'];
      >
      You need to call mysql_fetch_arr ay() BEFORE using these to retrieve the
      data returned.
      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstuck...@attgl obal.net
      =============== ===- Hide quoted text -
      >
      - Show quoted text -
      Hi Jerry,

      Thank you so much for you help. That fixed my problem.

      I appreciate your time in looking at this for me.

      ~John

      Comment

      Working...