how to insert data into mysql db using form in php database enter code
Post Description: how to insert data into mysql db using form in php database enter code MYSQL
Post Tags:
This Post Has Been Viewed 17249 Times Since Mon Feb 19, 2007 5:03 pm Author
hostman with 57 replies
how to insert data into mysql db using form in php database enter code
Advertise On This Page
ok, when i started to learn about php, i wanted to know how i can put that data from a HTML form into mysql database. it wasn't easy to lear because there was so much stuff i need to know. so to help you, make sure you have these things so you will be able to learn my tutorial here on how to insert data into mysql from a PHP web form.
1. Knowledge of HTML (average knowledge)
2. Knowldege of PHP (average knowledge)
3. Knowledge of Mysql (average knowledge)
4. Have a website with PHP (if you don't have a PHP/mysql website, visit our friends at www.webune.com and signup for a PHP/mysql web hosting plan)
now that you have at least all the four points above, its time to show you how you can insert the data.
Step 1
ok, i will give you a short tutorial...
In this example, lets say i want to collect data from a user. For the purpose of this tutorial guide, i only need the user's name and their email address. so i only need two text fields. one is user_name and the second one is user_email. so this is how the form would look like in HTML:
this is how the form will look like:
FORM HTMLCODE
<form method="post" action="">
Name:<br>
<input type="text" name="user_name">
<br>
Email: <br>
<input type="text" name="user_email">
<br><br>
<input type="submit" name="Submit" value="Submit">
</form>
STEP 2
the next step would be to have php check if the form has been submited or not. if the form has been submitted..
the way you can tell if a form has been submitted is to check if our submit button has a value.. if you look closely in our form html. i have this
HTML CODE
<input type="submit" name="Submit" value="Submit">
as you can see, the name of the submit button is called "Submit" so that the string variable name we can find out if its true or false. to check if the form is submitted i will use the if else operator in php
PHP CODE
if (isset($_REQUEST['Submit'])) {
// INSERT DATA FROM FORM ONCE THE FORM HAS BEEN SUBMITTED
} else {
// DISPLAY FORM IF FORM HAS NOT BEEN SUBMITTED
}
Step 3
ok, on step three, you will need to have the following information, if you dont have this information,,, you will not be able to continue on with this tutorial
- hostname (usually localhost)
- database user name (if you don't have this contact your host company)
- database user password (if you don't have this contact your host company)
- database name (if you don't have this contact your host company)
- database table name (you create this. you can create a table in phpmyadmin)
if you dont have all these requirements contact you webhosting company.
if you are a Webune customer, contact us and we will be glad to provide this information for you.
Step 4
the next step involves in us creating some tables in the mysql database.
so login to your database using phpmyadmin. if you are a webune customer, login to the control panel and click on the phpmyadmin link, you will need to login to the database in step 3.
once you are in phpmyadmin click on the SQL tab and copy and past this sql dump into the text area:
CREATE TABLE `user_info` (
`user_name` VARCHAR( 50 ) NOT NULL ,
`user_email` VARCHAR( 50 ) NOT NULL
);
this will create a table called user_info in your database, this is where we will enter the information from the form. as you can see, we created two fields, one called user_name and the other one called user_email
its should look something like this on your phpmyadmin
now its time to put the whole thing together.. we wrote up this simple script to show you how you can add. we made this script simple so that you can hack it, modify it or do whatever you want with it so you can practice and how how it all works. one this we ask is that please dont remove the www.webune.com link - we would appreciate it if you dont.
so open your favorite text editor. if you have windows like i do, im using windows xp, open a blank notepad and copy and paste the following php code into it:
webune.php<?php
####################################################################
# THIS SCRIPT CREATED BY WWW.WEBUNE.COM
# PLEASE DONT ERASE THIS
###################################################################
####################################################################
################ DATABASE CONFIGURE ##############################
####################################################################
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "username"; // change to your database password
$db_password = "passwd"; // change to your database password
$database = "databse"; // provide your database name
$db_table = "user_info"; // leave this as is
# STOP HERE
####################################################################
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
?>
<html>
<head>
<title>How To Insert Data Into MySQL db using form in php</title>
</head>
<body>
<?php
if (isset($_REQUEST['Submit'])) {
# THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE
$sql = "INSERT INTO $db_table(user_name,user_email) values ('".mysql_real_escape_string(stripslashes($_REQUEST['user_name']))."','".mysql_real_escape_string(stripslashes($_REQUEST['user_email']))."')";
if($result = mysql_query($sql ,$db)) {
echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="http://www.webune.com/images/headers/default_logo.jpg"';
} else {
echo "ERROR: ".mysql_error();
}
} else {
?>
<h1>How To Insert Data Into MySQL db using form in php</h1>By <a href="http://www.webune.com">Webune.com</a><hr>
<form method="post" action="">
Name:<br>
<input type="text" name="user_name">
<br>
Email: <br>
<input type="text" name="user_email">
<br><br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
}
?>
</body>
</html>
Now make sure to change the mysql connections settings. so for example, i have these settings. NOTE: this are fake, so make sure to change them, otherwise, this script will error out because it cant connect to the database.
$hostname = "localhost";
$db_user = "webune_user";
$db_password = "mypassword";
$database = "database";
$db_table = "user_info";
as you can see on my example, my hostname is localhost
my database username is webune_user
the password for my database is mypassword my database is database
and my database table is user_info
so now that you have changed these setting to your database credentials, save the notepad file as
webune.php
now upload to your php website and open it with your browser, enter the information and once you get a sucess message, the information was entered into the datase.
i tested and it works. if you are a webune customer, you should not have any problems, since this test is done on our servers
here is how the form looks like in my firefox browser when i open webune.php
and when i submit the form with my name and email address i get this
now when i go to my phpmyadmin, i can confirm that the information i've entered on the form, was inserted into my database
NOTE: you should not by any means deploy this code in a real production website, as there are many security issues, like sql injection. the purpose of this tutorial is to give you an idea on how the process of getting information from your users and storing it in a database so you can later diplay it or use it with your web applications. if you are not sure what i mean by slq injection, you can find lots of resources on this website or google it and you will find. for example you can google this string:
How To Avoid And Prevent Sql Injection mysql_real_escape_string wallpaperama
done..
i hope this helps you
remember if you need php web hosting
webune has the right service for you
Links Related to : how to insert data into mysql db using form in php database enter code
Related Topics:
Related Forums:
Comments and replies About how to insert data into mysql db using form in php database enter code
::
1 ::
Reply #128982 Reply By
robert On Fri Feb 26, 2010 10:32 pm
robert:
Thanks, helped a lot keep up the good work!! x
::
2 ::
Reply #127350 Reply By
bashir On Wed Feb 03, 2010 10:33 pm
bashir:
So good code for me
Thank you for your help.
::
3 ::
Reply #121700 Reply By
akhter On Fri Nov 27, 2009 12:55 pm
akhter:
thanks alot for helping me....
::
4 ::
Reply #121466 Reply By
Mubashir On Wed Nov 25, 2009 4:47 am
Mubashir:
its so learnable.
step by step guide.
but please explain in this way that bigners
should also benifit from here.
Isaac:
thanks alot your example has helped me to know more but when i try to apply the code it does not work.
am trying to develop asimple links database (sitename,category,siteurl,descript but when i try to submit the form it connects but it doesnot give me the feedback . please i need your help me the insert part of it i think that's where the problem is.
thanx
::
6 ::
Reply #115265 Reply By
suchindranath On Thu Sep 10, 2009 9:17 pm
suchindranath:
thanks a lot boss
::
7 ::
Reply #113693 Reply By
IceMan On Mon Aug 24, 2009 4:52 pm
IceMan:
hey thanks a lot.. with this article i managed to create a form in the joomla article and save the data in the mysql db using the \"sourcerer - place any code \" extention. i may blog this technique (when i find the time)\r\n\r\n iamiceman.blogspot \r\n twitter /icetweets
::
8 ::
Reply #113642 Reply By
sanjay On Mon Aug 24, 2009 6:58 am
sanjay:
thanxxxx.....
its really helpful....
::
9 ::
Reply #109579 Reply By
Sourav On Thu Jul 16, 2009 2:31 pm
Sourav:

u r a gem. no sorry much much much more than that. i
some of our friends r startng a social net site & as all of us were just users & and knew nothing about web page development. we were just clueless as to what the developes r talking about. we just gave them scrnshots of what we want & how the site should work, but those were just pictures. we decided that before spending a lot for domain name + web host + web development + design we should atleast try to have a general understanding as how things will work on the admin end. we tried to research a bit and found out that php + phpmyadmin/mysql was the best way to go for a member based web site.
we know html but no php or mysql. from youtube we had a vid which helped us to download xampp which had apache,mysql & actually turned our com to a server. but php we r blank, tried tutorials till we had this-simple & works. thks. pls put how to assign a profile pg to members after reg like say orkut/myyearbook profile having pic+vid to mysql
anj:
i wanted to know how to insert data using one page only
::
11 ::
Reply #97670 Reply By
Lokesh On Fri Mar 20, 2009 2:24 am
Lokesh:
thanks for the guide really it is very use full for me and it is perfect cod to insert the data in database.
nice
lokesh kumar singh
::
12 ::
Reply #95634 Reply By
marklaki On Sun Mar 01, 2009 9:38 pm
marklaki:
im new to php and im taking a class on how to connet and use databases, this really helped me more than that whole class itself
thank you sir
marklaki from india
::
13 ::
Reply #95632 Reply By
Jorgen On Sun Mar 01, 2009 8:48 pm
Jorgen:
thank you soooooo much!!!!! this is just what i was looking for. i'm relatively new at this stuff, and i just spent the last 2 hours straight trying to figure it out. when i used the code you provided, it worked like a charm instantly. thanks again!
::
14 ::
Reply #91910 Reply By
issac On Thu Jan 29, 2009 6:38 am
issac:
yea, really wanna, iam so greatful to u the way ur code is that much helpful to
me, but the only thing is i could not get conneted with the welcome page and my data is not entering into the database
bye
::
15 ::
Reply #90979 Reply By
dale On Thu Jan 22, 2009 4:02 am
dale:
thank you so much for this i had tried 5 other sites and could not get my php script to run.
i used yours and it worked first time.
::
16 ::
Reply #90055 Reply By
fred On Thu Jan 15, 2009 6:29 am
fred:
i completed the steps after i saved the files i was able to connect to the database, i have never done anything like that this is a new way of making website, is this how everybody does it, i am so far behind. i need to learn more, thanks for getting me started.
::
17 ::
Reply #89924 Reply By
shailesh On Tue Jan 13, 2009 10:43 pm
shailesh:
very nice...................
::
18 ::
Reply #88029 Reply By
amar On Mon Dec 29, 2008 8:44 pm
amar:
this is very usefull to learn.....
good process..
::
19 ::
Reply #87265 Reply By
phani On Mon Dec 22, 2008 10:48 pm
phani:
this helped me very much...
thz for the information
::
20 ::
Reply #85723 Reply By
wallpaperama On Thu Dec 11, 2008 12:03 am
wallpaperama:
try it, see what happens. i think that's the only way you are going to learn..
also, try replacing the single quotes with double quotes.
you have to use single quotes sometimes because it allows you to use single quotes within the string. or how about if you want it to show single quote in a string, then you would use double quotes.
exampe:
echo "Wallpaperama Wallpapers's are cool";
echo "Wallpaperama "Wallpapers" are cool";
in the second example, you will get an error, to avoid getting the errror, you would have to use this:
echo "Wallpaperama Wallpapers\"s are cool";
or
echo 'Wallpaperama Wallpapers"s are cool';
how about if you have this:
$string = "Wallpaperama Wallpapers's are cool";
echo "this is the string: $string";
and how about this?
echo 'this is the string: $string';
or this:
echo 'this is the string: '.$string.' - see!';
or this?
echo "this is the string: ".$string." - see!";
so try playing with it..
::
21 ::
Reply #85635 Reply By
mike On Wed Dec 10, 2008 10:27 am
mike:
what i meant to say is why is there single and double quotes around the mysql_real_escape_string and why is there periods at the beginning and end?
::
22 ::
Reply #85634 Reply By
mike On Wed Dec 10, 2008 10:26 am
mike:
('".mysql_real_escape_string(stri is the reason for single and double quotes and the periods at the beginning and end? i am new to this just trying to understand better.
::
23 ::
Reply #84634 Reply By
meble On Wed Dec 03, 2008 11:25 pm
meble:
thank you for sharing that, i wanted to know how to do this in php how to insert a user enter fields to mysql sample code
::
24 ::
Reply #77264 Reply By
steve On Fri Oct 24, 2008 11:10 pm
steve:
it just means that you are providing the wrong username or password. check your login credentials. if you are being host by a hostin company, ask them if you dont know. sometimes you have to create the database on their control panel. many hosting companies provide phpmyadmin panel
::
25 ::
Reply #77257 Reply By
omkar On Fri Oct 24, 2008 9:55 pm
omkar:
hi
i created table in database but while creating it is giving an error that command denied for user localhost at localhost
for table san plz help me out
::
26 ::
Reply #77103 Reply By
steve On Fri Oct 24, 2008 7:48 am
steve:
a';drop table users; select * from data where name like '%
::
27 ::
Reply #75718 Reply By
nadee On Thu Oct 16, 2008 1:53 am
nadee:
i learn a lot!explain in nice way
::
28 ::
Reply #73231 Reply By
jake On Tue Sep 30, 2008 3:04 pm
jake:
say you wanted to print this to a pdf how would you go about doing that ?
::
29 ::
Reply #71049 Reply By
THE On Thu Sep 18, 2008 10:47 am
THE:
dude....thanks a sh!tload...i have been looking all over for this information and haven't found anywhere to explain it as well. i will implement your instructions and let you know what happens!
::
30 ::
Reply #70064 Reply By
alpa On Wed Sep 10, 2008 7:41 am
alpa:
thanks for giving me idea to create form.
thanks
::
31 ::
Reply #67746 Reply By
Norb On Sat Aug 23, 2008 3:19 am
Norb:
you guys can't connect with this code because the database name setting is missing in the configuration part. add this line:
$db = "database name";
::
32 ::
Reply #64292 Reply By
min On Fri Aug 01, 2008 2:02 am
min:
i would like to insert generate password into database.
::
33 ::
Reply #62816 Reply By
ashwanipal On Tue Jul 22, 2008 3:40 am
ashwanipal:
you gave good example .i understood very well.but i have some problemany website have in headeing [ news, about us ,carrer ,contect me]
if i puss the [contect us] then some time late a
open contect no.
we want know how is it possible please tel us with code in php and my mysql
::
34 ::
Reply #62642 Reply By
anadra On Sun Jul 20, 2008 11:37 pm
anadra:
cool knowledge. i support you to create and write down more knowledge for next time.thankx
::
35 ::
Reply #61771 Reply By
sql On Mon Jul 14, 2008 8:52 pm
sql:
comprehensive inserting values into database using php with mysql code
::
36 ::
Reply #58830 Reply By
tuls On Sun Jun 22, 2008 9:10 am
tuls:
make sure you have your credentials correctly.
you are getting this error because what you are giving as you login/password and database do not match so it doesnt allow you to access that database because you login or password is incorrect.
basically, its just telling you,
Hay man, i can't login to this database with what you are giving me.
::
37 ::
Reply #58809 Reply By
Manoj On Sun Jun 22, 2008 4:25 am
Manoj:
i am getting the following error while i implement this script on my website. can anyone help???
warning: mysql_connect() [function.mysql-connect]: access denied for user 'admin' at 'localhost' (using password: yes) in /home/texaskuw/public_html/submit.p on line 14
warning: mysql_select_db(): supplied argument is not a valid mysql-link resource in /home/texaskuw/public_html/submit.p on line 15
::
38 ::
Reply #57621 Reply By
ghgh On Fri Jun 13, 2008 6:35 am
ghgh:
dear gjhj gjuykig fgyjgyk
::
39 ::
Reply #56542 Reply By
anu On Wed Jun 04, 2008 11:40 pm
anu:
good material in site.
::
40 ::
Reply #54112 Reply By
hostman On Sat May 17, 2008 7:13 am
::
41 ::
Reply #53879 Reply By
jill On Thu May 15, 2008 5:10 am
jill:
if show screen shot along with this documentation then it will be more helpfull for beginers.
::
42 ::
Reply #53876 Reply By
robert On Thu May 15, 2008 4:58 am
robert:
hi,
i am following the same method u have explained here its really understandable but i am getting one error once i enter data and click on submit
error is "no database is selected". can u tell me whats the wrong with code? why it happens.. i cheched each & every thing. but still i have this problem... plz tell me the solution
::
43 ::
Reply #53871 Reply By
rhrhgrgrgr On Thu May 15, 2008 2:46 am
rhrhgrgrgr:
rgrejgrhrekgrkeg
regregro4utre
::
44 ::
Reply #50241 Reply By
sriram On Tue Apr 15, 2008 6:48 am
sriram:
the coding very nice for the new commer,and one more suggestion,you will send the screen shots along with coding then it is useful for new commer ok,
because i am new commer
::
45 ::
Reply #48272 Reply By
ashton On Wed Apr 02, 2008 7:42 am
ashton:
confused enuf by you so thanks a lot.
the cpanel shows the enter-data.php file as plain text file instead of php.how come? plz tell me via mail if you got a solution or reply on this page only.
::
46 ::
Reply #43570 Reply By
sergio On Fri Feb 29, 2008 5:47 am
::
47 ::
Reply #43016 Reply By
gaurav On Mon Feb 25, 2008 5:15 am
gaurav:
i used this code but i m not able to see data in my table. i mean it says 1 row in set and so on but the data is not visible. its blank. the rows are getting updated though but i am not able to see anything when i do "select * from user_info;" in mysql. please help....thnx in advance....
::
48 ::
Reply #41475 Reply By
Kitten On Sat Feb 16, 2008 4:58 am
Kitten:
i think i love you, this page was a god send!
::
49 ::
Reply #40648 Reply By
hostman On Sun Feb 10, 2008 5:30 pm
::
50 ::
Reply #40077 Reply By
jghjk On Wed Feb 06, 2008 10:25 pm