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 = '[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!  :confused:

 

WR!

Link to comment
https://forums.phpfreaks.com/topic/250781-php-query-failing-despite-validumquery/
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', '[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)

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.