I've tried everything that I know of, to make the fields required.
The form is quite simple and small.

Here is the visual form code:

<form action="insert.php" method="post">
<table cellpadding="2" border="0">
  <tr>
    <td>Server Name:</td>
    <td>IP:</td>
  </tr>
  <tr>
    <td>
      <input name="Name" type="text" />
   </td>
   <td>
      <input name="IP" type="text" />
   </td>
  </tr>
  <tr>
  <td colspan="2"><?php
require_once('recaptchalib.php');
$publickey = "my public key"; 
echo recaptcha_get_html($publickey);
?>
  </td>
  </tr>
  <tr>
   <td colspan="2">
	    <input type="submit" />
   </td>
  </tr>
</table>
</form>

Here is insert.php:

<?php
$con = mysql_connect("host","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("tablename", $con);

$sql="INSERT INTO servers (Name, IP)
VALUES
('$_POST[Name]','$_POST[IP]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added.";

mysql_close($con)
?>

All the database details have been replaced.

Dani AI

Generated

A few focused points to solve this quickly and make the form robust.

The immediate problem is a name/case mismatch plus an inappropriate emptiness check. The form inputs are submitted under the names you set in the HTML; PHP array keys are case‑sensitive, so reading $_POST['NAME'] while the field is named Name yields no value. Also, isset() only tells you the variable exists (it returns true for an empty string), so it won’t catch a blank field. this is why your script always falls into the “missing name” path.

Server‑side checks that are simple and reliable (example):

$name = isset($_POST['Name']) ? trim($_POST['Name']) : '';
$ip   = isset($_POST['IP'])   ? trim($_POST['IP'])   : '';

if ($name === '') {
  // name missing
}

if ($ip === '' || !filter_var($ip, FILTER_VALIDATE_IP)) {
  // IP missing or invalid
}

Also add client-side niceties (but not replacements) — the HTML5 required attribute stops many bad submissions early. Never rely on client checks alone: always validate and sanitize on the server.

When inserting, stop using the old mysql_* functions. Use prepared statements (PDO or mysqli) to avoid SQL injection, e.g. prepare an INSERT with bound parameters and execute with the validated values.

Quick debug checklist:

  • Confirm form has method="post" and each input has the exact name you read in PHP (case matters).
  • Print var_export($_POST, true) to inspect what actually arrives.
  • Turn on error reporting while debugging.
  • Ensure reCAPTCHA is validated server‑side before inserting.

was right about client/server checks; was on the right track — just adjust the name/key casing and switch to an emptiness test like the one above and use prepared statements for the DB.

Recommended Answers

All 4 Replies

What do you mean by required? You can check your input fields with Javascript and/or PHP and act accordingly.

Make them a mandatory field. Im not sure how to go about doing that.

Insert.php

<?php
$Name = $_POST['NAME'];
$IP = $_POST['IP'];

if (!isset($Name)) {
echo "You must enter a name.";
exit;
}
elseif (!isset($IP)) {
echo "You must enter an IP address.";
exit;
}
else {
 // code for database insertion and printing success message.
}
?>

Now this is overly simplified. But basically it is saying that if there has been no value set for the Name box, then print out an error message about that box and quit running. If the IP box is empty then it gives an error message about the IP box being empty and it will quit. If it passes both of those tests then it will interact with the DB.

But some more checks should be run in addition. You can go here: for examples of how to add another elseif statement that will check that what is sent in the IP box is actually a possible IP address. It will make sure that someone doesn't enter "frank" or "2nh4ie8" in there. It won't necessarily check that it is the true IP address but it will at least ensure that it is a a true possible IP address.

You'll want to validate AND filter/sanitize every bit of form input sent by a user possible before adding it to the database and/or printing them out to the browser.

I have inserted the code, but every time I submit it just says, "You must enter a name."

<?php
$con = mysql_connect("host","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("tablename", $con);

$Name = $_POST['NAME'];
$IP = $_POST['IP'];

if (!isset($Name)) {
echo "You must enter a name.";
exit;
}
elseif (!isset($IP)) {
echo "You must enter an IP address.";
exit;
}

$sql="INSERT INTO servers (Name, IP)
VALUES
('$_POST[Name]','$_POST[IP]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added.";

mysql_close($con)
?>
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.