Tim
Tim

Reputation: 1

Form to add data to MySQL table

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">

Upvotes: 0

Views: 1089

Answers (4)

Khawer Zeshan
Khawer Zeshan

Reputation: 9646

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

Upvotes: 0

Jules
Jules

Reputation: 7223

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

Upvotes: 2

Yusuf ali
Yusuf ali

Reputation: 331

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.

Upvotes: 1

Ricardo
Ricardo

Reputation: 173

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.

Upvotes: 0

Related Questions