adv banner
Web Programming  Home Web Programming PHP PHP/MySQL Tutorial System
rss

PHP/MySQL Tutorial System

Author: GreyCobra.com More by this author


PHP/MySQL Tutorial SystemThis Tutorial will run you through the basics of building your own Tutorial CMS!

We are going to use these variables for our MySQL database.

  • ID
  • title
  • avatar
  • date
  • description
  • user
  • message

1. Lets start off by creating our table..Go to your database, as I asume you already know how to create, and import this SQL.

CREATE TABLE 'tutorials' (
'ID' tinyint(11) NOT NULL auto_increment,
'title' text NOT NULL,
'avatar' text NOT NULL,
'date' date NOT NULL default '0000-00-00',
'description' text NOT NULL,
'user' text NOT NULL,
'message' text NOT NULL,
PRIMARY KEY ('ID')
) TYPE=MyISAM AUTO_INCREMENT=1 ;

2. Now, we're going to enter in our tutorial into our table. In PHPmyAdmin (or whatever you use) insert a new row into your table, which we named tutorials. You dont need to add anything for the ID, it automatically increases the ID number by 1.

After you finished entering in your tutorial into the MySQL database, create a new file, using Dreamweaver or whatever you use. We will call it dbconnect.php.

Copy and paste this into your file...With of course adding in your information

<?
$username = "yourusername";
$password = "yourpassword";
$host = "localhost";
$database = "yourdatabase";
mysql_connect($host,$username,$password) or die("Error connecting to Database!
" . mysql_error());

mysql_select_db($database) or die("Cannot select database!
" . mysql_error());
?>

3. Now create a new file, in the same directory as the dbconnect.php file. Call it tutorials.php and put this in the file.. of course you may have to change it to fit your needs...

<?php

include ("dbconnect.php");

if (isset($ID))
{
$tuts="SELECT * from yourtable where ID='$ID'";
$tuts2=mysql_query($tuts);
while ($tuts3=mysql_fetch_array($tuts2))

{

echo "

<table width=100% border=0 cellspacing=3 cellpadding=0>
<tr>
<td>
<font color=#82A1FF size=1 face=Verdana, Arial, Helvetica, sans-serif><b>$tuts3[title]</b><br>
<font color=#000000>By <b>$tuts3[user]</b><br>
<i>$tuts3[description]</i><br>
<br>
$tuts3[message]<br>
<br>
</td>
</tr>
</table>

"; }
}

else if(!isset($id))
{
$tut="SELECT * from yourtable";
$tut2=mysql_query($tut);
while($tut3=mysql_fetch_array($tut2))

{

echo "

<font color=#000000 size=1 face=Verdana, Arial, Helvetica, sans-serif>
<table width=250 border=0 cellspacing=3 cellpadding=0>
<tr>
<td width=42 height=42>
<div align=center><a href='tutorials.php?ID=$tut3[ID]'><img src='$tut3[avatar]' border='0' alt='$tut3[title]' target='main'></a></div></td>
<td width=208><font color=#82A1FF size=1 face=Verdana, Arial, Helvetica, sans-serif><strong>$tut3[title]</strong><br>
<font color=#000000><em>$tut3[date]</em><br>
By <strong>$tut3[user]</strong></font><br></font></td>
</tr></table>

"; }
}

?>

4. Now, to create an add form.. create a new file called add.php and put this in it..

<form action="verify.php" method=post>
<p>Title:<br>
<input type="text" name="frmtitle" size="23" value="Title">
<br>
Avatar URL :<br />

<input type="text" name="frmavatar" size="23" value="Icon URL">
<br />
User:<br>
<input type="text" name="frmuser" size="23" value="Username">
<br>
Description:<br>
<input type="text" name="frmuser" size="23" value="">
<br>
Message:
<br>
<textarea name="frmmessage" cols="100" rows="25"></textarea>
<br>

<input type="submit" name="Submit" value="Submit">
</p>
</form>

5. Now, to make a verify file to put the new information in the MySQL database. Create a new file called verify.php and add this in it..

<?php

// Include the config

include("dbconnect.php");

// Set the variables from the form

$Title = $_POST['frmtitle'];

$Avatar = $_POST['frmavatar'];

$User = $_POST['frmuser'];

$Message = $_POST['frmmessage'];

$Description = $_POST['frmdescription'];


$sql = "INSERT INTO $table SET title='$Title', avatar='$Avatar',

user='$User', message='$Message', description='$Description'";

if (mysql_query($sql)) {

echo("Your tutoral has been added<br />");

} else {

echo("Error adding entry: " . mysql_error() . "");

}

?>

<script language=javascript>

<!--

location.href="tutorials.php";

//-->

</script>

6. And your done! This tutorial was made from several other tutorials, but with major editing and addons. Soon to add on an edit forum and delete form and possible a pagination addon.. if I can figure it ouit!



Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "PHP/MySQL Tutorial System"