0

I'm using:

*connection details*

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

$sql = "INSERT INTO mytable (id, fname, surname, address)
VALUES ('', '$fname', '$surname', '$address')";
$results = mysql_query($sql);

if ($results)
{
echo "Details added.";
}

the problem is an entry is added to the table but all the data is blank instead of the stuff from the form?

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" >
<input type="submit" name="submitButtonName" value="Add">
4
  • $results = mysql_query($sql); if ($results) { echo "Details added."; } Commented Aug 2, 2011 at 16:01
  • Do the _POST values actually contain any data? Commented Aug 2, 2011 at 16:03
  • 1
    can you post your html form for me? Have you done a var_dump($_POST) to make sure that the values are reaching the page as expected? Commented Aug 2, 2011 at 16:03
  • form has been edited in. Commented Aug 2, 2011 at 16:10

4 Answers 4

2

You should post your HTML code too. Make sure the form's method is POST as:

<form method="POST" action="process.php">
   <input type="text" name="fname" />
   <input type="text" name="surname" />
   <input type="text" name="address" />
</form>

Also the name attributes of your <input /> elements should be correctly set. If this is all done you should be able to get the $_POST variables in the process.php page. Thus:

$fname = stripslashes($_POST['fname']);

Check if your variables have a value by doing a simple echo $fname;. If they are set, a possible reason of the failing system may be your SQL query. Leave out the id and just do:

$sql = "INSERT INTO mytable (firstname, surname, address)
        VALUES ('" . $fname . "', '" . $surname . "', '" . $address . "')";
$result = mysql_query($sql);
Sign up to request clarification or add additional context in comments.

Comments

1

You should try print this variables for debug.

echo $fname = stripslashes($_POST['fname']);
echo $surname = stripslashes($_POST['surname']);
echo $address = stripslashes($_POST['address']);

And you will see variables' data.

1 Comment

echo $fname = stripslashes($_POST['fname']); print nothing (as a status for variables assignment)
0

At a guess, I would imaging that this is something small, like not having the "name" value set for your fields. If you dont have "name" set, then the post data will not contain any details. If that's not the problem, please post your code and I can see if I spot anything.

Comments

0

If the id field in the data base table is auto incremented than there is no need to put that in mysql query , try this

    $sql = "INSERT INTO mytable (fname, surname, address)
            VALUES ('$fname', '$surname', '$address')";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.