justlukeyou Posted December 26, 2012 Share Posted December 26, 2012 Hi, I have the following code which only fully works when I refresh the page. When I click the button it only inserts the $followerid into the database. However when I press F5 to refresh the page it inserts both the $profileid and $followerid It seems quite strange and I haven't seen an issue like this before. It some sort of silly mistake does anybody know? <?php $followerid = $_SESSION['userID']; $profileid = $row['id']; $error = ""; if($error == "") { // other checks here to determine various ID's are numeric, etc. $sql = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES ('" . $profileid . "', '" . $followerid . "')"; if (!mysql_query($sql)) { if (mysql_errno($link_identifier) == 1062) { //$link_identifier is necessary to avoid conflicting error notices due to multiple openning/closing SQL connections // duplicate attempt to follow // handle accordingly } } } ?> </div> <?php echo $profileid; ?> Profile ID <br> <?php echo $followerid; ?> Follower ID <br> </div> <div class="followbutton"> <a href="<?php echo $_SERVER['PHP_SELF']; ?>"><img src="/images/follow.png" id="followbutton" class="submit-button"/></a> </div> Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/ Share on other sites More sharing options...
Psycho Posted December 26, 2012 Share Posted December 26, 2012 It's impossible to know what the problem is based upon what you have provided. For example, where is $row['id'] defined? But, the more important thing is that the little error handling there is provides no feedback. So, if there was a problem you have no way to know what it is. You are defining $followerid as $_SESSION['userID'], but what if the session wasn't started (I actually think the problem is something to do with sessions)? Then, $followerid would have a 'false' value and the query would fail. But, there is nothing to check if $_SESSION['userID'] is defined and if the query fails there is nothing to provide an error. Also, I'm not sure if you stripped out some code or not, but this makes no sense: $error = ""; if($error == "") { Why would you set $error to an empty string immediately followed by a condition check to see if it is an empty string? Based upon what I see in that code I think you may be running queries in loops - which is a bad idea, but since there is not enough information provided I can't provide a solution for that. Give this code a try to at least pinpoint the problem. <?php $errors = array(); //Validate the input vars if(!isset($_SESSION['userID'])) { $errors[] = "\$_SESSION['userID'] value is not set."; } if(!isset($row['id'])) { $errors[] = "\$row['id'] value is not set"; } if(!count($error)) { //Validation of input vars passed, attempt to run query //Force vars to be ints to be safe for query statement $followerid = intval($_SESSION['userID']); $profileid = intval(($row['id']); $query = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES ('{$profileid}', '{$followerid}')"; $result = mysql_query($query); if (!$result) { $errors[] = "Query: {$query}<br>Error: " . mysql_error(); } } if(count($errors)) { echo "The following errors occured:\n"; echo "<ul>\n"; foreach($errors as $rr) { echo "<li>{$err}</li>\n"; } echo "</ul>\n"; } ?> </div> <?php echo $profileid; ?> Profile ID <br> <?php echo $followerid; ?> Follower ID <br> </div> <div class="followbutton"> <a href="<?php echo $_SERVER['PHP_SELF']; ?>"><img src="/images/follow.png" id="followbutton" class="submit-button"/></a> </div> Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401336 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 Sorted it. I should have noticed the problem. When I press F5 the it remains on the profile of the person I want to follow so that persons ID nubmer enters into the database. If press the button it reloads the page but without the ID of the person I want follow so I get a blank page. All I had to do was to echo the profileid into a query on the link. This now gives me what I want because it enters the two IDs into the database and relaods the page of the profile I wanted to follow. <a href="<?php echo $_SERVER['PHP_SELF']; ?>?ID=<?php echo $profileid; ?>"><img src="/images/follow.png" id="followbutton" class="submit-button"/></a> Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401339 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 Is there anyway I can make it so that the code only runs when I click the button. if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){ Should I be using the name from the button? Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401346 Share on other sites More sharing options...
Psycho Posted December 26, 2012 Share Posted December 26, 2012 Is there anyway I can make it so that the code only runs when I click the button. As opposed to what? If either of the IDs are not set, don't execute the code. If you are wanting someone to prevent using Refresh to insert a new record there are several options. Here are two: 1. After the record is added, redirect the user to a different (or the same) page using a header() function. This will wipe out any GET/POST values. 2. Put in logic to prevent a duplicate records Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401349 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 (edited) Hi, Is there a way to only allow the code to run if the button is pressed? I thought I could use this. I thought it said that the followbutton has to be clicked? if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){ Im going to add a duplicate logic. I dont know how to do this but I am trying to find out with another page. I shall also add one so that a user cant follow themselves. Although the button will be removed if the user is logged in and viewing their own profile. Edited December 26, 2012 by justlukeyou Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401359 Share on other sites More sharing options...
Pikachu2000 Posted December 26, 2012 Share Posted December 26, 2012 And what did you see when you print_r()'d the $_POST array? Was the value of the button in it? Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401364 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 1. After the record is added, redirect the user to a different (or the same) page using a header() function. This will wipe out any GET/POST values. Apologies mate, what is this function. Im baffled that a form can run just by refreshing the page. Currently if somoene is not logged in it inserts 0 into the database and if someone is logged in they follow the user just by refreshing the page. Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401366 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 And what did you see when you print_r()'d the $_POST array? Was the value of the button in it? Hi, not totally sure what you mean tired and frazzled after Christmas. I echoed it and it came back with this. Warning: print_r() expects at least 1 parameter, 0 given in /home/website.com/test/profileinserttest.php on line 322 Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401367 Share on other sites More sharing options...
Barand Posted December 26, 2012 Share Posted December 26, 2012 print_r Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401372 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 Thanks, What does it do? I tried it and echoes what is in the database. What is it that Im looking for? Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401374 Share on other sites More sharing options...
Pikachu2000 Posted December 26, 2012 Share Posted December 26, 2012 What does it do? From the manual: print_r (PHP 4, PHP 5) print_r — Prints human-readable information about a variable print_r ( mixed $exp<b></b>ression [, bool $return = false ] ) print_r() displays information about a variable in a way that's readable by humans. print_r(), var_dump() and var_export() will also show protected and private properties of objects with PHP 5. Static class members will not be shown. Return Values If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects. When the return parameter is TRUE, this function will return a string. Otherwise, the return value is TRUE. Example #1 print_r() example <pre> <?php $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z')); print_r ($a); ?> </pre> The above example will output: <pre> Array ( [a] => apple [b] => banana [c] => Array ( [0] => x [1] => y [2] => z ) ) </pre> Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401375 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 What does that mean? Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401377 Share on other sites More sharing options...
Pikachu2000 Posted December 26, 2012 Share Posted December 26, 2012 Read it. Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401378 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 Hi, When I print the variables it just prints the values from the database. It doesn't print any thing like this: Prints human-readable information about a variable Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401380 Share on other sites More sharing options...
Pikachu2000 Posted December 26, 2012 Share Posted December 26, 2012 Yes it does. Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401381 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 How come it just printed the contents from the DB? Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401384 Share on other sites More sharing options...
Pikachu2000 Posted December 26, 2012 Share Posted December 26, 2012 The mere fact that you recognize it as being content from the database confirms that is is indeed human readable. Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401385 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 How is it useful? Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401390 Share on other sites More sharing options...
Pikachu2000 Posted December 26, 2012 Share Posted December 26, 2012 I must refer you to reply #7 in this very thread. Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401394 Share on other sites More sharing options...
Barand Posted December 26, 2012 Share Posted December 26, 2012 How come it just printed the contents from the DB? It should be showing the content of $_POST array if you used the suggested code. How is it useful? When you start doing your own debugging, instead of asking everyone else to do it for you, you will realise just how useful it is. Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401396 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 (edited) What do you mean? Im not using a post array am I? Edited December 26, 2012 by justlukeyou Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401398 Share on other sites More sharing options...
Barand Posted December 26, 2012 Share Posted December 26, 2012 I must refer you to reply #7 in this very thread. Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401399 Share on other sites More sharing options...
justlukeyou Posted December 26, 2012 Author Share Posted December 26, 2012 But is there a 'bug'? Im now trying to find out how to stop the code from running when the page is refreshed. Is that related? Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401401 Share on other sites More sharing options...
justlukeyou Posted December 27, 2012 Author Share Posted December 27, 2012 Hi, Does anyone have any suggestions. Im not using a POST function. Should I be using this. Should I be using something like this. Whenever I try to use it creates one of many errors. Should I be using it at all? if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){ Quote Link to comment https://forums.phpfreaks.com/topic/272373-code-only-fully-works-on-page-refresh/#findComment-1401581 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.