Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert into: Resetting the value

Status
Not open for further replies.

aas1611

Programmer
Dec 14, 2001
184
DE
I wrote a PHP script that insert data into a database. After the user click submit, the page goes to the default setup. But on the database, the data is inserted twice. So I figured that I have to set these data (PHP variables) into null or something after inserting. But I don't know how.

For example, a user has typed John Doe under Name field and click button "Submit" only once. Then the page goes back to default (The Name entry field is blank). In the database table, this "John Doe" appears twice.

Suggestions?
 
You must be running the query with the INSERT sentence twice. Or reloading the page that does the insert. Without the code, that is all the information we can give you. You could query the database first to see if record exists and if it does, skip the inserting. But first we need to determine why is it inserting twice in the first place.
 
It inserts the same data twice when I refresh the page. Here is my code:

<?
include 'header.htm';
include 'db_verbinden.php';
?>

<h2> Bankdaten eintragen </h2>


<form action="eingabe.php" method="post">

<input type="text" size="20" maxlength="20" name="blz_input">
<input type="text" size="20" maxlength="20" name="name_input">
<br>
BLZ
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
Name der Bank
<br>
<br>
<input type="text" size="20" maxlength="20" name="blzhaupt_input">
<br>
BLZ der Hauptstelle
<br>
<br>
<input type="text" size="20" maxlength="20" name="str_input">
<input type="text" size="5" maxlength="5" name="strnr_input">
<br>
Strasse
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
Nr.
<br>
<br>
<input type="text" size="5" maxlength="5" name="plz_input">
<input type="text" size="20" maxlength="20" name="ort_input">
<br>
PLZ &nbsp &nbsp &nbsp &nbsp Ort
<br>
<br>
<input type="text" size="20" maxlength="20" name="tel_input">
<input type="text" size="20" maxlength="20" name="fax_input">
<br>
Tel.
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
Fax
<br>
<br>
Ansprechspartner: &nbsp
<comment> <input type="text" size="5" maxlength="5" name="anrede_input">
</comment>
<select name = "anrede_input" size = "1">
<option selected> Frau
<option> Herr
</select>
<input type="text" size="20" maxlength="20" name="nachname_input">
<input type="text" size="20" maxlength="20" name="vorname_input">
<input type="text" size="20" maxlength="20" name="email_input">
<br>
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
Anrede
&nbsp &nbsp
Nachname
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
Vorname
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
E-Mail
<br>
<p><input type="submit" name="submit" value="Eintragen"><p>
</form>


<?

$blz = $_POST["blz_input"];
$blzhaupt = $_POST["blzhaupt_input"];
$name = $_POST["name_input"];
$str = $_POST["str_input"];
$strnr = $_POST["strnr_input"];
$plz = $_POST["plz_input"];
$ort = $_POST["ort_input"];
$tel = $_POST["tel_input"];
$fax = $_POST["fax_input"];
$anrede = $_POST["anre_input"];
$nachname = $_POST["nachname_input"];
$vorname = $_POST["vorname_input"];
$email = $_POST["email_input"];

if (!empty($blz)) {
$query = " insert into bank values ('$blz', '$blzhaupt', '$name', '$str', '$strnr', '$plz', '$ort', '$tel', '$fax', '$anrede','$nachname','$vorname','$email')";
$result = mysql_query($query);
}


mysql_close($verbindung);

?>


 
I do not understand. If you refresh the page that does the insert, code will be run again and the data will get inserted again. Isn't that what you would expect. Personally, if I would want to prevent that, I would run a select query on the database and check if the record with the fields you're just trying to insert exists. If you get more than 0 results, you don't insert and output information that writing to the table was aborted since record with this information already exists in the database.
 
In cases like this I recommend to use some client side scripting to prevent the resubmission of the same content. A hidden field that is set when the page was posted can be checked in an onSubmit event handler and trigger an alert.

Alternatively checking for existence of a record with the same data as Vragabond suggested.

Just as a note:
There are better ways to position the labels to your fields. The &nbsp; accumulation (from a web prospective) is a really bad solution.

Das kann man schon viel besser machen! Wie wäre es mit einer Tabelle?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top