Jeff Pang Jeff Pang - 1 year ago 68
MySQL Question

Page isn't working when insert data to mysql with PHP

Good day. I have a simple html file with 2 button, when button clicked, it will input some data to database. Unfortunately, when the button is clicked, chrome displays is currently unable to handle this request.HTTP ERROR 500

The following is my html code snippet

<form action="mode.php" method="POST">
<input style="background-color: green;" id="home" name="home" type="submit" value="HOME"></br>
<input style="background-color: red;" id="away" name="away" type="submit" value="AWAY">

and this is the


$dbhost = 'localhost:3036';
$dbuser = 'xxx';
$dbpass = 'abc1234';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
die('Could not connect: ' . mysql_error());
if (isset($_POST['home'])) {
$sql= insert into home_mode (id, status) value (null, "HOME");


$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not enter data: ' . mysql_error());

echo "Mode changed to HOME";}

*I have tested the connection to MySQL with a test script and it's successful.
Please assist, thank you!

Answer Source

You're trying to insert NULL on auto increment column. Remove NULL from INSERT query and try like this:

$sql= insert into home_mode (status) value ("HOME");

Also you really should consider and change old, deprecated mysql_* to mysqli_* or PDO.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download