Jump to content

Recommended Posts

Hi all,

I have recently started learning PHP and had been good till I reached forms. This is where I am stuck up -

I have a simple contact me form that has 4 fields. After submitting the form I call a php script to update the data in a MySQL database. This part is ok. But the problem is that after update of the database, in case I click the refresh button, the data on the form gets resubmitted - that is after displaying data updated successfully, in case I refresh the page, the data just entered on the form is resubmitted and updated in the database. Any advice that I should try out?

 

Thanks for the advice in advance :)

 

Code of php script that gets called is

<?

$post_vars = $HTTP_POST_VARS;

$uname    = $post_vars['uname'];

$email    = $post_vars['email'];

$related  = $post_vars['related'];

$feedback  = $post_vars['msg'];

$flag = true;

//Validate the Input

//........

if($flag)

{

//connect to the database

//Create the Query/ Insert Statement

$ins = "INSERT INTO feedback (";

$flds = "name, email, relatedto, feedback) VALUES(";

$vals = '"' . $uname . '","' . $email . '","' . $related . '","' . $feedback . '")';

$statement = $ins . $flds . $vals;

//Execute the statement

$result=mysql_query($statement);

if($result)

{

print("Data Updated Successfully");

$flag=true;

}

else

{

die("Could not insert the data in the database... <br />" . mysql_error());

$flag=false;

}

mysql_close($connection);

}

?>

 

 

have your script check if info exists in db already, before you run your insert.

 

pseudo code:

 

select * from table where name = '$uname' or email = '$email'

if (mysql_num_rows > 0) {

  echo "you already registered!"

} else {

  insert ...

}

have your script check if info exists in db already, before you run your insert.

 

pseudo code:

 

select * from table where name = '$uname' or email = '$email'

if (mysql_num_rows > 0) {

  echo "you already registered!"

} else {

  insert ...

}

 

Tx Crayon Violent. I understand that ths needs to be done. But as for this form, it is a feedback form and in such a case the fields $uname and $email may be duplicated. Yes, I can try for the $msg(feedback's) content getting duplicated but what i want is the problem to be solved at the client's system, that is, in case the browser / visitor presses F5 or refresh button on toolbar, the resend thing should not happen et all. Hope this is not asking too much from PHP.

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.