I can't figure out how to add a record to MySQL from a PHP form. Here is the form: <form method="post" action="answer.php" /> First Name Last Name<br> <input type="text" name="fname" /><input type="text" name="lname" /><br/><br> City State<br> <input type="text" name="city" /><select name="state" /> <option>Alabama</option> <option>Wyoming</option> </select><br/><br> E-mail Address<br> <input type="text" name="email" /><br/><br> Answer: <input type="radio" name="answer" value="A" /> A<input type="radio" name="answer" value="B" /> B<input type="radio" name="answer" value="C" /> C<input type="radio" name="answer" value="D" /> D<br/> <input type="checkbox" name="remember" value="1" />Remember Me<br/><br/> <input type="submit" name="submit" value="Submit" /> </form> Here is my processor: <?php // connect to the database include('conn.php'); $error = false; // run this only, once the user has hit the "Add Contact" button if (isset($_POST['centries'])) { // assign form inputs $fname = $_POST['fname']; $lname = $_POST['lname']; $email = $_POST['email']; $city = $_POST['city']; $state = $_POST['state']; $answer = $_POST['answer']; // validate inputs $query = "INSERT INTO centries (fname,lname,email,city,state,answer) VALUES ('".$fname."','".$lname."',''".$email."','".$city."','".$state."','".$answer."')"; $result = $database->query($query); ?> Any help is much appreciated. Thanks in advance, Jawinn
Is include('conn.php'); actually giving you a connected database object? Are you getting any errors, or is it just not inserting? How is the database table defined? States are often only given 2 letters for the code.
No-one can tell what that code does unless you describe the $database object. Use error_reporting(E_ALL ^ E_STRICT) at the top of your code to catch as many warnings and notices as possible. Also use the error function of your database interface, whatever it is.
Yes I changed my code slightly to this: <?php include('conn.php'); $error = false; if(isset($_POST['submit'])) { $form = array(); $form['fname'] = $_POST['fname']; $form['lname'] = $_POST['lname']; $form['email'] = $_POST['email']; $form['city'] = $_POST['city']; $form['state'] = $_POST['state']; $answer = $_POST['answer']; if(!ini_get('magic_quotes_gpc')) { // Build safe query values string foreach($form as $key => $value) { $form[$key] = mysql_escape_string($value); } } $query = "INSERT INTO centries (fname,lname,email,city,state,answer) VALUES ('{$form['fname']}', '{$form['lname']}', '{$form['email']}', '{$form['city']}','{$form['state']}', '{$form['answer']}',)"; $result = $database->query($query); ?> I get this error now: Parse error: parse error, unexpected $ in xxx/xxx/xxx/answer.php on line 29
Try this: $query = "INSERT INTO centries (fname,lname,email,city,state,answer) VALUES ('" . $form['fname'] . "', '" . $form['lname'] . "', '" . $form['email'] . "', '" . $form['city'] . "','" . $form['state'] . "', '" . $form['answer'] . "')"; Code (markup):
You are using the ' sign as INSERT INTO table values('{$form['email']}'); See the ' sign inside $form['email'] and before and after '{}' That's why the code error. Try this code below. Secondly, I think you also need an auto increment field, else you might have problems displaying the output. <?php include('conn.php'); $error = false; if(isset($_POST['submit'])) { $formfname = $_POST['fname']; $formlname = $_POST['lname']; $formemail = $_POST['email']; $formcity = $_POST['city']; $formstate = $_POST['state']; $formanswer = $_POST['answer']; if(!ini_get('magic_quotes_gpc')) { // Build safe query values string foreach($form as $key => $value) { $form[$key] = mysql_escape_string($value); } } $query = "INSERT INTO centries (fname,lname,email,city,state,answer) VALUES ('$formfname', '$formlname', '$formemail','$formcity','$formstate','$formanswer')"; $result = $database->query($query); ?> Hope that helps you.