J4B Posted March 18, 2009 Share Posted March 18, 2009 Ok so I've got my script that I'm working on that is basically a rating system for movies for my friends to post ratings on. Now the problem that I can't figure out is this: $insert_movie = mysql_query("INSERT INTO movie (num, title) VALUES ('$num', '$title')"); $insert_movie = mysql_query("INSERT INTO members (username, movies) VALUES ('$myusername', '$num,')"); if($rate != '0') { $insert_movie = mysql_query("INSERT INTO members (rate) VALUES ('$rate,')"); $insert_movie = mysql_query("INSERT INTO movie (rate) VALUES ('$rate,')"); } Alright what I need it to do though, is when it tries to insert it into the members instead of making a new row I need it to add to the row that under the column "username" is the variable "$myusername". Quote Link to comment Share on other sites More sharing options...
Maq Posted March 18, 2009 Share Posted March 18, 2009 You can get the last inserted id with mysql_insert_id(). That way you can just update to the most recent row rather than create a new one. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted March 18, 2009 Share Posted March 18, 2009 Take a look at UPDATE in your Mysql manual or consider doing just one insert to members and one insert to movies: INSERT INTO members (username, movies,rate) VALUES ('$myusername', '$num', '$rate')"); INSERT INTO movies (num,title,rate) VALUES ('$num', '$title', '$rate')"); Though you should really normalise this database properly, or how will two different members rate the same movie? Quote Link to comment Share on other sites More sharing options...
J4B Posted March 18, 2009 Author Share Posted March 18, 2009 Though you should really normalise this database properly, or how will two different members rate the same movie? Umm, I'm still working on this. This is actually the first site I have ever made...ever. So I'm still learning quite a bit. This particular code is to add a new movie to the database, but when I program the page to add existing movies to your profile it will be different. I'll check out update right now. [EDIT] So I read over it, and it looked like it should be set like this? but it didn't work. $insert_movie = mysql_query("INSERT INTO movie (num, title, rate) VALUES ('$imdb', '$title', '$rate,')"); $insert_movie1 = mysql_query("UPDATE members SET rate = '$rate' WHERE username = '$myusername'"); Quote Link to comment Share on other sites More sharing options...
J4B Posted March 18, 2009 Author Share Posted March 18, 2009 *bump* Quote Link to comment Share on other sites More sharing options...
sloth456 Posted March 18, 2009 Share Posted March 18, 2009 hmmm, looks ok to me, put this at the end of your update statement like so $insert_movie1 = mysql_query("UPDATE members SET rate = '$rate' WHERE username = '$myusername'") or die(mysql_error()); Then you should get some sort of error that will help discern the problem, post the error here if you can't figure it out yourself. Another tip: I notice that when you run your mysql query's, you're always putting the result into the $insert_movie variable. like this: $insert_movie = mysql_query("INSERT INTO movie (num, title, rate) VALUES ('$imdb', '$title', '$rate,')"); It's not required, you don't need to put it into a variable, it'll run just fine without like this. mysql_query("INSERT INTO movie (num, title, rate) VALUES ('$imdb', '$title', '$rate,')"); You only ever need to put the result of a mysql_query() in a variable if you're doing a SELECT statement and want to then read through the results. Quote Link to comment Share on other sites More sharing options...
J4B Posted March 18, 2009 Author Share Posted March 18, 2009 I had it doing that so I could check if it was actually working, but doing the die(mysql_error()) is probably a lot better way. It was just a typo on my part was all the problem was, but I found it thanks to your method! Quote Link to comment Share on other sites More sharing options...
sloth456 Posted March 18, 2009 Share Posted March 18, 2009 No problem, please click the "solved" button right at the bottom of this page somewhere so that others can see the topic was solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.