Forum Moderators: coopster

Message Too Old, No Replies

Problem with reload in php?

php script connects to a database and inserts input form data

         

pascy

10:14 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



Hello,

I have a php script which works fine: i connect to a database and insert the name and email of somebody.

My problem is, that after I got the message, that the data was stored successfully, and (for whatever reason) I press the reload-button of my browser, i got the following message:

"Error adding submitted contact information: Duplicate entry 'pascy@example.at' for key 1"

For me it is ok, that it is not possible to store the one email (also used as primary key in the database) a second time!
But I do not like that there is no data in the input form anymore, but pressing the submit-button a second time or the reload button causes an error tries to insert it a second, third,... time :-(

What is going on here (what keeps the data, if it is not visible anymore in the input field?), and what can I do to solve this problem?

here is my source:

<form action="<?=$PHP_SELF?>" method="post">
<table width=80 border=0>
<tr> Tell us your </tr>
<tr>
<td>name: </td>
<td><input type="text" name="name" size="20" tabindex=1></td>
</tr>
<tr>
<td>email: </td>
<td><align=center><input type="text" name="email" size="20" tabindex=2></td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="submit" name="subscribe" value="SUBMIT" tabindex=4 />
</td>
</tr>
</table>
</form>

<?php
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "#*$!", "#*$!");
if (!$dbcnx) {
echo( "<p>Unable to connect to the " .
"database server at this time.<br>" .
"Please bookmark us and come again" .
"later</p>" );
exit();
}

// Select the database
if (! @mysql_select_db("clients") ) {
echo( "<p>Unable to locate the clients " .
"database at this time.</p>" );
exit();
}

if ($subscribe == "SUBMIT") {
$sql = "INSERT INTO clients(name, email, joined, NL1) VALUES ('$name', '$email', now(),'y')";
if (@mysql_query($sql)) {
echo("<p>Your contact information has been added.</p>");
}
else {
echo("<p>Error adding submitted contact information: ".
mysql_error() . "</p>");
}
}
@mysql_close($dbcnx);

$mailtext = "";
$mailtext .="name : ".$name."\n";
$mailtext .="email : ".$email."\n\n";

$yws = "From: ";

mail($email,"Your subscription @.com",$mailtext,$yws);

$name=NULL;
$email=NULL;
$subscribe=NULL;

?>

Thanks for your advice

Pascy

[edited by: coopster at 11:58 am (utc) on Sep. 17, 2004]
[edit reason] generalized email address [/edit]

Zipper

11:49 pm on Sep 16, 2004 (gmt 0)

10+ Year Member



Not sure as to what your question is regarding "no data in the input form anymore" & "if it is not visible anymore in the input field?" I'm probably guessing you're confused with the way http headers work.

Anyway, to avoid the error message run a query to check whether the e-mail aready exists in the db. e.g:

$sql = "SELECT FROM clients WHERE email = '$email'";
$result = mysql_query($sql);
if(mysql_num_rows($result) == 1){
//the e-mail is already in the db
} else P
//add contact details
}

coopster

12:05 pm on Sep 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, pascy!

You may also want to simply display the email address rather than place it in an input field after it has been added to the database. On the server-side you will still want to run the edit as described.