0

I'm wondering what is causing my search results to still show after the user has refreshed the page and how I could fix it. PHP Code:

$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($db_database, $con);

if (!empty($_REQUEST['term'])) {

$term = mysql_real_escape_string($_REQUEST['term']);     

$sql = "SELECT * FROM FUMUKU WHERE TimeD LIKE '%".$term."%'"; 
$r_query = mysql_query($sql); 

while ($row = mysql_fetch_array($r_query)){  
echo '<br /> Stock: ' .$row['Stock'];  
echo '<br /> Price: ' .$row['Price'];  
echo '<br /> Time: '.$row['TimeD'];   
}  

HTML Code:

<form action="" method="post">  
Search: <input type="text" name="term" /><br />  
<input type="submit" value="Submit" />  
</form>  
PaulB12345
  • 15
  • 5
  • I think you are refreshing throug `f5` and it will asking you to submit form again (in a pop-up) and you are clicking on yes. try to refresh your page url by first clicking on it and then press enter.. – Alive to die - Anant May 01 '16 at 12:56
  • 1
    What does the url look like? as $_REQUEST can be POST,GET or COOKIE. Do you refresh by "still sending post data" as some browsers usually ask, so it is possible that is happening – user3252497 May 01 '16 at 12:56
  • Don't use `mysql_*` functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`pdo`](http://php.net/manual/en/book.pdo.php) instead. [And this is why you shouldn't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Rajdeep Paul May 01 '16 at 13:00

1 Answers1

1

Your PHP script executes when the page loads. To prevent this, you need to add if(isset($_POST['submit'])) { at the top of your PHP code and add name="submit to your submit button.

<?php 
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con) {
 die('Could not connect: ' . mysql_error());
}

if(isset($_POST['submit'])) {
if (!empty($_REQUEST['term'])) {

$term = mysql_real_escape_string($_REQUEST['term']);     

$sql = "SELECT * FROM FUMUKU WHERE TimeD LIKE '%".$term."%'"; 
$r_query = mysql_query($sql); 

while ($row = mysql_fetch_array($r_query)){  
echo '<br /> Stock: ' .$row['Stock'];  
echo '<br /> Price: ' .$row['Price'];  
echo '<br /> Time: '.$row['TimeD'];   
}  
}
}  
?>

<form method="post">  
Search: <input type="text" name="term" /><br />  
<input type="submit" name="submit" value="Submit" />  
</form> 
The Codesee
  • 3,714
  • 5
  • 38
  • 78