0

Hi guys,

I'm a bit of a newbie to PHP/MySQL, so please excuse me if I seem ignorant of something seemingly obvious. I'm writing a booking system for a holiday home rental company. I'm building it up slowly. What I have so far is trying to calculate the number of nights that a person will stay based on the date that they arrive and depart.

This is what I have so far: -

<html>

<head><title>Booking Form</title></head>

<body>

<form action="calculate.php" method="post">

Date of arrival: <input type="text" name="arrival">
Date of departure: <input type="text" name="departure">

<input type="Submit" value="Submit"><input type="Reset" value="Reset">
</form>

</body>

</html>

The code above is for the user to insert the dates of arrival and departure.

<?php 
 // Connects to your Database 
 mysql_connect("localhost", "", "") or die(mysql_error()); 
 mysql_select_db("dissertation") or die(mysql_error());
 
 $sql="SELECT datediff('$departure','$arrival')" or die(mysql_error());
$result = mysql_query($sql);
		$row = mysql_fetch_row($result);

$insert = "INSERT INTO booking (arrival, nights, departure)
 			VALUES ('".$_POST['arrival']."', '$result', '".$_POST['departure']."')";
 	$holiday = mysql_query($insert);

?>

Using the code above, I want to work out the number of nights a person is staying for, and enter that with the dates of arrival and departure into the database. Currently, when I run the code, I get 'Resource id #3' in the 'nights' column of the table. Doing a search on Google would imply that I'm missing something, but i can't see what.

Below is the code for my table. The reason for such a big 'nights' column is so I can see the full error that I'm getting currently.

create table booking (
id INT primary key auto_increment,
arrival date NOT NULL,
nights varchar(100) NOT NULL,
departure date NOT NULL
);

Thanks in advance for any tips! :)

Edited by mikeypc2010: Noticed a $ extra in my query, but removing hasn't solved the problem

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by soldierflup
0

You are trying to insert $result into booking but after a quick look at your code that should be $row, if the previous select only gives one result.

0

Solved it! :) The problem was I wasn't passing the variables I was entering into the form to the select datediff query so the query wasn't generating a value. Add $_POST to the query and a few other tweaks and it works! :)

Thanks :)

0

The variable $result is just giving an identifier (the resource id) to use the result of the query later on.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.