-1

So I tried making a reservation system for a games library.

$timestamp = (strtotime($StartDate));
$EndDate = strtotime("+$Duration days",$timestamp);

This part of the code converts the date entered into a form into a timestamp The second line of the code calculates the end date timestamp for reserving the game for a certain number of days.

In my mind, how I would do it is by creating a variable and selecting certain values from the database. However, I am not that good at php, so I am not a 100% sure if I am right:

$reserved = mysql_query ("SELECT count(*) FROM reservations WHERE EndDate <= $EndDate AND StartDate >= $timestamp");
if (!$reserved) { /*do nothing*/ } ELSE { echo "That date has already been reserved"; }

What this should do is select the timestamps from the database which clash, and then I can display an error message if the variable $reserved is not empty. However, I can't get it to work. How do I display the error message properly?

M Bloxman
  • 23
  • 6
  • do a count(*) in your select or num_rows, then `if > 0` or `==1` display message, else, display something else. Google "if row exists mysql php". best I can offer without knowing which API you're using to connect with. – Funk Forty Niner Jun 20 '15 at 19:09
  • @Fred -ii- I am using mysql to connect mysql_connect("servername","username","password") or die(mysql_error()); – M Bloxman Jun 20 '15 at 19:46
  • @M Bloxman : Have you used `mysql_query()` function to execute the query? – AnkiiG Jun 20 '15 at 19:58
  • Your “example code” suggests that you don’t know the very basics of working with MySQL in PHP – so please go read some beginner’s tutorials on that. SO is not the place to teach you the basics. – CBroe Jun 20 '15 at 20:09
  • Also note that all `mysql_` functions are deprecated in PHP 5 and will be removed in PHP 7 (to be released later this year). Use PDO or mysqli instead. – Arjan Jun 20 '15 at 20:28
  • @CBroe I know the basics, but I am only a beginner, so I may have missed a few things. What I mean is that I may not be able to write code properly, but I can understand php coding easily, and can identify what I have done wrong by looking at answers that people have given me. And so can other people. – M Bloxman Jun 20 '15 at 20:29

1 Answers1

0

You should use as below :

    $result = mysql_query("Select * from reservations where StartDate >= '".$timestamp."' AND  EndDate <= '".$EndDate."'");
    $num_rows = mysql_num_rows($result);
    if($num_rows==0)
    { 
       //Your Process 
    } 
    else 
    { echo "That date has already been reserved"; }
AnkiiG
  • 3,468
  • 1
  • 17
  • 28
  • You could re-phase the if statement, so you don't have a "do nothing" clause (and also you accidentally commented out a `}`) -> `if($num_rows!==0) { echo "That date has already been reserved"; }` – Alex Szabo Jun 20 '15 at 20:18
  • If you're only interested in the number of rows a query returns, you should not return all rows but instead let MySQL do the counting for you. – Arjan Jun 20 '15 at 20:26
  • Thank you very much. I realised I had made a few silly mistakes in my code. For example I had forgotten the 'mysql_query' part. The '$num_rows' function had completely slipped my mind, so thank you for clarifying that. – M Bloxman Jun 20 '15 at 20:27
  • @M Bloxman : That's great, it helped you.. – AnkiiG Jun 20 '15 at 20:28