couple of questions

I am working on my first php website I thought it would be a simple one to do and I seem to be wrong hoping you can help me out with my 2 main problems with the site

question 1. Although I am using a html form I am not sure of the correct code to store the info into the database in the way I want info entered.

What I am trying to do here is add a date to mysql but I would like to use dropdown menu to do so I know how to make the dropdowns however I am not sure of the php code to combine the year, month and day so that it can be entered in one date field in Mysql database

Can someone help me and explain how this is done?

The second question also revolving around that I want to display info queried from database however users of my web site may put in a date that is between two dates. i.e. I have date field 1 and date field 2 the user looks for something on Aug 16, 2010 however in the database there is something that date field 1 has as aug 12, 2010 and date field 2 has as aug 21, 2010 ----this should show up in query results now if the user puts in aug 22, 2010 it returns empty because nothing is between the two dates.

How can i do this? I am assuming its the <= and the >= command but I want to make sure.

for the date create a new var to hold the date in then pass in the 3 date items like this:

[php]$date = $year.’-’.$month.’-’.$day;[/php]

for searching between dates yes your right it’s a case of using >= and <= like this:

[php]mysql_query(“SELECT something FROM table WHERE orderDate >= ‘$startDate’ AND orderDate <= ‘$endDate’”);[/php]

ok so if i am understanding this right my form should look like this

 

Jan Feb Mar Apr May June July Aug Sept Oct Nov Dec <\SELECT>   01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <\SELECT> 2010 2011 2012 2013 2014 2015 <\SELECT>

now where would i put the code for the $orderdate = seryear/sermonth/serdate

Ok since I am not sure on how my form above to work for now I am oging with a textbox to enter date however when I search a date that I know is in my database i am getting this error here

Parse error: syntax error, unexpected $end in C:\server\xampp\htdocs\fair123\searchdays.php on line [b]56

the PHP code i am using is
[/b]

<?php $username="myusername"; $password="mypass"; $database="mydatabase";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$query=“SELECT * FROM events WHERE orderDate >= ‘$fairopen’ AND orderDate <= ‘$fairclose’”;
$result=mysql_query($query);

if (mysql_num_rows($result) == 0) // Exits if your db table is empty
{
echo ‘I have no rows to display’;
exit;

$num=mysql_numrows($result);

mysql_close();
?>

<?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"fairname"); $f2=mysql_result($result,$i,"fairadd"); $f3=mysql_result($result,$i,"faircity"); $f4=mysql_result($result,$i,"faircty"); $f5=mysql_result($result,$i,"fairstate"); ?> Fair Name <?php echo $f1; ?>
<?php echo $f2; ?>
<?php echo $f3; ?>
<?php echo $f4; ?>
<?php echo $f5; ?>
<?php $i++; } ?> So what am I doing wrong here?
Value1 Value2 Value3 Value4 Value5

the reason for the error is you opened an if statment but did not close it:

if (mysql_num_rows($result) == 0) // Exits if your db table is empty { echo 'I have no rows to display'; exit; }

also you forgot a _ in your mysql_num_rows:

$num=mysql_num_rows($result);

The reason the search is not working is $fairopen and $fairclose is not anywhere in the script where does this value come from?

$result = mysql_query("SELECT * FROM events WHERE orderDate >= '$fairopen' AND orderDate <= '$fairclose'");

what I would do to make things a little easier for yourself instead of manually putting in the the dates use ranges in php then loop through each range for the dates for instance rather then typing out 1 - 31 for the days for can create a range:

[php]$days = range (1, 31);[/php]

then loop through the range to make a drop down list of days:

[php]

Day:
<?php
foreach ($days as $value) {
echo ‘’.$value.’\n’;
}
?>

[/php]

Here’s a sample form:

[php]<?php
$months = array (1 => ‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’,‘Jul’, ‘Aug’, ‘Sep’, ‘Oct’, ‘Nov’, ‘Dec’);
$days = range (1, 31);
$years = range (2008, 2015);
?>

<p>Month:<select name="month">
<?php      
foreach ($months as $value) {
  echo '<option value="'.$value.'">'.$value.'</option>\n';
}
?>
</select></p>

<p>Day:<select name="day">
<?php      
foreach ($days as $value) {
  echo '<option value="'.$value.'">'.$value.'</option>\n';
}
?>
</select></p>

