By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,954 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,954 IT Pros & Developers. It's quick & easy.

Retrieve data from database and set to a variable

P: n/a
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<br>\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 "<h3 align='center'>We're sorry that appointment date and time
are already scheduled, please select another date and time.</h3>";
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
Jun 2 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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<br>\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 "<h3 align='center'>We're sorry that appointment date and time
are already scheduled, please select another date and time.</h3>";
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.
js*******@attglobal.net
==================

Jun 2 '08 #2

P: n/a
On Apr 19, 8:32*pm, Jerry Stuckle <jstuck...@attglobal.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("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<br>\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 conflictingappoitment
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 "<h3 align='center'>We're sorry that appointment date and time
are already scheduled, please select another date and time.</h3>";
* * * * * * * * * *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.
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
Jun 2 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.