Form data into MySQL table

I have tried multiple ways of getting my data into the table but this is the most current one im using. php to process the form:

*Connection details*
$fname = stripslashes($_POST['fname']);
$surname = stripslashes($_POST['surname']);
$address = stripslashes($_POST['address']);

$sql = "INSERT INTO mytable (fname, surname, address)
VALUES ('" . $fname . "', '" . $surname . "', '" . $address . "')"
$results = mysql_query($sql);
if ($results)
{
echo "Details added.";
}
else
{
echo "Details not added.";	
}

The problem is no matter what i have tried i just get empty records added and the echoed “Details added.” - not any data that was in the form and i cannot understand why.

html for the form:

<form id="myform" action="add.php" method="post" name="myform">
<label for="fname">First Name</label> 
<input id="fname" name="fname" ><br />
<label for="surname">Surname</label> 
<input id="surname" name="surname" ><br />
<label for="address">Address</label> 
<input id="address" name="address" ><br />
<input type="submit" name="submitButtonName" value="Add">
</form>

what am i missing!? its driving me insane.
Thanks in advance.

Done, not solved the problem though…any other ideas guys?

  1. use mysql_real_escape_string() to sanitize the user input, not stripslashes.
  2. do an echo of $sql, so you can see what the query actually looks like
  3. do you check if the form has been submitted and the values are valid before you store them in the database?

sorry im new to php so:

  1. would that look like this: $fname = mysql_real_escape_string($_POST[‘fname’]);
  2. $sql echo: INSERT INTO mytable (fname, surname, address) VALUES (‘’, ‘’, ‘’)
  3. no validation has been put in place as yet - how do i check the form is submitted?

thank you

yes

  1. $sql echo: INSERT INTO mytable (fname, surname, address) VALUES (‘’, ‘’, ‘’)

See, the values are empty, that’s why it’s inserting empty rows in the database.

  1. no validation has been put in place as yet - how do i check the form is submitted?

Just check if the three form fields exist and if they have a value (I suppose you only want to insert if all three fields have a value?)

  1. Done
  2. Do you know why they are empty? thats what i dont understand…
  3. validation right now im not worried about i just want to be able to receive the values to begin with then build on it (but it will be implemented at some point).

No. Do a print_r($_POST) at the top of the script. Let’s see what you’re sending to the add.php script.

By the way, is the form code also in add.php, or is it another file?

that gives me: Array ( )
the form code is in a different file (below a table showing the data from the MySQL table but not within the php script for that)

So you inserted data in the form fields, clicked the submit button, and then the add.php script displays an empty $_POST array?

correct

Can you post the complete scripts of the form page, and of add.php?

sure, thanks for your time by the way, much appreciated.

add.php:

print_r($_POST);

*Connection details*
$fname = stripslashes($_POST['fname']);
$surname = stripslashes($_POST['surname']);
$address = stripslashes($_POST['address']);

$sql = "INSERT INTO mytable (fname, surname, address)
VALUES ('" . $fname . "', '" . $surname . "', '" . $address . "')"
$results = mysql_query($sql);
if ($results)
{
echo "Details added.";
echo "$sql";
}
else
{
echo "Details not added.";	
}

form page:

<table style="color: #333; line-height: 16px;" cellpadding="3">
<tr style="font-weight: bold;"><th>First Name</th><th>Surname</th><th>Address</th></tr></thead>
<?PHP
*connection details*

$youthquery = "SELECT * FROM mytable ORDER BY id ASC";
$youthresult = mysql_query($youthquery);
while($row = mysql_fetch_array($youthresult)){
echo "</td><td>";
echo $row['fname'];
echo "</td><td>";
echo $row['surname'];
echo "</td><td>";
echo $row['address'];
echo "</td></tr>";
}
?>
</table>

<h1>Add Record</h1>
<form id="myform" action="add.php" method="post" name="myform">
<label for="fname">First Name</label> 
<input id="fname" name="fname" ><br />
<label for="surname">Surname</label> 
<input id="surname" name="surname" ><br />
<label for="address">Address</label> 
<input id="address" name="address" ><br />
<input type="submit" name="submitButtonName" value="Add">
</form>

It’s a long shot, but try closing the input tags?

<input id="fname" name="fname" />

unfortunately the same problem :frowning:

Weird. I don’t know.
Do you have a link to it?

not currently its just local…guess i’m out of luck!

Try giving your inputs a TYPE. Just as long of a shot, but it cant hurt to be standards compliant :wink:

I’ve just stuck your code into my localhost and run it. It works fine. You havent done something silly like stick <form> into one of your inputs, have you?

my page code is exactly as shown :frowning:

Try changing the form method in get, and do a print_r($_GET); in add.php