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.