i have created a registration page. when user fill it and submit a unique user_id will be given him. but problem is when user_id has been given if user refresh the page a new user_id will be alloted to him. if he refresh the page for thousand time a new uer_id will be alloted to him each time. how can i stop refreshing page or alloting new user_id.

my php code is :

<?php

if (isset($_POST)) {

$name=$_POST;
$dob=$_POST;
$address=$_POST;
$city=$_POST;
$state=$_POST;
$country=$_POST;
$pin=$_POST;
$mail=$_POST;
$web=$_POST;
$sex=$_POST;
$contact=$_POST;

$con = mysql_connect("********","*******","********");

if (!$con)
{
echo "Sorry, Server is not Responding this time, So Please Try After some time";
}

mysql_select_db("*****", $con);


$sql="INSERT INTO ******(name, dob, address, city, state, country, pin, mail, web, sex, contact)

VALUES

('$name','$dob','$address','$city','$state','$country','$pin','$mail','$web','$sex','$contact')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else {
$result = mysql_query("SELECT user_id, name FROM ****** WHERE dob = '$dob' AND name ='$name'");

echo "<table border='1'>
<tr>
<th>User_ID</th>
<th>Name</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row . "</td>";
echo "<td>" . $row . "</td>";
echo "</tr>";
}
echo "</table>";

}
}

mysql_close($con);

?>

When your data is inserted to table you should redirect user to new page.

header("Location:success.php");
exit;

Because if you wont then when you press refresh form will be again submitted and again data will be inserted to table.

So its better practice to use header when php code is completed.

yaa..it is the way as vibhadevit said. if you want with in that page then you write select query above the insert query.
for example in your form user name should be unique. then you write select query like this. if it is true then insert the value.

$select=mysql_query("select * from table where username='$username'");
$count=mysql_num_rows($select);
if($count==0)
{
write insert query here
}

Thanks all of you to help me. but it cant solve my problem . actually i have created two sql tables one for Registration and second for username & password. when some one fill the registeration form his information will stored in registration table and a username & password will copied into Registration table from second table which have lots of username and password already created.

if i redirect user to other page how can i show him ,his own username and password. because i dont have any input from user on the redirected page.

Please help me solve it.

Thanks

When you want to get access the user input on the same page use "GET" method instead of "POST" method and use isset($_GET), I am new to php and recently I have started working on php, so I have jus suggested my taught

Here is some logic:

1) in register.php give all fielsa like name, address, city, state..etc.
2) When form is submitted store all values in table and get last inserted id $lastid with mysql_insert_id.
3) write header now.

header("Location:success.php?lastid=".$lastid);
exit;

4) now on success.php you have $lastid = $_GET.
you can have one select query where id = $lastid. and you can show all information.

start a session at the top of script.

then wrap the form in an if statement to check if form has been processed.
put new session variable in form script.

<?PHP
session_start()
if (!isset($_SESSION['form'])){
//form stuff
$_SESSION['form'] = 'complete';
}
?>

if you wish to tell the user form has already been processed:

else {
$refresh = 'true'
}
if ($refresh){
echo 'You may only submit this form once.';
}

Hope this helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.