How To Insert Data Into MySQL Db Using Form In Php Database Enter Code



How To Insert Data Into MySQL Db Using Form In Php Database Enter Code
 (380) Click. Work. Collect
How To Insert Data Into MySQL Db Using Form In Php Database Enter Code
Post Description:
Post Tags: how, to, insert, data, into, mysql, db, using, form, in, php, database, enter, code
This Post Has Been Viewed 10493 Times Since Mon Feb 19, 2007 5:03 pm Posted By hostman with 22 replies
How To Insert Data Into MySQL Db Using Form In Php Database Enter Code
UPDATE: ok guys, i have updated tutorial, i checked all the bugs, the tutorial on this link is much better, i wrote it myself and tested.
http://www.webune.com/forums/php-how-to-enter-data-into-database-with-php-scripts.html




---------------------------
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.

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:



     Code:
<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>


Then next stop is to check if the form has been submitted, for this we will need PHP code. for cdoe to check if the form has been submited or not will be by checking the value of "Submit" which is the name of the button in our form.

     Code:
<?
if (isset($_REQUEST['Submit']))
{
// INSERT DATA FROM FORM ONCE THE FORM HAS BEEN SUBMITTED
}
else
{
// DISPLAY FORM IF FORM HAS NOT BEEN SUBMITTED
}
?>


the third piece we are going to need is the actual connection to your database from the PHP script. so in order to do this, you must tell your PHP script how to connect to your MYSQL database and you will need the 7 following items, if you don't know these 7 items, you are wasting your time here, because you need these to be able to put the data into your mysql tables or database. and they are:

1. hostname (usually localhost)
2. database user name (if you don't have this contact your host company)
3. database user password (if you don't have this contact your host company)
4. database name (if you don't have this contact your host company)
5. database table name (you create this. you can create a table in phpmyadmin)
6. Create a field called user_name in the database table (you create this. you can create a table in phpmyadmin)
7. Create a field called user_email in the database table (you create this. you can create a table in phpmyadmin)

OK, once you have confirmed you have all these seven items we can proceed. for my example, i am going to pretend that these are mine:

my hostname is "localhost"
my database user name is "wallpaperama1"
my wallpaperama1 password is "password123"
my database name is "users"
my database table name is "user_info"
my user_name field in the user_info table will be called "user_name"
and user_email field in the user_ifo table will be called "user_email"

if you have phpmyadmin, you can dump this query. just click on the "SQL" tab on your phpmyadmin and put this code in the text area and submit:

     Code:
CREATE TABLE `user_info` (
`user_name` VARCHAR( 50 ) NOT NULL ,
`user_email` VARCHAR( 50 ) NOT NULL
);


so once i have all these and can put my code together and this is how my php script would look like:


     Code:

<?php
####################################################################
################ DATABASE CONFIGURE ##############################
####################################################################
$hostname = "localhost";
$db_user = "wallpaperama1";
$db_password = "password123";
$db_table = "user_info";


# STOP HERE
####################################################################
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($db_table,$db);
?>
<html>
<head>
<title>How To Insert Data Into MySQL db using form in php</title>
</head>
<body>
How To Insert Data Into MySQL db using form in php<hr>
<?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 ('$user_name','$user_email')";
if($result = mysql_query($sql ,$db))
{
echo "Thank you, Your information has been entered into our database";
}
else
{
echo "ERROR: ".mysql_error();
}

}
else
{
?>
<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 what you can do is open your text editor like notepad and copy and past the php code above and save it as enter-data.php (if you are using notepad, make sure to put qoutes in between the file name, like this "enter-data.php")

IMPORTANT: make sure you change the following to match your database, otherwise, nothing will be entered into your databse if you don't give the correct information in the following lines in this code:

     Code:
$hostname = "localhost";
$db_user = "wallpaperama1";
$db_password = "password123";
$db_table = "user_info";


now that yo have saved the enter-data.php, upload to your PHP hosting website and open it with your brows and you will see how it work, it will enter the data into your mysql database.

REMEMBER: This is a plain and simple script. There are alot of security issues you need to implement. The purpose of this tutorial was to give you an idea of how you can enter data into your mysql database. You can make your scripts more secure by using the mysql_real_escape_string() for example.

Leave Your Comments     [ dejar commentarios ]
  * Name     [nombre]

  * eMail (will not be published)     [coreo electronico]

* Enter Your Reply or Comments:    [commentarios]


Add Picture To Comments         [incluir foto]
YES NO             upload
Receive Replies on my Comments (An email will be sent to you when someone replies to your comments)

     

Comments and replies About How To Insert Data Into MySQL Db Using Form In Php Database Enter Code




:: 1 :: #653 - Reply By Ian On Thu Mar 01, 2007 6:06 am
You rock! Thanks so much for this. Had me up and running in no time!
:: 2 :: #704 - Reply By jS On Mon Mar 05, 2007 4:20 am
Google 'MySQL injection' for security issues with this method before you use it in a production environment.
:: 3 :: #1433 - Reply By sql teacher On Tue Apr 10, 2007 12:54 pm
how to insert a field into database table
:: 4 :: #21091 - Reply By wasi On Fri Sep 21, 2007 8:51 am
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 :: #28006 - Reply By bb1 On Fri Nov 16, 2007 7:58 pm
might be helpful to add in the variables for the form elements:

$user_name=($_post['user_name']);
$user_email=($_post['user_email']);

otherwise empty data might be written to the database.

with regards to wasi - looks like you haven't configured the mysql socket - this can be done in the mysql config file.
:: 6 :: #31125 - Reply By lilhelp On Sun Dec 09, 2007 6:37 pm
"might be helpful to add in the variables for the form elements:

$user_name=($_post['user_name']);
$user_email=($_post['user_email']);

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 :: #32875 - Reply By Imtiaz On Mon Dec 24, 2007 12:39 am
i have seen your code how to insert data into database.
thanks
:: 8 :: #40077 - Reply By jghjk On Wed Feb 06, 2008 10:25 pm
hjkgjkghjkghk
:: 9 :: #40648 - Reply By hostman On Sun Feb 10, 2008 5:30 pm
:: 10 :: #41475 - Reply By Kitten On Sat Feb 16, 2008 4:58 am
i think i love you, this page was a god send!
:: 11 :: #43016 - Reply By gaurav On Mon Feb 25, 2008 5:15 am
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 :: #43570 - Reply By sergio On Fri Feb 29, 2008 5:47 am
sdaasdasdasd
:: 13 :: #48272 - Reply By ashton On Wed Apr 02, 2008 7:42 am
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 :: #50241 - Reply By sriram On Tue Apr 15, 2008 6:48 am
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 :: #53871 - Reply By rhrhgrgrgr On Thu May 15, 2008 2:46 am
rgrejgrhrekgrkeg
regregro4utre
:: 16 :: #53876 - Reply By robert On Thu May 15, 2008 4:58 am
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 :: #53879 - Reply By jill On Thu May 15, 2008 5:10 am
if show screen shot along with this documentation then it will be more helpfull for beginers.
:: 18 :: #54112 - Reply By hostman On Sat May 17, 2008 7:13 am
if you want to see screen shots then go to this post:

webune.com/forums/php-how-to-enter-data-into-database-with-php-scripts
:: 19 :: #56542 - Reply By anu On Wed Jun 04, 2008 11:40 pm
good material in site.
:: 20 :: #57621 - Reply By ghgh On Fri Jun 13, 2008 6:35 am
dear gjhj gjuykig fgyjgyk
:: 21 :: #58809 - Reply By Manoj On Sun Jun 22, 2008 4:25 am
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 :: #58830 - Reply By tuls On Sun Jun 22, 2008 9:10 am
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.