Php code doesn't update MySql table

Hi Everyone,
This is the HTML page I created for input:


<html>
<body>
 <form action = "insert_persons.php" method="GET">
	startdate: <input type ="text" name="startDate">
	enddate: <input type ="text" name="endDate">
	<input type="submit">
</form>
</body>
</html>

This is insert_persons.php file that contains the code to insert data from HTML page into MySql table


<?php
 echo $_GET['startDate'];
 echo $_GET['endDate'];

  $con=mysqli_connect("localhost","root","pass","db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

 $sql="INSERT INTO persons (startDate, endDate, intervalTime)
VALUES
(
 '$_GET[startDate]',
 '$_GET[endDate]',
 TIMEDIFF('$_GET[endDate]','$_GET[startDate]')
 )";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";
?>

Here is the data I assigned in the HTML page:

startdate- 2013-07-02 11:00:00
enddate- 2013-07-02 14:45:00

When I press the “Submit” button something strange happens: On the screen I get the code of php file shown above.
The code doesn’t update the MySql table. It just appears on the screen.
Any idea what goes wrong?
Thanks

Elaborate on that sentence for me please. Are you seeing the actual code on the page? If so, then your web server isn’t setup properly and it isn’t executing your PHP script, it is just serving it up as a text file.

Assuming you have php 5.3, this should work.

<?php
	$con=mysqli_connect("localhost","root","pass","db");
	// Check connection
	if (mysqli_connect_errno())
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}
	if(isset($_GET['startDate'])){
		$intervalTime = TIMEDIFF("{$_GET['endDate']}","{$_GET['startDate']}");
		$sql="INSERT INTO persons (startDate, endDate, intervalTime)
		VALUES
		(
		'{$_GET['startDate']}',
		'{$_GET['endDate']}',
		'$intervalTime')
		)";
		
		if (!mysqli_query($con,$sql))
		{
		die('Error: ' . mysqli_error($con));
		}
		echo "1 record added";	
	}
?>

If less than php5.3 you’ll need to use strtotime() to get minutes.

<?php
	$con=mysqli_connect("localhost","root","pass","db");
	// Check connection
	if (mysqli_connect_errno())
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}
	
	if(isset($_GET['startDate'])){
		$datetime1 = strtotime($_GET['startDate']);
		$datetime2 = strtotime($_GET['endDate']);
		$interval  = abs($datetime2 - $datetime1);
		$intervalTime   = round($interval / 60);
		$sql="INSERT INTO persons (startDate, endDate, intervalTime)
		VALUES
		(
		'{$_GET['startDate']}',
		'{$_GET['endDate']}',
		'$intervalTime')
		)";
		
		if (!mysqli_query($con,$sql))
		{
		die('Error: ' . mysqli_error($con));
		}
		echo "1 record added";	
	}
?>

At least I think so. Not tested

Hi
Attached is screenshots of my servers.

Not sure what else you’ve got going on the page but it looks like you’ve already got a connection established. Try removing those lines.

As a minor aside, and not saying it will make any difference, but is there a reason you’re using GET instead of POST here?

Thanks,
Could the problem lay on Google chrome?
Here is a more simple code:


<?php
 echo "Hello world";
?>

From right click menu on “test.php” which is the above file’s name, I select “open with” and from windows explorer I select:
“Program files, Google, Application, Chrome.exe”.
What I get is the code :frowning:

This sounds like the root of your problem… PHP won’t execute if you load it into the browser directly from the file system, it has to be served by a webserver with PHP installed.

To run PHP on a windows machine you’ll need to install something like WampServer or [URL=“http://www.apachefriends.org/en/xampp.html”]XAMPP. Then you open your browser and goto the special url http://localhost/ which is usually the address of the server running on your machine.

And the root of my problem it was !
Inside the address bar of my Crome browser I typed: “http://localhost/addhours.php” and the php code initiated by the “Submit” button worked just fine !
Thanks to all of you !