Dec
7
2011

Mysql Database with PHP and HTML with Search Function Part 3

Sign up to 000webhost.com if you haven’t already!

Right, now your database and table are ready to be entered into, we can start connecting.

Open the text editor of your choice, I use notepad++ because it’s awesome. If you’re using a Linux platform like Ubuntu I recommend using Bluefish. First and foremost, we’re going to make the HTML form used to input your data. Nice and simple of course, mine looks something like this:

<html>
<body>

<form action="insert.php" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
Phone: <input type="text" name="phone"><br>
<input type="Submit">
</form>

</body>
</html>

Save that file as form.html, it will be saved in the root directory of wherever you’re hosting, but for now just save it in a folder on your desktop, and remember where it is. insert.php is what we’re going to make next, it’s the php used to process the information from this form. Make a new file, call it insert.php and use the following code to connect to your database:

<?php

$hostname="mysql6.000webhost.com";
$username_sql="a5649649_test";
$password_sql="yourpassword";
$dbname="a5649649_test";
mysql_connect($hostname, $username_sql, $password_sql) OR DIE ('Unable to
connect to database! Please try again later.');

@mysql_select_db($dbname) or die( "Unable to select database");
mysql_close();
?>

A lot of hosts require that you use ‘localhost’ as your hostname, this doesn’t work for 000webhost, and you must use the one provided. To test this you can upload insert.php to your webhost foot folder and locate it in your browser, so far if everything is correct, it should say nothing when you access it. Ok? Excellent! Moving on.

We need to set the database to understand how each section of the form relates to the corresponding section of your table in the database, we do this with the following:

$fname=$_POST['fname'];
$lname=$_POST['lname'];
$phone=$_POST['phone'];
$query = "INSERT INTO test VALUES ('','$fname','$lname','$phone')";

Just to get all the ordering correct, below is my complete insert.php file.

<?php

$hostname="mysql6.000webhost.com";
$username_sql="a5649649_test";
$password_sql="yourpassword";
$dbname="a5649649_test";

$fname=$_POST['fname'];
$lname=$_POST['lname'];
$phone=$_POST['phone'];

mysql_connect($hostname, $username_sql, $password_sql) OR DIE ('Unable to
connect to database! Please try again later.');

@mysql_select_db($dbname) or die( "Unable to select database");

$query = "INSERT INTO test VALUES ('','$fname','$lname','$phone')";

mysql_query($query);

mysql_close();

?>

OK! Now, upload both form.html and insert.php to your host, go to yourhostname.com/form.html , fill in some details, press send and it should update your database! Login to 000webhost.com MySql to see the changes, if you have an error message show up then there’s obviously a problem, leave a message if so!

Now, if you’re ready (and if it’s ready), move on to page 4!

 

Leave a comment