Php How To Enter Data Into Database With Php Scripts - Tutorial and Guides

Forums » Tutorial and Guides » Php How To Enter Data Into Database With Php Scripts
This Page Contains information about Php How To Enter Data Into Database With Php Scripts - Tutorial and Guides By webune hosting with 46 Replies. Last Upated: Sun Mar 09, 2014
hello, thanks for visiting webune.com - we host to many websites with php and mysql service.

many of our customers are newbies wanting to learn how to create their own websites. so we created this tutorial on how you can use php and mysql and html to add dynamic webpages to your sites using database. so here we go with the guide:


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:







HTML FORM
<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>





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
}





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.




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
phpmyadmin snapshot




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
####################################################################
# COPYRIGHT NOTICE:
# THIS SCRIPT CREATED BY WWW.WEBUNE.COM
# PLEASE DONT ERASE THIS
# Copyrights Webune.com
###################################################################
####################################################################
################ 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.



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
webune-php

and when i submit the form with my name and email address i get this
mysql-form-submitted

now when i go to my phpmyadmin, i can confirm that the information i've entered on the form, was inserted into my database
phpmyadmin-insert.jpg


done..

i hope this helps you

remember if you need php web hosting

webune has the right service for you
Rony
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.
devesh
thanks for your tutorial..
these are the best..plz give me information if required further..
manoj
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
manoj
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
manoj
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
Manoj
Thank You Guys, Now it worked.. It was my silly mistake.. Am really happy to see my first data in the DB.. great buddy
Manoj
I want the confirmation message to be as a popup mesage inside a div element. Can anyone advise what changes to be done??


echo ????????.
Manoj
I want the confirmation message to be as a popup mesage inside a div element. Can anyone advise what changes to be done??


echo ????????.
baisakhi
Thanks a bunch !!! it was a great help.
rgds
Stan Fraser
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
http://www.webune.com/forums/php-how-to-enter-data-into-database-with-php-scripts.html
shafika
Why this happen??
after i inserted all the data in the form..this was happen..
what should i do??
"ERROR: No database selected"
Web Hosting Support
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";
eric
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?
gandolf98
Once I figured out what the php was doing and how I could adapt it, it works great.
Now is there a way for the PHP not only to place data in DB, but what code addition is needed to allow it to Mail the info to me and a responce "Thank You" letter to the person that filled out the form?
omkar
hi friends it works fine but i got error

ERROR: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (46)

can u or anyone help me out it is very important to me
omkar
hi friends it works fine but i got error

ERROR: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (46)

can u or anyone help me out it is very important to me
omkar
hi
i am using web host there local host name
is mysql ur code is working but i am not able to see inserted data into my table that is thank you message comes but after that table does not show any record plz help
$hostname = "localhost";
$db_user = "webune_user";
$db_password = "mypassword";
$database = "database";
$db_table = "user_info";
abdul
yes, thank you for teaching me how how to insert data from html form into mysql database
Manick
Hi, I have entering data html form to mysql table but if i save php file or refresh html page either 0 data has been saved or previous data has been saved.
I want only those data, which are enter by html form.
Kindly, tell about this.
Manick
Hi, I have done the above example, but data has been not entering the table.
im using
hostname = "localhost";
$db_user = "root";
$db_password = "";
$database = "database";
$db_table = "user_info";
poaulss555
i got mine working using your code at first i thot how to enter the data table in mysql? but then when i follow you step by step tutorial i was able to insert the data into my db using the php code files. thanks, im new at this and it helped me understand how this works. i always wondered how you can put words of information into a database and then come back and put it on the website. this is neat. i will take a class on this now you got me interested.
new4nes
my questions is.. how to post data between php scripts i have multiple php scripts, can i use the same code for them?
srikrishna
i have my wone php myadmin so ineed to connect my database please provied me d details how can i connect to tha t please provied details
briou
can u show how to enter data into ms access database with php script
marri
doe this work if no value should entered into database on refresh in php
651temerier
wow, very detailed tutorial step by step using phpmyadmin and connect to html form. this is exactly what i was googling for. hahahah. you're a genius, it worked for me
surekiger
my tip is that if you use php and mysql make sure all field are filled before inserting data to the database
luious
i am entering data into the database two are more request at time run in my server using php
styema32
i was interested in knowing how to create web page to enter data to database, but this works great. thank you
jackman
i get the text boxes and the update button, but when i press submit, the page refreshes, but the data has not been updated in the database using php and mysql
contant_ip
i want to insert my login details to phpmyadmin database ??
anthoney
error: can't connect to local mysql server through socket '/var/run/mysqld/mysqld.sock' (2)

i keep getting this message
neximin
how to add the details from php input boxes to phpmyadmin database?
Karan
thanks. it helped a lot.
palupe
i can show you how does it know to enter values to database when we hit submit in php
harsha
its extra ordinary
randy
i learned how to , but i got an error you guys saved me, i can do this now by myself
Kevin
thank you, but its not working. i'am not getting an error message, and the information is not going into the database.
jay&viru
your website wants more develope in future for easy learning for help.
Rahima
thankyou for the tutorial.its working. i want to know how to enter the selected radio button details into the mysql daytabase.the textbox details are getting updated to my database but the radio button datas are not getting updated.
Bob
this was good, but i found it a bit confusing and way to much to take in, try and make it simpler!
samal
how to enter date of birth in database
Laura
i've been searching online for a very basic way to create input fields that fill out an online database. this was the first to really make sense and i got it to work with a little trial and error. thank you!
nraji
thank you for your tutorial.
james
ada ponga da
tan
am experiencing an error that no database is selected while i did include one. $hostname = "localhost"; $db_user = "root"; // $db_password = ""; // $database = "customer"; $db_table = "user_info"; where did i go wrong?
Title: Php How To Enter Data Into Database With Php Scripts - Tutorial and Guides [655]
Tags: Php How To Enter Data Into Database With Php Scripts
By , 46 Comments