PDA

View Full Version : Searching for exact date



revray
02-24-2008, 09:47 PM
Afternoon,

Ok I am ready to tear my hair out, of course if I had some it would help in that aspect.

I am building an application where the user will enter a date in the following format: yyyy-mm-dd.

Here is the form that the user is using to enter their information:


<body>
<div id="content">
<p>To see if Jeff is available for your special day, please use this form to input your wedding date.</p>


<form method="post" action="result.php">
<p>Enter your wedding date below to see if Jeff is available</p>
<p> Please note, you must enter your date in the following format yyyy-mm-dd. </p>
<p> <span id="sprytextfield1">
<label>Wedding Date:
<input type="text" name="date_search" id="date_search" />
</label>
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span>

&nbsp;&nbsp;
<input type="submit" value="Check Your Date"></p>
</form>
</div>
<script type="text/javascript">
<!--
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "date", {format:"yyyy-mm-dd", hint:"yyyy-mm-dd"});
//-->
</script>
</body>
</html>

The form actions result.php, which is listed below:


<?
$hostname = "server"; // Usually localhost.
$username = "username"; // If you have no username, leave this space empty.
$password = "password"; // The same applies here.
$usertable = "booking_dates"; // This is the table you made.
$dbName = "turnerbooking"; // This is the main database you connect to.
mysql_connect($hostname, $username, $password) OR DIE("Unable to connect to database");
mysql_select_db( "$dbName") or die( "Unable to select database");
$query = mysql_query("SELECT * FROM $usertable WHERE {$_POST['date_search']} = '$search' ");
while ($row = mysql_fetch_array($query))
{
$variable1=$row["date"];

print ("$variable1");
}

//below this is the function for no record!!
if (!$variable1)
{
echo "<p>Great news! Jeff is available. Please fill out the form below to contact Jeff about booking your date.<br> Please note, your date IS NOT booked yet, it simply is available. Make sure to fill out this form so Jeff can get back to you.</p>";

echo "<form action=\"http://formmail.dreamhost.com/cgi-bin/formmail.cgi\" method=\"POST\">";
echo "<input type=\"hidden\" name=\"recipient\" value=\"pray@wburgnaz.com\"><br>";
echo "<input type=\"hidden\" name=\"subject\" value=\"Wedding Booking Inquiry\"></p><br>";
echo "<p>Full Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type=\"text\" size=\"20\" name=\"name\" ></p><br>";
echo "<p>Phone Number: <input type=\"text\" size=\"20\" name=\"phone\"></p><br>";
echo "<p>Email Address: <input type=\"text\" size=\"20\" name=\"email\"></p><br>";
echo "<p>Wedding Date: <input type=\"text\" size=\"20\" name=\"weddate\"></p><br>";
echo "<p><input name=\"Submit\" type=\"submit\" value=\"Submit\" class=\"button\" /></p>";
echo "</form>";
}
//end
?>

Now, when I test this, no matter what date is entered, the search does not find the record. I have two rows in my database, the first being named date, which holds DATE info. The second being ID which is an autoincrement integer and is the primary key.

I enter a specific date into the date row, then search it with the form, and the application STILL says that the record is not found. Now if change the query to LIKE, it will list all the records.

I am obviously doing something wrong here, can anyone shed some light on the issue for me? I would be eternally in your debt.

Removed MySQL login info. Please do not leave your login information in any code snippet.

tech_support
02-25-2008, 02:12 AM
$query = mysql_query("SELECT * FROM $usertable WHERE {$_POST['date_search']} = '$search' ");
Where's $search?

Try something like:


$q = mysql_real_escape_string($_POST['date_search']);
$query = mysql_query("SELECT * FROM $usertable WHERE `column_name_here`= '$search' ");

....where column_name_here is the column you'd like to search.