In this tutorial you are going to learn about basic PHP/MySQL interaction by making a link database. First you need to setup a table for us to work from.

<?php
// First connect to the MySQL server.
$db mysql_connect("localhost","user","password");
mysql_select_db ("database"); 
// If table esists get rid of it
$drop mysql_query('DROP TABLE IF EXISTS links');
echo (
"<p>links table  dropped</p>");
// Now create links table
$sql mysql_query('CREATE TABLE links ( 
id mediumint(10) NOT NULL auto_increment, 
title varchar(80), 
url varchar(80), 
PRIMARY KEY (id) 
)'
) or die("unable to create table");
echo (
"<p>links table created.</p>");
?>

Replace ("localhost","user","password") and ("database") with the settings your host requires, and save this as install.php


Next you need to create a form from which you will insert your links. The first input must have name="title" and the second must have name="url". Your PHP script will use these names when inserting your data. Remember when you created your table the second column was named "title" and the third was named "url". The first column, "id" is used as your index and kinda takes care of its self with no input needed from you.

<form name="links" method="post" action="add_links.php">
<table>
<tr>
<td>Title :</td>
<td><input type="text" name="title" size="60" /></td>
</tr>
<tr>
<td>URL :</td>
<td><input type="text" name="url" size="60" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>

Save this as add.html. Notice in input type="text" name="title", the "title" and input type="text" name="url", the "url". This is important for later as the script below requires this as $_POST['title'] and $_POST['url']. Now you have your form, time to make a script that inputs the date from the form into your MySQL DB.


Now for your script that your form will use to insert your link title and the URL into your database. See in your form - action="add_links.php"

<?php
$db 
mysql_connect("localhost","user","password");
mysql_select_db ("database"); 
$query "INSERT INTO links(title, url) 
VALUES('"
.$_POST['title']."','".$_POST['url']."')";
$result mysql_query($query);
echo 
'Link entered.';
?>

Replace ("localhost","user","password") and ("database") with the settings your host requires, and save this as add_links.php . I think this script is fairly self explanatory.


There's no point is putting your links in a database if you can't see them so now you need a bit of code that will present your data in a table.

<?php
// Connect to the database
$db mysql_connect("localhost","user","password");
mysql_select_db ("database");
// Ask the database for the information from the links table
$result mysql_query("SELECT * FROM links");
// Now we print out the results, starting by making a table
echo "<table align=\"center\" border=\"0\">";
while (
$rows mysql_fetch_row($result))
// Here we make the script keep making new rows until all
// the links in our database are shown, this is called a loop
{
echo 
"<tr><td><a href='$rows[2]'>$rows[1]</a></td></tr>";
}
// Finally we close off the table
echo "</table>";
?>

Replace ("localhost","user","password") and ("database") with the settings your host requires, and save this as view_links.php. Load it up in your browser, you should see the names of the links in your databases, and when you click on them you'll be taken to the URL.


View demo

Download code


Copyright © 2000 — 2017 by The Square Eye