nomadsoul Posted January 17, 2011 Share Posted January 17, 2011 G'day Freaks MySql version 5.1.4 MyTable: id -ai pk firstname varchar(20) lastname varchar(20 dob date(yyyy-m-d) gender varchar(1) MyProblem Heres the error after clicking submit: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\nomadsoul\mysql_insert.php on line 21(the sql variable) Here's the code (with php and html): <html> <body> <form action="mysql_insert.php" method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> Dob: <input type="text" name="dob" /> Gender: <input type="text" name="gender" /> <input type="submit" /> </form> </body> </html> <?php require ("connect.php"); $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $dob = $_POST['dob']; $gender = $_POST['gender']; $sql="INSERT INTO people(firstname, lastname, dob, gender) VALUES('$_POST['firstname']','$_POST['lastname']','$_POST['dob']','$_POST['gender'])"; when I comment out the query the error goes away and the form shows. The query doesn't need place values for the id ai does it? I'm pretty sure it dosen't Hope I've provided enough info. Sorry if I didn't All of my other php CRUDs are working. This last Insert script has been the most stubborn. ?> Quote Link to comment https://forums.phpfreaks.com/topic/224682-insert-problems-from-form-with-error/ Share on other sites More sharing options...
suresh_kamrushi Posted January 17, 2011 Share Posted January 17, 2011 There is simple problem with insert query. Please write it below: $sql="INSERT INTO people(firstname, lastname, dob, gender) VALUES ('".$_POST['firstname']."','".$_POST['lastname']."',".$_POST['dob'].",'".$_POST['gender']."')"; It is a good practice to store all forms value in a variable and than use in different place. Quote Link to comment https://forums.phpfreaks.com/topic/224682-insert-problems-from-form-with-error/#findComment-1160598 Share on other sites More sharing options...
nomadsoul Posted January 17, 2011 Author Share Posted January 17, 2011 thanks suresh_kamrushi I will do that. Also, do I need the mysql_query() function too? Quote Link to comment https://forums.phpfreaks.com/topic/224682-insert-problems-from-form-with-error/#findComment-1160617 Share on other sites More sharing options...
nomadsoul Posted January 17, 2011 Author Share Posted January 17, 2011 I am no longer getting the error. THANKS! But nothing is inserting in the db. I think I'll need the query function. Quote Link to comment https://forums.phpfreaks.com/topic/224682-insert-problems-from-form-with-error/#findComment-1160618 Share on other sites More sharing options...
nomadsoul Posted January 17, 2011 Author Share Posted January 17, 2011 Used the query function like this: $sql=mysql_query("INSERT INTO people(firstname, lastname, dob, gender) VALUES ('".$_POST['firstname']."','".$_POST['lastname']."',".$_POST['dob'].",'".$_POST['gender']."')"); and now INSERT is working great. Thanks again suresh Quote Link to comment https://forums.phpfreaks.com/topic/224682-insert-problems-from-form-with-error/#findComment-1160622 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.