Won't Insert to Database

Good Morning,

I am using the following code, and no matter what I change, nothing will insert into the database. Can you guys point me in the right direction to get it fixed?



if(isset($_POST['submit'])) {
$hostname = $_POST['hostname'];
$ip = $_POST['ip'];
$port = $_POST['port'];
$notes = $_POST['notes'];

$query = mysql_query("INSERT INTO ip (id, hostname, ip, switchport, notes) VALUES ('NULL', '$hostname', '$ip', '$port', '$notes')");
$result = ($query);

	echo 'Success!';
} else {
	echo 'Error!';


Are you certain that your database connection has been established? (Presumably in config.php)

This line makes no sense: $result = ($query); :wink:

Try this:

$query = "INSERT INTO ip (id, hostname, ip, switchport, notes) VALUES ('NULL', '$hostname', '$ip', '$port', '$notes')";
$result = mysql_query($query) or die ("you've got an error " . mysql_error() . " in query $query"); 

And see if you get some error.

I tried the code you gave me, and the page just refreshes but nothing inserts into the database. Could that be my connection in config.php?

Do you get one of the two messages from your code? “Success!” or “Error!” ?

I do not get any messages at all. I put that in there to try to troubleshoot, but I never get it.

Ok, so the problem is this line:

if(isset($_POST['submit'])) { 

because the script never enters the if (you would have gotten one of the two messages if the if was entered).

It’s never a good idea to check for the button name, because in some cases (IE if you press enter if I remember correctly) the button value isn’t sent.
Try checking for one of the form fields instead, $_POST[‘hostname’] for example.

And two by the ways:

  1. if you’re learning PHP and MySQL right now, you should not use mysql_ to access the MySQL database, because that PHP extension is deprecated. Study mysqli_ and or PDO instead.
  2. never use user input ($_POST, $GET) in queries without sanitizing and validating.
    The sanitizing part will be done by mysqli
    or PDO if you use prepared statements.
    The validating you’ll have to do in your PHP code. For example, in your form, if I click Submit without filling in any field, you’re going to insert an empty row in the database. I’m sure you’ll want to check that all form fields contain a value before doing the insert?

I appreciate your response, and the indepthness, I definitely learned a couple different things from you.

The main problem I had was I didn’t have a name on the submit button, so like you said, it was never entering the if.

On another note, I am currently looking into mysqli, as I think it will be better for me with learning it all.

I appreciate your response… I can’t thank you enough!

hope this code helps you:

// Check connection
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();

$sql=“INSERT INTO Persons (ip, port, notes)

if (!mysqli_query($con,$sql))
die('Error: ’ . mysqli_error($con));
echo “1 record added”;



<form action=“insert.php” method=“post”>
ip: <input type=“text” name=“ip”>
port: <input type=“text” name=“port”>
notes: <input type=“text” name=“notes”>
<input type=“submit”>