Connecting Tech Pros Worldwide Forums | Help | Site Map

saving data to another database

Newbie
 
Join Date: Feb 2007
Posts: 12
#1: May 17 '07
I have a database that users can search for concert listings.
I also have a user registration database where users can log on.
I want to be able to have a link in the search results for the concert listings to save the show information to their profile.
I have thought long and hard about this but I am still green on PHP
I thought i could create a link next to the each concert listing (SAVE THIS EVENT) and then send the event info to a form that automatically inserts it into their saved shows column in the user reg database.
Am I even close?
If so how would i send the info to the user reg database?

Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#2: May 17 '07

re: saving data to another database


Quote:

Originally Posted by thepresidentis

I have a database that users can search for concert listings.
I also have a user registration database where users can log on.
I want to be able to have a link in the search results for the concert listings to save the show information to their profile.
I have thought long and hard about this but I am still green on PHP
I thought i could create a link next to the each concert listing (SAVE THIS EVENT) and then send the event info to a form that automatically inserts it into their saved shows column in the user reg database.
Am I even close?
If so how would i send the info to the user reg database?

I would create another table, consisting of UserID and EventID. When a user saves an event, put it in the table.
Newbie
 
Join Date: Feb 2007
Posts: 12
#3: May 17 '07

re: saving data to another database


Quote:

Originally Posted by Motoma

I would create another table, consisting of UserID and EventID. When a user saves an event, put it in the table.

okay. Thanks
one more question though,
how would I send the data from the concert listing to the database?
i was thinking that i could create the save this show link and then if they were to press it then it would call a function?
that would use insert into.
but my problem is that i am still new to coding with php, and I am not sure how to pass the data stored in the variables to a function that will use insert into.
any help would be such a life saver!!!!
here is what I have so far....
Expand|Select|Wrap|Line Numbers
  1.  
  2. echo "Results<BR>";
  3.  
  4. $count = 1 + $s;
  5.  
  6.  
  7.  
  8. // this registers the information into a variable
  9.  
  10. while ($row= mysql_fetch_array($result)) {
  11.  
  12. $title = $row["City"];
  13. $title2 = $row["State"];
  14. $title3 = $row["Metro_Area"];
  15. $title4 = $row["Date_Input"];
  16. $title5 = $row["Band_Or_Event_Name"];
  17. $title6 = $row["Genre"];
  18. $title7 = $row["Genre_2"];
  19. $title8 = $row["Genre_3"];
  20. $title9 = $row["Genre_4"];
  21. $title14 = $row["Venue"];
  22. $title10 = $row["Other_Info"];
  23. $title11 = $row["MP3_Link"];
  24. $title12 = $row["Link_to_Directions"];
  25. $title13 = $row["Age_Restrictions"];
  26.  
  27.  
  28.  
  29. //this displays the information stored in the variables
  30.  
  31. echo "$count.)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  32. Date : <FONT color='red'>$title4</FONT><BR> 
  33. &nbsp;Band or event : <FONT color='red'>$title5</FONT> 
  34. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  35. &nbsp;&nbsp;Venue : <FONT color='red'>$title14</FONT><BR>
  36. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  37. &nbsp;&nbsp;&nbsp;&nbsp;City : <FONT color='blue'>$title</FONT><BR> 
  38. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  39. &nbsp;&nbsp;&nbsp;&nbsp;State : <FONT color='red'>$title2</FONT> <BR>
  40. &nbsp;&nbsp;&nbsp;Metro Area : <FONT color='red'>$title3</FONT><BR>
  41. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  42. &nbsp;Genre : <FONT color='red'>$title6</FONT><BR> 
  43. &nbsp;&nbsp;Other genre's : <FONT color='red'>$title7,&nbsp;$title8,&nbsp;$title9</FONT><BR> 
  44. &nbsp;Age restriction : <FONT color='red'>$title13</FONT><BR>
  45. &nbsp;Other information : <FONT color='red'>$title10</FONT><BR> 
  46. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Link to mp3 : <FONT color='blue'>$title11</FONT><BR> 
  47. &nbsp;Link to directions : <FONT color='blue'>$title12</FONT></a><BR><BR>";
  48.  
  49. //this is what i used to create a link but i would rather use a function to process the insert into.
  50.  
  51. echo "[<a href=\"http://www.showsniffer.com/fgen/use/Submit/SaveShow.html\">Save This Event</a>]";
  52.  
  53. $count++ ;
  54.  
  55. }
  56.  
  57.  
  58.  
  59. $currPage = (($s/$limit) + 1);
  60.  
Newbie
 
Join Date: Feb 2007
Posts: 12
#4: May 17 '07

