Jeff Pang Jeff Pang - 1 month ago 11
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

192.168.0.168 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">
</form>


and this is the
mode.php
coding:

<?php

$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");

mysql_select_db('home_mode');

$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

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.

Comments