<p>Year:<select name="year">
<?php      
  foreach ($years as $value) {
      echo '<option value="'.$value.'">'.$value.'</option>\n';
}
?>
</select></p>
                
<input type="submit" name="submit" value="submit" />
[/php]

The reason the search is not working is $fairopen and $fairclose is not anywhere in the script where does this value come from?

$result = mysql_query("SELECT * FROM events WHERE orderDate >= '$fairopen' AND orderDate <= '$fairclose'");

$fairopen and $fairclose are the field names that I am actually using in my database

I made the other changes that you said however now I am getting this error
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\server\xampp\htdocs\fair123\searchdays.php on line 13
I have no rows to display

are in that case, you need to format your query like this:

[php]$query=“SELECT * FROM events WHERE fairopen >= ‘$date’ AND fairclose <= ‘$date’”;[/php]

the $date would contain a date value, either being passed from a form or hand coded either way it needs to come from somewhere otherwise they will never be a date to use in the search.

I still must be doing something wrong I’ve changed the form to the form you gave and corrected the code everything seems ok however when I search the dates all i get is a page that says I have no rows to display

[left] :frowning:

There has to be something I am missing I know for a fact I have one listing it has the following attributes assigned fieldname - attribute

fairidnum - 1
fairname - San Diego County fair
fairadd -2260 Jimmy Durante Blvd
faircity - Del Mar
faircty - San diego
fairstate - CA
fairctry - USA
fairopen - 2010-06-14
fairclose - 2010-07-05
fairmail - them @ theiremail (used actual email)
fairweb - their website

so with fairopen of 2010-06-14 and fair close of 2010-07-05 a search of 2010-06-20 should return a result right?

[/left]

yes that’s right but is the date being passed to the search?

can you post your scripts so far

ok this is from date.php which is my form

[code]



Search by date
                      </h2>  </h2>
<?php $months = array (1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); $days = range (1, 31); $years = range (2008, 2015); ?>
<p>Month:<select name="month">
<?php      
foreach ($months as $value) {
  echo '<option value="'.$value.'">'.$value.'</option>\n';
}
?>
</select></p>

<p>Day:<select name="day">
<?php      
foreach ($days as $value) {
  echo '<option value="'.$value.'">'.$value.'</option>\n';
}
?>
</select></p>

<p>Year:<select name="year">
<?php      
  foreach ($years as $value) {
      echo '<option value="'.$value.'">'.$value.'</option>\n';
}



?>
</select></p>
                
<input type="submit" name="submit" value="submit" />

 

[/code]

and the form searchdays.php has this

[code]

<?php $username="myusername"; $password="mypassword"; $database="mydatabase"; $date = $year.'-'.$month.'-'.$day;

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$query=“SELECT * FROM events WHERE fairopen >= ‘$date’ AND fairclose <= ‘$date’”;
$result=mysql_query($query);

if (mysql_num_rows($result) == 0) // Exits if your db table is empty
{
echo ‘I have no rows to display’;
exit;
}

$num=mysql_num_rows($result);

mysql_close();
?>

<?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"fairname"); $f2=mysql_result($result,$i,"fairadd"); $f3=mysql_result($result,$i,"faircity"); $f4=mysql_result($result,$i,"faircty"); $f5=mysql_result($result,$i,"fairstate"); ?> Fair Name <?php echo $f1; ?>
<?php echo $f2; ?>
<?php echo $f3; ?>
<?php echo $f4; ?>
<?php echo $f5; ?>
<?php $i++; } ?> [/code]

i think it’s not getting the varriables i’m wording if i need to use an isset which is what i am researching now since i’m not that faniliar with it or any php for that matter I didn’t think this site would be this hard to do boy was I wrong :frowning:

Value1 Value2 Value3 Value4 Value5

are I can see what’s going on. when using forms to collect data you speciafy how the information will be sent either post or get in the form method

Since your using post to access the information in searchdays.php you need to use $_POST followed by the name of the item that was created by the form.

For instance the select drop down for days has a name of days this is the name you use to get the day selected:

[php]$_POST’day’][/php]

Then create a variable for each item:

[php]$day = $_POST[‘day’];
$month = $_POST[‘month’];
$year = $_POST[‘year’];

$date = $year.’-’.$month.’-’.$day;[/php]

Here’s your form with the post data added:

[php]