re: saving data to another database


also could I just register the primary Key as a variable and just save the Primary Key in the saved events column in the USER REG DB?
I am Hoping this will save disk space rather than saving all of the show information again.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#5: May 17 '07

re: saving data to another database


I'm moving this thread to the MySQL forum 'cause that's what it's turning into!

Welcome to database normalization 101.

Alrightey. So you have concerts, and you have Users. Users can save as many events as they want, and more than one User can save any given concert. You have what we like to call a "many-to-many" relationship.

Many-to-many relationships between two tables actually requires three tables:
  • Data_Concerts
  • Data_Users
  • Map_UserConcert

Map_UserConcert tells you what Concerts each User has saved:

Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT * FROM `Map_UserConcert` LIMIT 5;
  2. +--------+-----------+
  3. | userid | concertid |
  4. +--------+-----------+
  5. |       1|        158|
  6. |       8|         32|
  7. |       2|         60|
  8. |      12|        204|
  9. |       1|          1|
  10. +--------+-----------+
  11. 5 rows in set (0.0000 sec)
  12.  

As you can see (at least by this sample of 5 rows), User #1 saved concerts 1 & 158; User #2 saved concert 60; User #8 saved concert 32 and User #12 saved concert 204.

For best results, you'll be wanting to create a view:

Expand|Select|Wrap|Line Numbers
  1. CREATE VIEW `View_SavedConcerts` AS SELECT * FROM (`Data_Users` LEFT JOIN `Map_UserConcert` USING(`userid`) LEFT JOIN `Data_Concerts` USING(`concertid`)) ORDER BY `userid`, `concertid` ASC;
  2.  
Which would give you something like this:

Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT * FROM `View_SavedConcerts` WHERE `userid` = '1' AND `Date_Input` > NOW();
  2. +--------+------+-----------+---------+-------+---------+
  3. | userid | Name | concertid |   City  | State | etc.... |
  4. +--------+------+-----------+---------+-------+---------+
  5. |       1| John |       158 | Chicago |    IL |
  6. +--------+------+-----------+---------+-------+
  7. | etc... |
  8. +--------+
  9.  
And boom; all in one table, you have your User and concert data.
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#6: May 18 '07

re: saving data to another database


This tutorial may help

Normalization and Table structures

Mary
Newbie
 
Join Date: Feb 2007
Posts: 12
#7: May 20 '07

re: saving data to another database


Thanks for all the info, it is really helping...
I am still a little confused(sorry)
on map_concerts does there need to be an index?
I have 2 fields
userid and concert id.
Newbie
 
Join Date: Feb 2007
Posts: 12
#8: May 20 '07

re: saving data to another database


p.s the users db is a seperate database than the concerts database

here is my structure

Database: EVENT DATA
Table:Events
then there is a seperate fields for State, city, metro area, etc...

Database: UserDB
Table: Active_guests
Table:Active_Users
Table:Banned_users
Table:Map_userconcerts
Table:Users
msquared's Avatar
Administrator
 
Join Date: Aug 2006
Location: Dublin, Ireland
Posts: 10,885
#9: May 20 '07

re: saving data to another database


Quote:

Originally Posted by thepresidentis

Thanks for all the info, it is really helping...
I am still a little confused(sorry)
on map_concerts does there need to be an index?
I have 2 fields
userid and concert id.

Assuming you are talking about a JOIN table to split up a many to many relationship then it would have a composite primary key made up of both foreign keys.
Newbie
 
Join Date: Feb 2007
Posts: 12
#10: May 20 '07

re: saving data to another database


would it be possible to create a clickable link to envoke a variable called
Expand|Select|Wrap|Line Numbers
  1. $SAVE = mysql_connect("host","username","password");
  2. mysql_select_db("userdb");
  3. $query2="insert into map_userconcert (userid,concertid) values ('".$userid."','".$concertid."')";
  4. mysql_query($query2);
  5.  
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#11: May 20 '07

re: saving data to another database


Quote:

Originally Posted by thepresidentis

would it be possible to create a clickable link to envoke a variable called ....

To do that, create a link to another PHP script that executes that statement.

E.g.,
Expand|Select|Wrap|Line Numbers
  1. <a href="save.php?concertid=4">Save this Concert</a>
  2.  
Then in save.php:
Expand|Select|Wrap|Line Numbers
  1. mysql_connect("host","username","password");
  2. mysql_select_db("userdb");
  3. $query2="insert into map_userconcert (userid,concertid) values ('".$userid."','".intval($_GET['concertid'])."')";
  4. mysql_query($query2);
  5.  
Reply