Retrieve data from database and set to a variable

Retrieve data from database and set to a variable

am 20.04.2008 05:08:20 von John

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("database.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
\n";
//if ($row != mysql_fetch_array ($result)) {
//while ($row != mysql_fetch_array ($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($conn); // 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_resubmit();
}
elseif ($appt_date == $rTime) //Check for conflicting appoitment
time
{
echo "We're sorry that appointment time is unavailable, please
select another time.";
user_form_resubmit();
}
elseif ($appt_date == $rDate || $appt_time == $rTime) //Check for
conflicting date and time
{
echo "

We're sorry that appointment date and time
are already scheduled, please select another date and time.

";
user_form_resubmit();
}
}
}

------------------------------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

Re: Retrieve data from database and set to a variable

am 20.04.2008 05:32:43 von Jerry Stuckle

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("database.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
\n";
> //if ($row != mysql_fetch_array ($result)) {
> //while ($row != mysql_fetch_array ($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($conn); // 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_resubmit();
> }
> elseif ($appt_date == $rTime) //Check for conflicting appoitment
> time
> {
> echo "We're sorry that appointment time is unavailable, please
> select another time.";
> user_form_resubmit();
> }
> elseif ($appt_date == $rDate || $appt_time == $rTime) //Check for
> conflicting date and time
> {
> echo "

We're sorry that appointment date and time
> are already scheduled, please select another date and time.

";
> user_form_resubmit();
> }
> }
> }
>
> ------------------------------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_array() BEFORE using these to retrieve the
data returned.

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

Re: Retrieve data from database and set to a variable

am 20.04.2008 05:42:03 von John

On Apr 19, 8:32=A0pm, Jerry Stuckle wrote:
> 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'])) {
>
> > =A0 =A0require_once("database.php");
> > =A0 =A0$appt_date =3D $_POST['appt_month'] . '/' . $_POST['appt_day'] . =
'/' .
> > $_POST['appt_year'];
> > =A0 =A0$phone =3D $_POST['phone1'] . '-' . $_POST['phone2'] . '-' .
> > $_POST['phone3'];
> > =A0 =A0$appt_time =3D $_POST['appt_time'];
>
> > =A0 =A0$query =3D "SELECT appt_date, appt_time FROM pest_control WHERE
> > appt_date =3D " . "'" . $appt_date . "' and appt_time =3D" . "'" .
> > $appt_time . "'";
> > =A0 =A0$result =3D mysql_query ($query);
> > =A0 =A0$rDate =3D $row['appt_date'];
> > =A0 =A0$rTime =3D $row['appt_time'];
> > =A0 =A0print $rDate;
> > =A0 =A0print $rTime;
> > =A0 =A0$num =3D mysql_num_rows($result);
> > =A0 =A0print $num;
> > =A0 =A0//echo $num =A0. " Number of rows returned
\n";
> > =A0 =A0 =A0 =A0 =A0 =A0//if ($row !=3D mysql_fetch_array ($result)) {
> > =A0 =A0 =A0 =A0 =A0 =A0//while ($row !=3D mysql_fetch_array ($result)) {=

> > =A0 =A0 =A0 =A0 =A0 =A0if ($num == 0)
> > =A0 =A0 =A0 =A0 =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$=
query =3D "INSERT INTO pest_control (appt_date, appt_time,
> > appt_type, name, address1, address2, city, state, zip, phone,
> > cross_streets)
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0VALUES ("."'" . $appt_date . "', "."'" .
> > $_POST['appt_time'] . "', "."'" . $_POST['appt_type'] . "', "."'" .
> > $_POST['name'] . "',
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0"."'" . $_POST['address1'] . "', "."'" .
> > $_POST['address2'] . "', "."'" . $_POST['city'] . "',
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0"."'" . $_POST['state'] . "', "."'" . $_POST['zip'] . "',
> > "."'" . $phone . "',
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0"."'" . $_POST['cross_streets'] . "')";
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$=
result =3D mysql_query ($query); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 // Execute the query.
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0m=
ysql_close($conn); =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // Close the database connec=
tion.
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0e=
xit;
> > =A0 =A0 =A0 =A0 =A0 =A0}
> > =A0 =A0 =A0 =A0 =A0 =A0elseif ($appt_date == $rDate) =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 //Check for conflicting=

> > appointment date
> > =A0 =A0 =A0 =A0 =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0echo "We're sorry that appointmen=
t date is unavailable, please
> > select another date.";
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0user_form_resubmit();
> > =A0 =A0 =A0 =A0 =A0 =A0}
> > =A0 =A0 =A0 =A0 =A0 =A0elseif ($appt_date == $rTime) =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 //Check for conflicting=
appoitment
> > time
> > =A0 =A0 =A0 =A0 =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0echo "We're sorry that appointmen=
t time is unavailable, please
> > select another time.";
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0user_form_resubmit();
> > =A0 =A0 =A0 =A0 =A0 =A0}
> > =A0 =A0 =A0 =A0 =A0 =A0elseif ($appt_date == $rDate || =A0$appt_time=
== $rTime) =A0 =A0 =A0 =A0 =A0//Check for
> > conflicting date and time
> > =A0 =A0 =A0 =A0 =A0 =A0{
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0echo "

We're =
sorry that appointment date and time
> > are already scheduled, please select another date and time.

";
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0user_form_resubmit();
> > =A0 =A0 =A0 =A0 =A0 =A0}
> > =A0 =A0}
> > }
>
> > ------------------------------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.
>
> =A0 =A0 =A0 =A0 $rDate =3D $row['appt_date'];
> =A0 =A0 =A0 =A0 $rTime =3D $row['appt_time'];
>
> You need to call mysql_fetch_array() BEFORE using these to retrieve the
> data returned.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.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