472,956 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,956 software developers and data experts.

Getting ID's to insert into a relation table for a "many to many" relation - MySQL

bugboy
160 100+
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!
Oct 14 '07 #1
4 5060
pbmods
5,821 Expert 4TB
Heya, bugboy.

You might like mysql_insert_id().
Oct 14 '07 #2
bugboy
160 100+
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..!?
Oct 15 '07 #3
bugboy
160 100+
Ok i'm closer... i'm not retrieving the results properly.. i thought if it only had one value that it would be assigned to my variable and i didn't need to fetch an array.. but maybe i do... I'm reading here:

http://us2.php.net/manual/en/function.mysql-query.php
Oct 15 '07 #4
bugboy
160 100+
YAY! i added this in my SELECT attempt to get the wID!! and it seems to work.. have to test it a bit...

[PHP]

if ($row = mysql_fetch_assoc($result4)) { $wid = $row["wID"]; }[/php]
Oct 15 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paul Janssen | last post by:
Hello! Can anyone help me out with the following situation: (a) a single query with 550 id's in the IN-clause resulting into 800+ seconds; (b) 550 queries with a single id in the IN-clause...
2
by: Larry R Harrison Jr | last post by:
I have an Access 97 database with 2 tables that have a one-many relationship. I have a SQL statement in the "one" table which I want to execute and insert 7 records into the "many" table, and I...
1
by: Guido | last post by:
How to cope with query output which provides all possible values based on a one to many relationship between two tables. I'm only interested in one answer per primary key in the 'one' table. I've...
2
by: hasanainf | last post by:
Hi, I have a main table named tblCrPurchase2 with following fields CrPurchase2ID - key field InvoiceDate InvoiceNumber PF1 I have a child table called tblCrPurchase2Detail (this is the many...
14
by: Sharon | last post by:
I have two tables in the DataSet, the first present rooms and the other furniture kinds. Furniture kind, like chare, can be in more then one room. And specific room can contain more then one...
4
by: Jan Nielsen | last post by:
Hi all I'm a former Access developer who would like to implement a many-to-many relation in about the same way you do in Access: With a subform and a combo box. Is it possible to use a...
2
by: etep | last post by:
In my effort to create an order management database, I have established a one to many relation between my tables orders and order details. Tables: Orders: Customer, shipping method, etc. ...
0
by: rengaraj | last post by:
Hello. I have an @Entity class, Person, with a @ManyToMany relation with itself. This relation is the friend association. The code of the class is as follows: @Entity @Table(name = "PERSON",...
1
by: lionelm2007 | last post by:
In mysql, I need to create a table to store the many-to-many relationship of the other two tables. Here is the problem: the first table has three fields that act as the primary key of the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.