|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
Multiple insert problem
I'm trying to set up an insert query to add info into 2 tables at once, a "one-to-many" relation I beleive its called. Lets say its a song lyric database. Table 1 is called "artist" with the fields "id" and "artist". Table 2 is called "song" with the fields of "title", "lyrics", and a reference to the artist id, which i think is where this is messing up. Basically, I'll have a php page to show just a list of the artists from Table 1, which links to Table 2 showing song titles and lyrics.
Heres the insert query... php Code:
This is the only reference I could find to do this insert, and I'm sure its wrong. I don't get mysql error messages, but nothing is going into the tables. ??? |
|
#2
|
|||||
|
|||||
|
RE: Multiple insert problem
I am assuming your id field in the artist database is an autoincrement field so here goes.
in a nutshell... 1. you need to insert the artist name into the first table. 2. you need to find out the autoincrement id of the record just inserted 3. you need to insert the data into the song table. here is what you need to do php Code:
that should solve your problem. |
|
#3
|
|||
|
|||
|
RE: Multiple insert problem
The artist table still needs an id field to be auto-incremented, doesnt it? I'm not getting it to work either way.
|
|
#4
|
|||
|
|||
|
RE: Multiple insert problem
yes, that is the only way it will work.
|
|
#5
|
|||||
|
|||||
|
RE: Multiple insert problem
php Code:
Nothing is going in. I have an id field auto-incrementing on the artist table. Does the song table need an id field also? Indexing, primary? |
|
#6
|
|||
|
|||
|
RE: Multiple insert problem
I just ran the code here and it worked like a charm. I am not quite sure what is going on...I will keep looking
|
|
#7
|
|||
|
|||
|
RE: Multiple insert problem
OK, I'll start the database from scratch and try again.
|
|
#8
|
|||
|
|||
|
RE: Multiple insert problem
Found the problem. Hopefully the display part will be easier. Thnx for the help.
|
|
#9
|
|||
|
|||
|
RE: Multiple insert problem
what was the problem?
|
|
#10
|
|||
|
|||
|
RE: Multiple insert problem
I took the (yada yada) stuff out of the insert querys and it went right in. Now my php artist list shows up with the artist names as links, which is fine, because I want them to link to the song table, but I'm not getting the song table to display the info when i click on them, even though the info is in the db. Not sure if the link info is getting passed to bring up the song table or what, probably need to redo the code for it.
|
|
#11
|
||||||||
|
||||||||
|
RE: Multiple insert problem
OK, I made some minor changes, maybe you can help me straighten this out.
I changed the original insert query to add the artist and title to the artist table. This way, the artist listing will show the artist and song title. However, the artist.php page shows only the artist as the link to the second table. I'd like to change this to include title, with the title as the link. I haven't been able to figure out how to change this. php Code:
The second table (song.php) being linked to isn't displaying anything. I can usually alter a results page to display the way I want, but this one has me totally stumped. php Code:
Sorry if I'm dense on this, I usually don't have this much trouble. |
|
#12
|
|||
|
|||
|
RE: Multiple insert problem
the song query is incorrect.
$result=mysql_query("SELECT * FROM song WHERE artist_id='.$artist.'"); should be $result=mysql_query("SELECT * FROM song WHERE artist_id='".$artist."'"); that should fix it. and as for including the song title in the artist table you have broken the one to many relational rule and will have quite a bit of redundant data in the db. If you want to have the artist and title display together for each title, you would use a join query to pull the data from both tables. |