473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem inserting data into mysql database from a form...

vikas1111
122 100+
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....
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. $con = mysql_connect("localhost","root","rootwdp");
  4. if (!$con)
  5.   {
  6.   die('Could not connect: ' . mysql_error());
  7.   }
  8. mysql_select_db("sample", $con);
  9. $sql="INSERT INTO usttable (usr_login,usr_password,usr_name,usr_email)
  10. VALUE ('$_POST[fname]','$_POST[password]','$_POST[fname]','$_POST[email]')";
  11. if (!mysql_query($sql,$con))
  12.   {
  13.   die('Error: ' . mysql_error());
  14.   }
  15. mysql_close($con)
  16. ?>
  17.  
May 24 '08 #1
4 1724
hsriat
1,654 Expert 1GB
Apply validations before you insert the data to the database.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //redirect to the form page if not submitted
  4. if (!isset($_POST['submit']))
  5. header ("Location:login_form.php");//which ever page is submitting to this script.
  6.  
  7. //if any of the parameter is missing.
  8. if (!isset($_POST[fname]) || !isset($_POST[email]) || !isset($_POST[password]))
  9. die ("Invalid parameters");
  10.  
  11.  
  12. //---- your code ---
  13. $con = mysql_connect("localhost","root","rootwdp");
  14. if (!$con)
  15.   {
  16.   die('Could not connect: ' . mysql_error());
  17.   }
  18. mysql_select_db("sample", $con);
  19. $sql="INSERT INTO usttable (usr_login,usr_password,usr_name,usr_email)
  20. VALUE ('$_POST[fname]','$_POST[password]','$_POST[fname]','$_POST[email]')";
  21. if (!mysql_query($sql,$con))
  22.   {
  23.   die('Error: ' . mysql_error());
  24.   }
  25. mysql_close($con)
  26. ?>
  27.  
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).
May 24 '08 #2
thanks for the tips...
i have the same problem to.....
^_^
May 25 '08 #3
vikas1111
122 100+
hi hsriat

thanks its working........
May 29 '08 #4
hsriat
1,654 Expert 1GB
You are welcome... :)

I'm glad that one reply helped both of you.
May 29 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
6
by: Murtix Van Basten | last post by:
HI all, I am trying to migrate a database from mysql to mssql2k. I use myODBC to connect to mysql server to pull the database from DTS and insert in to sql server. But in mysql server, there is...
3
by: Materialised | last post by:
Hello everyone, I am having a issue inserting values into a MYSQL table, and for the life of me, I can figure out why. I know the connection is successful, however I am getting errors. The...
5
by: hfk0 | last post by:
Hi, I'm new to ASP.net, SQL Server and visual studio.net, and I'm having problem inserting and storing data from a web form to a SQL database. I created a simple ASP.NET web form, a simple SQL...
4
by: Nate12o6 | last post by:
Mabee you guys can help me with this. I have a form with enctype="multipart/form-data" that has a textarea in it as well as file upload. The text area is for a description of the file. If...
10
by: farukcse | last post by:
Dear Sir, i am facing a encoding problem with MySQL data base and phpMyAdmin. i developed a form . From wher user can insert the Car plate No. to the MySQL database.Both of the php file for...
2
by: Ravigandha | last post by:
Hello everybody, My question is how to insert special characters and symbols in Mysql5 database and how to retrieve them from database in php. Here i am inserting some data from a form,by post...
3
by: len | last post by:
Hi All I have started a little pet project to learn python and MySQL. The project involves figuring out all the combinations for a 5 number lottery and storing the data in a MySQL file. The...
6
by: Bunty | last post by:
I want to insert values in the database.If i insert values one by one then it works till 4 or 5 fields then after it gives error.In my database there are more than 20 field.Pls help me.
4
by: liberty1 | last post by:
Hi everyone. I appreciate your effort at helping newbies like me. I have the following problems and will appreciate urgent help. PROBLEM NUMBER 1: Using PHP and MySQL, I am able to upload...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.