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>
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
}
?>
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
);
`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";
$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.

