Php How To Enter Data Into Database With Php Scripts
php how to enter data into database with php scripts
php, how, to, enter, data, into, database, with, php, scripts
Php How To Enter Data Into Database With Php Scripts
Post Description: php how to enter data into database with php scripts
POST# 1119
Posted On: Wed Apr 09, 2008 8:03 pm
otakd1985
Topic: Php How To Enter Data Into Database With Php Scripts
i want to learn how i can enter or insert data into a database using mysql and php code

can you show me how

this would really be helpful to get my website started

i have web hosting with you

thanks
Share:
BBCODE:
HTML Code:


Wed Apr 09, 2008 8:07 pm
1
hostman
Reply #1841

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:



Name:

Email:







PHPCODE
<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>
Wed Apr 09, 2008 8:25 pm
2
hostman
Reply #1842

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
}
Wed Apr 09, 2008 8:32 pm
3
hostman
Reply #1843

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

  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)


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.
Wed Apr 09, 2008 8:39 pm
4
hostman
Reply #1844

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
Wed Apr 09, 2008 9:56 pm
5
hostman
Reply #1845
Picture 09--phpmyadmin-sql-tab.jpg
its should look something like this on your phpmyadmin
Wed Apr 09, 2008 9:57 pm
6
hostman
Reply #1846
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.
Wed Apr 09, 2008 9:58 pm
7
hostman
Reply #1847
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
Wed Apr 09, 2008 10:08 pm
8
hostman
Reply #1848
Picture 09--webune-php.jpg
here is how the form looks like in my firefox browser when i open webune.php
Wed Apr 09, 2008 10:19 pm
9
hostman
Reply #1849
Picture 09--mysql-form-submitted.jpg
and when i submit the form with my name and email address i get this
Wed Apr 09, 2008 10:28 pm
10
hostman
Reply #1850
Picture 09--phpmyadmin-insert.jpg
now when i go to my phpmyadmin, i can confirm that the information i've entered on the form, was inserted into my database

done..

i hope this helps you

remember if you need php web hosting

webune has the right service for you
Wed Apr 09, 2008 10:44 pm
11
hostman
Reply #1851

Step 5



now with step 5, it starts to get exciting, on step 5 we will retrieve the information from the database and then display it or show it in HTML browser so you can start createing dynamic web pages with php and mysql. just like this forum.
Sat May 10, 2008 10:55 pm
12
Rony
Reply #2026
Thanks for your tutorial. its very much helpful for all of us who fall on problem from php to database. thanks again for your this tutorial. i was searching this type of article from long time.
Wed May 21, 2008 7:38 am
13
devesh
Reply #2033
thanks for your tutorial..
these are the best..plz give me information if required further..
Sun Jun 22, 2008 4:33 am
14
manoj
Reply #2074
Why this error???


Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'admin'at 'localhost' (using password: YES) in /home/texaskuw/public_html/submit.php on line 19

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/texaskuw/public_html/submit.php on line 20
How To Insert Data Into MySQL db using form in php
Sun Jun 22, 2008 4:37 am
15
manoj
Reply #2075
Why this error???


Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'admin'at 'localhost' (using password: YES) in /home/texaskuw/public_html/submit.php on line 19

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/texaskuw/public_html/submit.php on line 20
How To Insert Data Into MySQL db using form in php
Sun Jun 22, 2008 4:38 am
16
manoj
Reply #2076
Why this error???


Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'admin'at 'localhost' (using password: YES) in /home/texaskuw/public_html/submit.php on line 19

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/texaskuw/public_html/submit.php on line 20
How To Insert Data Into MySQL db using form in php
Sun Jun 22, 2008 5:06 am
17
Manoj
Reply #2077
Thank You Guys, Now it worked.. It was my silly mistake.. Am really happy to see my first data in the DB.. great buddy
Tue Jun 24, 2008 2:09 am
18
Manoj
Reply #2082
I want the confirmation message to be as a popup mesage inside a div element. Can anyone advise what changes to be done??


echo ????????.
Tue Jun 24, 2008 2:12 am
19
Manoj
Reply #2083
I want the confirmation message to be as a popup mesage inside a div element. Can anyone advise what changes to be done??


echo ????????.
Thu Jul 10, 2008 4:39 pm
20
baisakhi
Reply #2094
Thanks a bunch !!! it was a great help.
rgds
Thu Jul 24, 2008 1:20 am
21
Stan Fraser
Reply #2105
Found this very useful, what I would like to do is return automatically to the input form after data is submitted, how do I do that. Thanks and Regards
Mon Jul 28, 2008 7:24 pm
22
shafika
Reply #2109
Why this happen??
after i inserted all the data in the form..this was happen..
what should i do??
"ERROR: No database selected"
Thu Jul 31, 2008 1:43 pm
23
Web Hosting Support
Reply #2111
it just means that the script cannot connect to the database your provided, make sure you have provided the correct username and password

$hostname = "localhost";
$db_user = "webune_user";
$db_password = "mypassword";
$database = "database";
$db_table = "user_info";
Sun Aug 03, 2008 1:45 pm
24
eric
Reply #2114
I think I followed the instructions correctly but when I run the page in a browser I get this error stuff at the top of the page:

Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 113 in /data/17/1/106/141/1595793/user/1719441/ on line 28

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /data/17/1/106/141/1595793/user/1719441/ on line 29

Any Ideas?

What do you think?

* name:  

* email:  

* Please enter comments:


Receive Replies on my Comments
(An email will be sent to you when someone replies to your comments)

Add image to comments
yes no             upload