t3stin
August 3, 2011, 8:01am
1
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.
t3stin
August 3, 2011, 12:28pm
2
Done, not solved the problem though…any other ideas guys?
t3stin:
would that look like this: $fname = mysql_real_escape_string($_POST[‘fname’]);
yes
$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.
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?)
t3stin:
Do you know why they are empty? thats what i dont understand…
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?
t3stin
August 3, 2011, 8:56am
8
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?
Can you post the complete scripts of the form page, and of add.php?
t3stin
August 3, 2011, 9:16am
12
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" />
t3stin
August 3, 2011, 10:05am
14
unfortunately the same problem
Weird. I don’t know.
Do you have a link to it?
t3stin
August 3, 2011, 10:18am
16
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
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?
t3stin
August 3, 2011, 1:06pm
19
my page code is exactly as shown
Try changing the form method in get, and do a print_r($_GET); in add.php