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! :)

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.

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 :)

Member Avatar for soldierflup

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.