Chris Mazzochi Chris Mazzochi - 2 years ago 86
SQL Question

SQL Syntax Using PHP Join

Database query is failing most certainly due to a syntax error below.
Does anyone see the error? I'd appreciate it.

$sql = "INSERT INTO ".self::$table_name." (";
$sql .= join(", ", array_keys($attributes));
$sql .= ") VALUES ('";
$sql .= join("', '", array_values($attributes));
$sql .= "')";

Answer Source

Your previous code would evaluate to an sql statement similar to:

$sql = "INSERT INTO tableName (1,2,3,4) VALUES ('a','b','c','d')";

The string should not fail if the keys in $attribute are actually the columns in your database, and if the datatypes of the values in $attribute are in the necessary format of their respective columns.

Seeing that your sql query failed, one of the previous 2 conditions is probably false. You should try commenting out the execution of the query, and temporarily use the code echo $sql, that way you'll be able to identify what part of your code is causing the error.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download