how to insert data into mysql db using form in php database enter code
Webmasters: If you have a web page related to this topic, Click here to Add Your Link.
Reply Your Comments: Click this button if you are interested in replying to this topic and leave your comments
Sent To Friend CLick this button if you want to send this page to a friend.
Subscribe to RSS CLick this button if you want to subscribe to this RSS Feed. You can use your browsers feeds burners if you have mozilla or internet explorer 7 or higher and keep up with updates.
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 17204 Times Since Mon Feb 19, 2007 5:03 pm Author hostman with 48 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:
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.
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
You can use this HTML code to put it on your website to show your friends this wallpaper. Use this code on your profile like myspace, friendster, Facebook or others. Just Copy and Paste it in your HTML on your websites
To embed this topic, just copy the code from the "Embed" box. Once you've copied the code, just paste it into your website or blog to embed it.
BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums
Links Related to : how to insert data into mysql db using form in php database enter code
Comments and replies About how to insert data into mysql db using form in php database enter code
:: 1 :: Reply #653 Reply By Ian On Thu Mar 01, 2007 6:06 am
Ian:
You rock! Thanks so much for this. Had me up and running in no time!
:: 2 :: Reply #704 Reply By jS On Mon Mar 05, 2007 4:20 am
jS:
Google 'MySQL injection' for security issues with this method before you use it in a production environment.
:: 3 :: Reply #1433 Reply By sql teacher On Tue Apr 10, 2007 12:54 pm
sql teacher:
how to insert a field into database table
:: 4 :: Reply #21091 Reply By wasi On Fri Sep 21, 2007 8:51 am
wasi:
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
error: can't connect to local mysql server through socket '/tmp/mysql.sock' (46)
can you plz explain me wht should i do to sort this problem out.
need ur help
very thank full to u
with regards
wasiuddinmalik at email2
:: 5 :: Reply #28006 Reply By bb1 On Fri Nov 16, 2007 7:58 pm
bb1:
might be helpful to add in the variables for the form elements:
otherwise empty data might be written to the database. "
where do i add this?? can you post the whole code? it worked but even though i entered data on my form
i entered all 0's now i can't entered anything because of the blank info.
:: 7 :: Reply #32875 Reply By Imtiaz On Mon Dec 24, 2007 12:39 am
Imtiaz:
i have seen your code how to insert data into database.
thanks
:: 8 :: Reply #40077 Reply By jghjk On Wed Feb 06, 2008 10:25 pm
jghjk:
hjkgjkghjkghk
:: 9 :: Reply #40648 Reply By hostman On Sun Feb 10, 2008 5:30 pm
:: 10 :: 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!
:: 11 :: 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....
:: 12 :: Reply #43570 Reply By sergio On Fri Feb 29, 2008 5:47 am
sergio:
sdaasdasdasd
:: 13 :: 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.
:: 14 :: 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
:: 15 :: Reply #53871 Reply By rhrhgrgrgr On Thu May 15, 2008 2:46 am
rhrhgrgrgr:
rgrejgrhrekgrkeg
regregro4utre
:: 16 :: 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
:: 17 :: 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.
:: 18 :: Reply #54112 Reply By hostman On Sat May 17, 2008 7:13 am
hostman:
if you want to see screen shots then go to this post:
:: 19 :: Reply #56542 Reply By anu On Wed Jun 04, 2008 11:40 pm
anu:
good material in site.
:: 20 :: Reply #57621 Reply By ghgh On Fri Jun 13, 2008 6:35 am
ghgh:
dear gjhj gjuykig fgyjgyk
:: 21 :: 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
:: 22 :: 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.
:: 23 :: Reply #61771 Reply By sql On Mon Jul 14, 2008 8:52 pm
sql:
comprehensive inserting values into database using php with mysql code
:: 24 :: Reply #62642 Reply By anadra heidhta-scultz On Sun Jul 20, 2008 11:37 pm
anadra heidhta-scultz:
cool knowledge. i support you to create and write down more knowledge for next time.thankx
:: 25 :: 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
:: 26 :: Reply #64292 Reply By min min On Fri Aug 01, 2008 2:02 am
min min:
i would like to insert generate password into database.
:: 27 :: 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";
:: 28 :: Reply #70064 Reply By alpa On Wed Sep 10, 2008 7:41 am
alpa:
thanks for giving me idea to create form.
thanks
:: 29 :: Reply #71049 Reply By THE MAN On Thu Sep 18, 2008 10:47 am
THE MAN:
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 #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 ?
:: 31 :: Reply #75718 Reply By nadee On Thu Oct 16, 2008 1:53 am
nadee:
i learn a lot!explain in nice way
:: 32 :: Reply #77103 Reply By steve On Fri Oct 24, 2008 7:48 am
steve:
a';drop table users; select * from data where name like '%
:: 33 :: 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
:: 34 :: 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
:: 35 :: 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
:: 36 :: 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.
:: 37 :: 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?
:: 38 :: 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..
:: 39 :: Reply #87265 Reply By phani On Mon Dec 22, 2008 10:48 pm
phani:
this helped me very much...
thz for the information
:: 40 :: Reply #88029 Reply By amar On Mon Dec 29, 2008 8:44 pm
amar:
this is very usefull to learn.....
good process..
:: 41 :: Reply #89924 Reply By shailesh kumar On Tue Jan 13, 2009 10:43 pm
shailesh kumar:
very nice...................
:: 42 :: Reply #90055 Reply By fred quymbi On Thu Jan 15, 2009 6:29 am
fred quymbi:
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.
:: 43 :: 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.
:: 44 :: Reply #91910 Reply By issac raju (hyderabad) On Thu Jan 29, 2009 6:38 am
issac raju (hyderabad):
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
:: 45 :: 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!
:: 46 :: 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
:: 47 :: Reply #97670 Reply By Lokesh Kumar On Fri Mar 20, 2009 2:24 am
Lokesh Kumar :
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
:: 48 :: Reply #103324 Reply By anj On Fri May 15, 2009 3:46 am
anj:
i wanted to know how to insert data using one page only