| re: Getting ID's to insert into a relation table for a "many to many" relation - MySQL
Cool! thanks!
I'm so close! I'm getting a '0' wid with mysql_insert_id() when the IGNORE is used so i have an 'else' that initiates a SELECT on w.wid to find it instead. Everything appears to work except I'm still getting a wid of '0' inserted when i have to use the SELECT to retrieve it ...It should set $wid to the proper key but doesn't.. I even have an elseif to stop it if it doesn't ...but that doesn't seem to work either.. any ideas?
[PHP]<?php
$word = $_GET['word'];
$def = $_GET['def'];
require_once ('mysql_connect.php');
$query1 = "INSERT IGNORE INTO db.w (db.w.word, db.w.creationdate) VALUES ('$word', NOW())";
$result1 = @mysql_query ($query1);
$wid = mysql_insert_id();
$query2 = "INSERT INTO db.c (db.c.def, db.c.creationdate) VALUES ('$def', NOW())";
if ($wid > 0) { $result2 = @mysql_query ($query2);
$cid = mysql_insert_id();
$query3 = "INSERT IGNORE INTO db.s (db.s.wid, db.s.cid, db.s.date) VALUES ('$wid', '$cid', NOW())";
$result3 = @mysql_query ($query3);
echo '<p>It worked and new word was added';
$ok = 1;
} else { $query4 = "SELECT db.w.wid FROM db.w WHERE db.w.word = '$word'";
$wid = @mysql_query ($query4);
}
if ($ok == 1) { die('Yay');
} elseif ($wid == 0) { die('Sorry there\'s a problem');
} else { $result2 = @mysql_query ($query2);
$cid = mysql_insert_id();
$query5 = "INSERT IGNORE INTO db.s (db.s.wid, db.s.cid, db.s.date) VALUES ('$wid', '$cid', NOW())";
$result5 = @mysql_query ($query5);
echo '<p>It worked but the word already existed';
}
mysql_close();
?>[/PHP]
If i 'echo' $wid immediately after the $query4 SELECT attempt i don't get a number i get "Resource id #3".. maybe this is a clue.. i just don't know to what..!?
|