tvanmaren Posted July 28, 2010 Report Share Posted July 28, 2010 Hi all. This is my first programming in PHP, but I keep getting syntax error when trying to use INSERT INTO ... VALUES. Here's the code. <?php ini_set('display_errors', 'On'); error_reporting(E_ALL | E_STRICT); /* server info */ $server = 'localhost'; $user = 'root'; $password = ''; $database = 'cart'; /* get variables */ $name_last1 = $_POST['name_last1']; $name_first1 = $_POST['name_first1']; $name_mi1 = $_POST['name_mi1']; $gender1 = $_POST['gender1']; $birthdate1 = $_POST['birthdate1']; $ssn1 = $_POST['ssn1']; $name_last2 = $_POST['name_last2']; $name_first2 = $_POST['name_first2']; $name_mi2 = $_POST['name_mi2']; $gender2 = $_POST['gender2']; $birthdate2 = $_POST['birthdate2']; $ssn2 = $_POST['ssn2']; $name_last3 = $_POST['name_last3']; $name_first3 = $_POST['name_first3']; $name_mi3 = $_POST['name_mi3']; $gender3 = $_POST['gender3']; $birthdate3 = $_POST['birthdate3']; $ssn3 = $_POST['ssn3']; $name_last4 = $_POST['name_last4']; $name_first4 = $_POST['name_first4']; $name_mi4 = $_POST['name_mi4']; $gender4 = $_POST['gender4']; $birthdate4 = $_POST['birthdate4']; $ssn4 = $_POST['ssn4']; $name_last5 = $_POST['name_last5']; $name_first5 = $_POST['name_first5']; $name_mi5 = $_POST['name_mi5']; $gender5 = $_POST['gender5']; $birthdate5 = $_POST['birthdate5']; $ssn5 = $_POST['ssn5']; $name_last6 = $_POST['name_last6']; $name_first6 = $_POST['name_first6']; $name_mi6 = $_POST['name_mi6']; $gender6 = $_POST['gender6']; $birthdate6 = $_POST['birthdate6']; $ssn6 = $_POST['ssn6']; $street = $_POST['street']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $email = $_POST['email']; $home_phone = $_POST['home_phone']; $insurance = $_POST['insurance']; $policy = $_POST['policy']; $group = $_POST['group']; /* server and database connection */ $my_connection = mysql_connect($server, $user, $password); if(!$my_connection) { die('Could not connect to server:' . mysql_error()); } echo 'Connected successfully to $server. <br><br>'; mysql_select_db ($database, $my_connection) or die ( 'Couldn\'t open $database: ' . mysql_error() ); echo 'Connected successfully to $database.'; /* Add to database */ $query = " INSERT INTO applicant ( name_last1, name_first1, name_mi1, gender1, birthdate1, ssn1, name_last2, name_first2, name_mi2, gender2, birthdate2, ssn2, name_last3, name_first3, name_mi3, gender3, birthdate3, ssn3, name_last4, name_first4, name_mi4, gender4, birthdate4, ssn4, name_last5, name_first5, name_mi5, gender5, birthdate5, ssn5, name_last6, name_first6, name_mi6, gender6, birthdate6, ssn6, street, city, state, zip, email, home_phone, insurance, policy, group ) VALUES ( '$name_last1' , '$name_first1', '$name_mi1', '$gender1', '$birthdate1', '$ssn1', '$name_last2', '$name_first2', '$name_mi2', '$gender2', '$birthdate2', '$ssn2', '$name_last3', '$name_first3', '$name_mi3', '$gender3', '$birthdate3', '$ssn3', '$name_last4', '$name_first4', '$name_mi4', '$gender4', '$birthdate4', '$ssn4', '$name_last5', '$name_first5', '$name_mi5', '$gender5', '$birthdate5', '$ssn5', '$name_last6', '$name_first6', '$name_mi6', '$gender6', '$birthdate6', '$ssn6', '$street', '$city', '$state', '$zip', '$email', '$home_phone', '$insurance', '$policy', '$group') " ; $result = mysql_query ($query) or die ( "Error in query: $query. " . mysql_error() ); echo "<br><br>New record inserted with ID ".mysql_insert_id(); mysql_close ($my_connection); ?> and the error: Connected successfully to $server. Connected successfully to $database.Error in query: INSERT INTO applicant ( name_last1, name_first1, name_mi1, gender1, birthdate1, ssn1, name_last2, name_first2, name_mi2, gender2, birthdate2, ssn2, name_last3, name_first3, name_mi3, gender3, birthdate3, ssn3, name_last4, name_first4, name_mi4, gender4, birthdate4, ssn4, name_last5, name_first5, name_mi5, gender5, birthdate5, ssn5, name_last6, name_first6, name_mi6, gender6, birthdate6, ssn6, street, city, state, zip, email, home_phone, insurance, policy, group ) VALUES ( 'V' , 'T', 'p', 'M', '', '', '', '', 'a', 'F', '', '', '', '', '', ' ', '', '', '', '', '', ' ', '', '', '', '', '', ' ', '', '', '', '', '', 'M', '', '', '', '', '', '', '', '', '', '', '') . You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group ) VALUES ( 'V' , 'T', 'p', 'M', '', '', '' at line 1 Any suggestions? Thanks! Quote Link to comment Share on other sites More sharing options...
administrator Posted July 28, 2010 Report Share Posted July 28, 2010 It's simple really, just recheck your SQL syntax. Stefan Quote Link to comment Share on other sites More sharing options...
tvanmaren Posted July 28, 2010 Author Report Share Posted July 28, 2010 It's simple really, just recheck your SQL syntax. Stefan Seriously, it's not something I can't find, I think it's something I'm not understanding. I'm copying the format I found from the ZEND PHP 101 tutorial. I don't see what I'm missing. I'll keep messing with it I guess. Quote Link to comment Share on other sites More sharing options...
administrator Posted July 28, 2010 Report Share Posted July 28, 2010 This is just some small clerical error ... a stray character, probably a comma. ... You are just going to have to search for it. PS, that is a big blathering SQL INSERT statement, hard to manage. You might be better off using a 'prepared statement' as it is much easier to debug and provides extra features for free. Stefan Quote Link to comment Share on other sites More sharing options...
Reedyseth Posted July 28, 2010 Report Share Posted July 28, 2010 Those are many fields for a table, unless you extremely need these fields in the table just leave them an check that you are inserting the correct data type. If some of your table fields requires a INT and you are inserting a string that may cause you problems. Otherwise split you table into smallers ones so that you can handle your data easier. And I'm totally agree with Stefan, may be you misspelled you code, check it more than twice. Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.