Jump to content

Code Only Fully Works On Page Refresh


justlukeyou

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by justlukeyou
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

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.