Jump to content

PHP query failing despite valid...um...query.


WhiteRau

Recommended Posts

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 = 'bigKahuna@bigKahuna.edu';

$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!  :confused:

 

WR!

Link to comment
Share on other sites

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', 'bigKahuna@bigKahuna.edu')

 

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, bigKahuna@bigKahuna.edu)

 

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)

Link to comment
Share on other sites

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}'";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.