problem on page refresh
Posted: Sat Aug 08, 2009 6:02 pm
hello
im very new to php coding
having some problem hope you can help me
i have form with text input field which take the username and and the php codes for this is in the same page as the form and then the php code will do its job
the php code do its job as it is supposed to do but the problem is for the first time that the page loads everything is ok but if i put anything in my form input box ( the username i want ) and submit the php code will the desired job but everytime i refresh the page or revist it the same username will be added to database though there is nothing in the input filed and this happens until i enter new username and then again on every page refresh then the new username start adding to databse
hope i have explained clearly
this my code i tried unset or ob_start and end_clean but nothing i tried if not empty too but still nothing
im very new to php coding
having some problem hope you can help me
i have form with text input field which take the username and and the php codes for this is in the same page as the form and then the php code will do its job
the php code do its job as it is supposed to do but the problem is for the first time that the page loads everything is ok but if i put anything in my form input box ( the username i want ) and submit the php code will the desired job but everytime i refresh the page or revist it the same username will be added to database though there is nothing in the input filed and this happens until i enter new username and then again on every page refresh then the new username start adding to databse
hope i have explained clearly
this my code i tried unset or ob_start and end_clean but nothing i tried if not empty too but still nothing
Code: Select all
<form action="" method="post" name="username">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
<?php
ob_start() ;
$username= $_POST['username'];
if (!empty ($username)) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
//select from database
mysql_select_db("amir", $conn);
$add = mysql_query ("INSERT INTO users (username)
VALUES ('$username')");
if ($add == true )
{echo 'done'; } ;}
unset ($username);
ob_end_flush();
ob_end_clean ();