Jump to content

I don't want those 3 other rows, just the 1.


RyanSF07

Recommended Posts

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,

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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')

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

 

 

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.