Jump to content

Anything missing?


dreampho

Recommended Posts

Hi. I am new to PHP and trying out database connections. I am trying to query the database, to see if the date matches a record in the field 'date'. If it does echo yes, if there are no matches 'no'.

 

Can anyone tell me what is missing from my code:

 

<?php

$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("date_picker", $con);

$date= $_POST['date'];

$new_date = date('Y-m-d',strtotime($date));

$sql = 'SELECT date FROM availability'

if (date = $new_date) echo "Yes";

else echo "No";
   
mysql_close($con)

?>

Link to comment
https://forums.phpfreaks.com/topic/258103-anything-missing/
Share on other sites

Well you've written the query for the table but you haven't actually executed it.. really the query will do nothing you want it to because you havent asked it to compare the date field with the date 'data' you have. Below!

 

<?php
$sql = "SELECT `date` FROM `availability` WHERE `date` = '".$date."'";
$get = mysql_query($sql);
$pull = mysql_fetch_assoc($get);

if($pull['date'] == $new_date){

    echo 'Yes.';

} else {

    echo 'No.';

} 
?>

 

OR!

 

<?php
$sql = "SELECT `date` FROM `availability`";
$get = mysql_query($sql);
$pull = mysql_fetch_assoc($get);

while($pull = mysql_fetch_assoc($get)){

    if($pull['date'] == $new_date){

        echo 'Yes.';

    } else {

        echo 'No.';

    } 

}
?>

 

The first will search for records that match the date and then go through the IF you wanted the second will run through ALL database records and then pair up the dates using the IF.

 

Whenever you want to query a database you need to remember to actually execute it, its all well and good writing what you want to do (properly!) but if you don't then attempt to run the query it was a pointless effort: Write -> Execute (Or Error) -> Fetch results.

Link to comment
https://forums.phpfreaks.com/topic/258103-anything-missing/#findComment-1323061
Share on other sites

also a word of advice : get the list of RESERVED WORDS that that your RDB uses and then don't use them for column, table or database names.  Date is a reserved word, that's why Anon used backticks around it (well to be honest it looks as though Anon just wraps everything-and-its-dog in backticks whether it needs it or not, but that's besides the point) , this tells MySQL not to try to do anything with it. Also, as an FYI, Backticks are not single quotes and are not processed in the same way.

Link to comment
https://forums.phpfreaks.com/topic/258103-anything-missing/#findComment-1323064
Share on other sites

Thank you to both of you.

 

How would I redirect to a certain page for the if or else statements?

 

Thanks

 

Using the header function...

 

<?php 

if($pull['date'] == $new_date){

    header("Location: http://www.example.com");

} else {

    header("Location: http://www.example2.com");

} 
?>

 

Just make sure where you are redirecting to is valid of course and you should be ok.

Link to comment
https://forums.phpfreaks.com/topic/258103-anything-missing/#findComment-1323071
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.