I have a list of tags which I want to store in my database. The table called "tags" has 2 columns: id (auto-increment) and tag (varchar). The goal is to append every tag of my array to a unique row of the "tag" column. The error I get is this:
Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25) VALUES ('al' at line 1 in C:\xampp\htdocs\cmanager\draft.php on line 36 New tags created successfully Notice: Use of undefined constant tags - assumed 'tags' in C:\xampp\htdocs\cmanager\draft.php on line 34
And the code is the following:
<?php
include('../config.php');
$tagList = array('black', 'purple', 'alpha', 'pink', 'normal', 'green', 'shininess', 'specular', 'blue', 'orange',
'nylon', 'stretched', 'brown', 'yellow', 'green', 'suede', 'wood', 'linen', 'red', 'white', 'no', 'tile', 'gray',
'velvet', 'mauve', 'white');
sort($tagList);
print_r($tagList);
try {
$dbh = new PDO("mysql:host=$hostname;dbname=store", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
echo "Connected to database \r\n";
foreach ($tagList as $k => $v) {
$prep[':' . $k] = $v;
$bind = ':' . implode(',:', array_keys($tagList));
// $sql = 'INSERT INTO tags (tag) ' .
// ' VALUES (:bind)';
$sql = 'INSERT INTO ' . tags . '(' . implode(',', array_keys($tagList)) . ') ' . 'VALUES (' . $bind . ')';
$stmt = $dbh->prepare($sql);
$stmt->execute(array_combine(explode(',', $bind), array_values($tagList)));
echo "New tags created successfully \r\n";
}
} catch (PDOException $e) {
echo $e->getMessage();
}
I don't understand what is the mistake. Could someone help me with this? Thanks
echo $sql, what does the query look like? It seems like it looks likeINSERT INTO .. (0, 1, 2, ...), which is obviously nonsense, no?