problem inserting data into mysql database from a form...  | Member | | Join Date: Feb 2008 Location: 12° 18' N, 76° 42' E
Posts: 118
| |
Hi All..
While putting data into database from form if i refresh the php form a blank data will be added into database ... How can i remove that bug???
Here is my code.... -
-
<?php
-
$con = mysql_connect("localhost","root","rootwdp");
-
if (!$con)
-
{
-
die('Could not connect: ' . mysql_error());
-
}
-
mysql_select_db("sample", $con);
-
$sql="INSERT INTO usttable (usr_login,usr_password,usr_name,usr_email)
-
VALUE ('$_POST[fname]','$_POST[password]','$_POST[fname]','$_POST[email]')";
-
if (!mysql_query($sql,$con))
-
{
-
die('Error: ' . mysql_error());
-
}
-
mysql_close($con)
-
?>
-
|  | Expert | | Join Date: Jan 2008 Location: Bath, UK
Posts: 1,609
| | | re: problem inserting data into mysql database from a form...
Apply validations before you insert the data to the database. -
<?php
-
-
//redirect to the form page if not submitted
-
if (!isset($_POST['submit']))
-
header ("Location:login_form.php");//which ever page is submitting to this script.
-
-
//if any of the parameter is missing.
-
if (!isset($_POST[fname]) || !isset($_POST[email]) || !isset($_POST[password]))
-
die ("Invalid parameters");
-
-
-
//---- your code ---
-
$con = mysql_connect("localhost","root","rootwdp");
-
if (!$con)
-
{
-
die('Could not connect: ' . mysql_error());
-
}
-
mysql_select_db("sample", $con);
-
$sql="INSERT INTO usttable (usr_login,usr_password,usr_name,usr_email)
-
VALUE ('$_POST[fname]','$_POST[password]','$_POST[fname]','$_POST[email]')";
-
if (!mysql_query($sql,$con))
-
{
-
die('Error: ' . mysql_error());
-
}
-
mysql_close($con)
-
?>
-
And make sure you add name="submit" to the submit button.
I would suggest you to do the same validations in PHP before you insert data, which you do in JavaScript onsubmit of form (if you are doing any).
| | Newbie | | Join Date: May 2008 Location: malaysia
Posts: 10
| | | re: problem inserting data into mysql database from a form...
thanks for the tips...
i have the same problem to.....
^_^
|  | Member | | Join Date: Feb 2008 Location: 12° 18' N, 76° 42' E
Posts: 118
| | | re: problem inserting data into mysql database from a form...
hi hsriat
thanks its working........
|  | Expert | | Join Date: Jan 2008 Location: Bath, UK
Posts: 1,609
| | | re: problem inserting data into mysql database from a form...
You are welcome... :)
I'm glad that one reply helped both of you.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,537 network members.
|