WhiteRau Posted November 9, 2011 Share Posted November 9, 2011 here's the code: $companyName = 'big company'; $address1 = 'big bay #8'; $address2 = 'some big warehouse'; $city = 'big city'; $province = 'AB'; $postalCode = 'T1T0N0'; $phone = '0123456789'; $email2 = '[email protected]'; $query = "INSERT INTO clients ( companyName, address1, address2, city, province, postalCode, phone, email) VALUES ( ". $companyName.",".$address1.",".$address2.",".$city.",".$postalCode.",".$phone.",".$email2.")"; $result = mysql_query($query, $connexion); if ($result) { // Success! echo "Fabulous! check the DB, we did it! <br>"; ?> <pre> <?php print_r($result); ?> </pre> <?php } else { // Fail! echo"CRAAAAAPP! something went wrong. FIX IT! <br>"; echo mysql_error(); } if (isset($connexion)) { mysql_close($connexion); } i copied it over from an old *working* file to illustrate how a simple INSERT works. this is the error i get: 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 'company,big bay #8,some big warehouse,big city,T1T0N0,0123456789,bigKahuna@bigKa' at line 4 looks completely valid to me. all the database table elements are set to VARCHAR(80), so it can't be a space/type issue... halp! WR! Quote Link to comment https://forums.phpfreaks.com/topic/250781-php-query-failing-despite-validumquery/ Share on other sites More sharing options...
Psycho Posted November 9, 2011 Share Posted November 9, 2011 You need to put quotes around your values 'inside' the query. When your query fails it is helpful to echo it to the page. The complete query SHOULD look something like this INSERT INTO clients (companyName, address1, address2, city, province, postalCode, phone, email) VALUES ('big company', 'big bay #8', 'some big warehouse', 'big city', 'T1T0N0', '0123456789', '[email protected]') Yours currently looks like this (note, no quotes around the values. INSERT INTO clients (companyName, address1, address2, city, province, postalCode, phone, email) VALUES (big company, big bay #8, some big warehouse, big city, T1T0N0, 0123456789, [email protected]) Change your query definition to this $query = "INSERT INTO clients (`companyName`, `address1`, `address2`, `city`, `province`, `postalCode`, `phone`, `email`) VALUES ('{$companyName}', '{$address1}', '{$address2}', '{$city}', '{$province}', '{$postalCode}', '{$phone}', '{$email2}'"; EDIT: Added province (credit to elkyn) Quote Link to comment https://forums.phpfreaks.com/topic/250781-php-query-failing-despite-validumquery/#findComment-1286667 Share on other sites More sharing options...
elkyn Posted November 9, 2011 Share Posted November 9, 2011 You also missed province. $query = "INSERT INTO clients (`companyName`, `address1`, `address2`, `city`, `province`, `postalCode`, `phone`, `email`) VALUES ('{$companyName}', '{$address1}', '{$address2}', '{$city}', '{$province}', '{$postalCode}', '{$phone}', '{$email2}'"; Quote Link to comment https://forums.phpfreaks.com/topic/250781-php-query-failing-despite-validumquery/#findComment-1286670 Share on other sites More sharing options...
WhiteRau Posted November 9, 2011 Author Share Posted November 9, 2011 i see. the variables evaluated correctly BUT they needed to be quoted. got it. thanks you two! WR! Quote Link to comment https://forums.phpfreaks.com/topic/250781-php-query-failing-despite-validumquery/#findComment-1286673 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.