<?php $username="myusername"; $password="mypassword"; $database="mydatabase"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database");

$day = $_POST[‘day’];
$month = $_POST[‘month’];
$year = $_POST[‘year’];

$date = $year.’-’.$month.’-’.$day;

$query=“SELECT * FROM events WHERE fairopen >= ‘$date’ AND fairclose <= ‘$date’”;
$result=mysql_query($query);

if (mysql_num_rows($result) == 0) // Exits if your db table is empty
{
echo ‘I have no rows to display’;
exit;
}

$num=mysql_num_rows($result);

mysql_close();
?>

<?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"fairname"); $f2=mysql_result($result,$i,"fairadd"); $f3=mysql_result($result,$i,"faircity"); $f4=mysql_result($result,$i,"faircty"); $f5=mysql_result($result,$i,"fairstate"); ?> Fair Name <?php echo $f1; ?>
<?php echo $f2; ?>
<?php echo $f3; ?>
<?php echo $f4; ?>
<?php echo $f5; ?>
<?php $i++; } ?> [/php]
Value1 Value2 Value3 Value4 Value5

ok i copied the info you gave me above however I am still coming up with the same thing so now the only thing I haven’t done is in forms i haven’t used $_POST['day']

Should i add that to the form as well?

I really appreciate your helping me with this
and am sorry if I am being a pain

that’s alight these things take time to make sense.

you don’t need to add $_POST[‘day’] to the form, what happens is for every element of the form, each will have a name, the name is used by php to get the entered date so:

<input type="text" name="email" value="">

becomes $_POST[‘email’] when getting the information using php.

If your not getting anything that would suggest a problem with query.

okay in searchdays.php put the following at the top of the file:

[php]<?php
echo “

”;
echo print_r($_POST);
echo “
”;
?>[/php]

then run the form again and post the results back

OK, i think I see the problem correct me if I am wrong though and I will await your response but when I did what you said it returned this

Array ( [month] => Jun [day] => 21 [year] => 2010 [submit] => submit ) 1 I have no rows to display

What I am thinking it is is the fact that month was returned as Jun and not as the numerical value 06 which I know would be in the form area but how to correct that I am not sure since now doing it as php

yes your right it needs to be a numeric date, this is simple to change in the form replace the month drop down with this:

[php]

Month:
<?php
$i = 1;
foreach ($months as $value) {
echo ‘’.$value.’\n’;
$i++;}
?>

[/php]

I’ve created a variable called $i given it an inital value of 1 and increment it on each loop and using that in the option tag so when it’s passed to the next page the numeric number is passed.

ok this did not resolve my problem so. This gave me the following display

Array
(
    [month] => 6
    [day] => 20
    [year] => 2010
    [submit] => submit
)
1      I have no rows to display

my showall.php file shows the following for all fields except idnum
Fair Name San Diego County Fair Address 2260 Jimmy Durante Blvd CityDel Mar CountySan Diego StateCalifornia CountryUSA Opens on 2010-06-14 Closes on2010-07-05 [email protected] websitehttp://www.sdfair.com

so I know all the fields are displaying ok. The only thing I can think of now is in the comparrison field but I don’t know what the problem can be

[/code]

been doing some tests in phpmyadmin and realised the query was wrong you want to search for matches that are less then the current date for the opening and less then or equal to in relation to the closing date.

here’s the updated query:

[php]SELECT * FROM events WHERE fairopen <= ‘$date’ AND fairclose <= ‘$date’[/php]

I have to admit I was just testing also and that is what I came up with as well that we had the <= and the >= reversed it works now :slight_smile:

Thank you for all your help and if I ever get this site off the ground I would like to give you the credit you deserve for helping me work out the bus since most of this is your code if you can pm any details you would like me to use it would be appreciated.
and actuall it s more then open date and less then close date the code I used that worked was

$query="SELECT * FROM events WHERE fairopen <= '$date' AND fairclose >= '$date'";

I guess there is one more question I know you can sort by a fieldname is there a way to sort three fields though? say in my case country then state then city so all are grouped together and in alphabetical order.

I believe you can pass in multiple fields for the sort by by comma separating them, if you use the group by command the results will be grouped.

I like to give back to the community as I’ve learned a lot this way, I don’t expect to be given credit on anyone’s work it’s a nice gesture but honestly there’s no need.

Sponsor our Newsletter | Privacy Policy | Terms of Service