Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP script thrown Error #1054 in MySQL when column is exist

Good day everyone!

My querry:

$name    = $vm_product->product_name;

$columns = array('cart_id', 'product_id', 'quantity', 'price', 'name');
$values  = array($cart_id, $product_id, $quantity, $price, $name);

$query
           ->insert($db->quoteName('#__products'))
           ->columns($db->quoteName($columns))
           ->values(implode(',', $values));

$db->setQuery($query);
$db->execute();

If $name is number only, as example $name = 100 everything is OK.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But if $name is string, as example $name = "Cable" an error is thrown:

1054 – Unknown column ‘Cable’ in ‘field list’

Why 1054? "Cable" is not column!

>Solution :

Because when you implode, your values becomes WHERE column_name IN (Cable,Another string,Some other value) and that is syntax error because of missing quotes around string literals (when there is no spaces in strings, mysql assumes its names of columns).

Use ->values("'" . implode("','", $values) . "'"), or consult your used code base documentation how to properly and securely generate WHERE IN condition

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading