How To Insert Data Into MySQL db using form in php Database Enter Code

RegisterLogin
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
Tags: How, To, Insert, Data, Into, MySQL, db, using, form, in, php, Database, Enter, Code
This Post Was Posted On Dec 10, 2011 By web hosting #1473
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
manish
#5417 1
nyc..............:)
Aug 30, 2011 Reply Report abuse
JUAN
#5416 2
i added this for select the right database

$conn = mysql_connect($hostname, $db_user, $db_password) or die ('error connecting to mysql');
$db = 'wallpaperama1';
mysql_select_db($db);

mi database name is wallpaperama1

cheers and great tips very useful for me
Aug 29, 2011 Reply Report abuse
Johann
#5237 3
very helpfull information. txs very much.
Jun 01, 2011 Reply Report abuse
dhananjay
#5212 4
<?php
$connect_mysql_conect("localhost","root","1") or die (mysql_error());
?>
May 21, 2011 Reply Report abuse
bob
#5086 5
when i use this script i get a message saying "no database selected"

i see you have named you database 'users' and after going over and over the script i cant see anywhere where you state which database to connect to, so i cant understand how anyone has possibly made this work??
May 05, 2011 Reply Report abuse
derlier
#5039 6
works perfect
Apr 18, 2011 Reply Report abuse
sachin
#4858 7
awesome explanation....please write more.
Feb 24, 2011 Reply Report abuse
asia
#4828 8
hi just used yu code be it gives mi error:column count doesn't match value count at row 1
with the code below;
<?php
####################################################################
# copyright notice:
# this script created by webune
# please dont erase this
# copyrights webune
###################################################################
####################################################################
################ 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 = "root"; // change to your database password
$db_password = "ab1986"; // change to your database password
$database = "stock"; // provide your database name
$db_table = "authors"; // leave this as is


# stop here
#################################################################
Feb 09, 2011 Reply Report abuse
dfs
#4793 9
sdssdsfsfsffsdfsdddddddddddddddddddddddddddd
Jan 31, 2011 Reply Report abuse
robbie
#4747 10
im confuses, is there anything easier to learn
Jan 09, 2011 Reply Report abuse
ET
#4723 11
can you please give me a simple example of how you could combine a login page so, let say i register on the register page, then login after confirmation thereafter i can enter many entries under my username that only i can manipulate under my username. i hope your getting my drift, regards etienne
Dec 29, 2010 Reply Report abuse
chris
#4390 12
this good, but pls kindly send a script of php on how to connect my form to work with my database.
email: ccare - at - squaregrabba
Aug 25, 2010 Reply Report abuse
jarid
#1913 13
hello sir, my name is jarid from india i want to learn about entering form data info mysql database php
Sep 17, 2009 Reply Report abuse
sergio
#1912 14
sdaasdasdasd
Feb 29, 2008 Reply Report abuse
gaurav
#1911 15
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....
Feb 25, 2008 Reply Report abuse
View More Comments
Leave Your Comments...
©2011 Webune Forums - Sun Dec 18, 2011 5:06 pm
Powered by: Webune Forums V3