I'm inserting a new word into table 'w' and a definition into table 'c' which are linked in table 's' which is the relation table for the many to many relationship between 'w' and 'c'.
I've been passed 2 variables from a form; $word and $def and i'm inserting them ok but i'm having trouble getting and passing on their ID's in order to insert them into the relation table 's'
I've checked every query and they all work individually, so i think it must be something in my php. I'm a beginner trying to teach myself so i may be going about this totally wrong.
[PHP]<?php
//I get the variables OK
$word = $_GET['word'];
$def = $_GET['def'];
//I connect OK
require_once ('mysql_connect.php');
//This works fine. w.word is a unique column so i use IGNORE
$query1 = "INSERT IGNORE INTO db.w (db.w.word, db.w.date) VALUES ('$word', NOW())";
$result1 = @mysql_query ($query1);
//This works fine
$query2 = "INSERT INTO db.c (db.c.def, db.c.date) VALUES ('$def', NOW())";
$result2 = @mysql_query ($query2);
//This works on it's own but may not here.
$query3 = "SELECT db.w.wid FROM db.w WHERE db.w.word = '$word' LIMIT 1";
$result3 = @mysql_query ($query3);
//This works on it's own but may not here.. the 'titles' are not unique so I order them by their ID and pick the last one created to get the proper one.
$query4 = "SELECT db.c.cid FROM db.c where db.c.title = '$mod' ORDER BY db.c.cid DESC LIMIT 1";
$result4 = @mysql_query ($query4);
//See note below:
$query5 = "INSERT IGNORE INTO db.s (db.s.wid, db.s.cid) VALUES ('$result3', '$result4')";
$result5 = @mysql_query ($query5);
mysql_close();
?>[/php]
The last query is suppose to take the ID's from the previous two queries and INSERT IGNORE them into my relation table 's'. When run it on an empty table it inserts but assignes a '0' for both ID's. The second time i run it with a different word and def nothing happens, no insert. Because the two ID's are indexed together as unique it's probably not inserting the second time because it still has a value of '0' for the ID's.
My question is why isn't it passing on values for $result3 and $result4 to $query5?
And.. i could probably use some help with my methodology. Any ways to do this type of thing without 5 queries?
Thanks!