|  | Posted by J.O. Aho on 01/23/07 18:54 
Tom wrote:> On Sun, 21 Jan 2007 14:16:01 -0000, egibberate wrote...
 >> Hi,
 >>
 >>       complete novice I'm afraid. When I input data into the form
 >>
 >>      and click the submit button, the data is sent to the database twice ?
 >>
 >>     ie. two identical records are logged into the table. Can anyone spot
 >> the
 >>
 >>     flaw in the code below?  Thanks in anticipation.
 >>
 >> <?
 >>
 >> if ($_POST) {
 >>
 >> $msg = "";
 >>
 >> foreach($_POST as $k => $v) {
 >>
 >> $v = trim($v) ;
 >>
 >> $$k = $v ;
 >>
 >> if ($v=="") {
 >>
 >> $msg = "Please fill in both fields";
 >>
 >> }
 >>
 >> }
 >>
 >> if ($msg=="") {
 >>
 >> $insert = "INSERT INTO tblshop_name
 >>
 >> (strshop_name, intshopcentre)
 >>
 >> VALUES ('$sn','$sc')";
 >>
 >> $link = mysqli_connect('localhost','root','blablabla','blablabla');
 >>
 >> if (!mysqli_query($link, $insert)) {
 >>
 >> $msg = "Error inserting data";
 >>
 >> } else {
 >>
 >> $msg = "Record successfully added";
 >>
 >> $sn = $sc = "";
 >>
 >> }
 >>
 >> mysqli_close($link);
 >>
 >> }
 >>
 >> echo "<div class=\"error\">$msg</div>";
 >>
 >> } else {
 >>
 >> $sn = $sc = "";
 >>
 >> }
 >>
 >> ?>
 >>
 >> <FORM METHOD="post" ACTION="<? echo
 >>
 >> $_SERVER['PHP_SELF'] ?>">
 >>
 >> <TABLE BORDER="1" CELLPADDING="5">
 >>
 >> <TR>
 >>
 >> <TH>Shop Name</TH>
 >>
 >> <TH>Retail Park</TH>
 >>
 >> </TR>
 >>
 >> <TR>
 >>
 >> <TD><INPUT TYPE="text" NAME="sn" VALUE"<?
 >>
 >> echo $sn ?>" /></TD>
 >>
 >> <TD><INPUT TYPE="text" NAME="sc" VALUE="<?
 >>
 >> echo $sc ?>" /></TD>
 >>
 >> </TR>
 >>
 >> </TABLE>
 >>
 >> <BR/>
 >>
 >> <INPUT TYPE="submit" VALUE="Add to database" />
 >>
 >> <INPUT TYPE="reset" VALUE="Cancel" />
 >>
 >> </FORM>
 >>
 >>
 >
 > I normally use the "mysql_query" function. Might be worth trying that instead of
 > the "mysqli_query" function and see if that makes a difference.
 
 There aren't any difference between those two functions, his problem was that
 he hit the reload button after have submitted the form, as he thought it would
 refresh the form.
 
 
 --
 
 //Aho
  Navigation: [Reply to this message] |