basically I am trying to implement a function in one of my PHP classes that makes an entry into a junction table for a many to many relationship.
This is the method here:
public function setTags($value, $id){
global $db;
$tags = $value;
$query .= "DELETE FROM directorycolumntags
WHERE directorycolumn_id = $id; ";
foreach($tags as $tag){
$query .= "INSERT INTO directorycolumntags (directorycolumn_id, tag_id)
VALUES (".$id.",".$tag.");";
}
mysql_query($query);
}
The SQL is produces works fine, as I've echoed it and manually executed it via phpMyAdmin. However, If I leave it as above, the data is never inserted. Does anyone know why this might be happening?
This is the sql it is generating which works fine when I type it manually in:
DELETE FROM directorycolumntags WHERE directorycolumn_id = 178;
INSERT INTO directorycolumntags (directorycolumn_id, tag_id) VALUES (178,29);
INSERT INTO directorycolumntags (directorycolumn_id, tag_id) VALUES (178,30);
INSERT INTO directorycolumntags (directorycolumn_id, tag_id) VALUES (178,32);
mysql_querydoes only one statement at a time, it is also deprecatedmysqlextension. You can usemysqli, but you'd be better of deleting, and then using a prepared statement to insert your tags one by one (perhaps in a transaction). Learn the current DB extensions (mysqliorPDO). PS:globalis evilmysqlextension and I'm just concerned with patching the existing functionality is it safe to use the deprecated extension, until I'm in a position to upgrade the entire app itself?mysql_*function call will result in aE_DEPRECATEDnotice being issued, clogging the logs. The extension itself is scheduled for removal in the future, though no date has been set. You can quite easily update your code to the newer extensions. I preferPDO, for its clean API and it being, all things considered, easy to learn (check my answer, it contains a full example of how you can do what you're trying to do using PDO)