0

My query:

$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 numbers only, as example $name = 100, then everything is OK.

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

1054 - Unknown column 'Cable' in 'field list'

Why #1054? "Cable" is not a column!

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.