RyanSF07 Posted August 17, 2011 Share Posted August 17, 2011 Hi all, This is a first for me... the code below is inserting 4 rows instead of 1. In one row everything is inserted, but in the 3 others, all but the $session_userid is inserted (it's inserted as 0). I don't want those 3 other rows, just the 1. Can you help point out what's going on here? $favoritequiz = (int) $_GET['favorite_id']; if ($favoritequiz) { $favqtitle = mysql_real_escape_string($_GET['favorite_title']); $favqusername = mysql_real_escape_string($_GET['favorite_usern']); $favqphoto = mysql_real_escape_string($_GET['favorite_photo']); mysql_query("INSERT INTO favorites (favuser_id, favquiz_id, favtitle, favusername, favphoto) VALUES ('$session_userid', '$favoritequiz', '$favqtitle', '$favqusername', '$favqphoto')"); } thanks, Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/ Share on other sites More sharing options...
Alex Posted August 17, 2011 Share Posted August 17, 2011 Does this happen to be inside a loop or something? I think we'll need to see more context to help. Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1258705 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 The code you provided should only add one row to the database. Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1258707 Share on other sites More sharing options...
RyanSF07 Posted August 17, 2011 Author Share Posted August 17, 2011 regarding loop... I don't think so... here's how I have this set up: ... includes.inc etc called then.... $sql = "SELECT * FROM video, registered_users WHERE video.id = '$get_id' AND video.user_id = registered_users.id"; $query_result = mysql_query($sql); $row = mysql_fetch_array($query_result); if ($session_userid) { $favtitle = $row['title']; $favusername = $row['user_name']; $favphoto = $row['photo']; $favorite = "<a style=\"position: relative; left: 310px; top: -43px; height: 0px; width: 100px; padding: 0;\" href=\"thispage.php?favorite_id=$get_id&id=$get_id&favorite_title=$favtitle&favorite_usern=$favusername&favorite_photo=$favphoto\" onClick=\"return confirm('click OK to add to favorites message');\"><img src='images/favorite_button.gif' alt='favorite button' width='90' height='40' border='0'></a>"; }else{ $favorite = "<a style=\"position: relative; left: 310px; top: -43px; height: 0px; width: 100px; padding: 0;\" href=\"thispage.php?id=$get_id\" onClick=\"return confirm('need to log in message');\"><img src='images/favorite_button.gif' alt='favorite button' width='90' height='40' border='0'></a>"; } .. rest of the code that makes up "thispage.php" where that same sql query works to display content on this page: for example: $tags = "$row[tags_text]"; ... the page ends exactly like this (no close brackets removed)... $favoritequiz = (int) $_GET['favorite_id']; if ($favoritequiz) { $favqtitle = mysql_real_escape_string($_GET['favorite_title']); $favqusername = mysql_real_escape_string($_GET['favorite_usern']); $favqphoto = mysql_real_escape_string($_GET['favorite_photo']); mysql_query("INSERT INTO favorites (favuser_id, favquiz_id, favtitle, favusername, favphoto) VALUES ('$session_userid', '$favoritequiz', '$favqtitle', '$favqusername', '$favqphoto')"); } include_once ("template.php"); ?> so basically, the user is on a page -- if he/she clicks "favorite" the important details are passed back to that same page and inserted into the database. I don't know where the sql is getting direction to enter or try to enter the data 4 times (as explained below). thank you again for your help, Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1258728 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Try doing something like this to help debug $query = "INSERT INTO favorites (favuser_id, favquiz_id, favtitle, favusername, favphoto) VALUES ('$session_userid', '$favoritequiz', '$favqtitle', '$favqusername', '$favqphoto')"; echo $query mysql_query( $query ); Check out what the query is actually outputting. Nothing you've posted leads me to believe it's that line of code that's inserting the blank entries though. Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1258732 Share on other sites More sharing options...
RyanSF07 Posted August 17, 2011 Author Share Posted August 17, 2011 thanks. I put this: if ($favoritequiz) { $favqtitle = mysql_real_escape_string($_GET['favorite_title']); $favqusername = mysql_real_escape_string($_GET['favorite_usern']); $favqphoto = mysql_real_escape_string($_GET['favorite_photo']); $query = "INSERT INTO favorites (favuser_id, favquiz_id, favtitle, favusername, favphoto) VALUES ('$session_userid', '$favoritequiz', '$favqtitle', '$favqusername', '$favqphoto')"; mysql_query( $query ); } echo $query; and get this: INSERT INTO favorites (favuser_id, favquiz_id, favtitle, favusername, favphoto) VALUES ('38', '8638', 'Compound words', 'Joseba', '1308040816-compound words.jpg') Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1258743 Share on other sites More sharing options...
PFMaBiSmAd Posted August 17, 2011 Share Posted August 17, 2011 It's likely that your page is being requested multiple times (or some of the code you didn't post also contains a similar INSERT query that is being executed three times.) It would certainly help if you told us if the first row or the last row is the correct one and posting the complete code for the page would help as that would allow someone to either confirm or eliminate the code as the cause of the problem. Do you have any URL rewriting or unusual domain forwarding/add-on domain that might be causing your page to be requested multiple times? Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1258755 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Even with multiple page requests, the $_SESSION variable isn't being carried, while the POST variables are. It doesn't make sense that the rouge request would send POST headers, but not cookie headers. Perhaps this is a client-side issue? Do you have other computers or friends that could try submitting the form for you? Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1258762 Share on other sites More sharing options...
PFMaBiSmAd Posted August 17, 2011 Share Posted August 17, 2011 If the URL's being requested were switching between www. and no-www. on the URL, you could have session variables with one request and no-session variables with other requests. Since all the variables I see are $_GET, they would be carried with the URL being requested and would not disappear after any one request. Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1258775 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Ah, didn't notice the $_GET, just assumed $_POST. Good catch. Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1258808 Share on other sites More sharing options...
RyanSF07 Posted August 18, 2011 Author Share Posted August 18, 2011 Hi and thank you again for your help. "It would certainly help if you told us if the first row or the last row is the correct one and posting the complete code for the page would help as that would allow someone to either confirm or eliminate the code as the cause of the problem." Looks like the first row -- but it's not consistent. Testing I see sometimes one row is added (correct), then again 2, then again 3 --random. "Do you have any URL rewriting or unusual domain forwarding/add-on domain that might be causing your page to be requested multiple times?" no I have an "id" field in the database set to autoincrement -- could that have anything to do with it? Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1258846 Share on other sites More sharing options...
PFMaBiSmAd Posted August 18, 2011 Share Posted August 18, 2011 You still haven't posted the complete code on the page you are having a problem with so that someone could A) see if it is doing something that is causing this problem, and B) duplicate the problem or not to see if this is something to do with your code, your browser, or your server. If the number of rows varies, this is still most likely due to the page being requested multiple times and a timing/race condition. Are you doing this on a live server or on a localhost development system? I would suggest a way of detecting and filtering out the excess (wrong/empty) data, but getting as many as 3 excess pieces of data indicates something is occurring that is not normal and should be found and fixed. If every time someone on your site clicks a link, there is the original request plus up to 3 more, that will make for a lot of unnecessary requests to the server (and we still don't know if the problem is actually your code executing multiple queries, perhaps due to a missing exit; statement after a header() redirect somewhere, because you have not posted enough of your code on the offending page for someone to see and/or duplicate the problem.) Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1259039 Share on other sites More sharing options...
RyanSF07 Posted August 18, 2011 Author Share Posted August 18, 2011 You are right -- fortunately this is a completely isolated incident. I got around it by moving the "favorite" processing script to a separate success page, which is working well (no random inserts) without sacrificing user experience. I really appreciate the help, and took as advice to try a different way. Thanks again, Ryan Quote Link to comment https://forums.phpfreaks.com/topic/245056-i-dont-want-those-3-other-rows-just-the-1/#findComment-1259075 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.