Jump to content


Photo

PHP: Tutorial dB


  • Please log in to reply
4 replies to this topic

#1 ludwigw

ludwigw

    P2L Jedi

  • Members
  • PipPipPip
  • 823 posts
  • Gender:Male
  • Interests:Designer at Lee ter Wal.

Posted 27 November 2004 - 07:14 PM

People of P2L

Could you please help me with this PHP problem.

I want to make a tutorial dB on my website. It will work like this:

1.) Fill out a form on the website including Title, Author, Author Email, Description, Tutorial, Category (drop down selection) and Website.
2.) The info is inserted into a MYSQL dB
3.) The Title, Author and Description fields are added to the beggining of the catalog in the right category
4.) When someone clicks on the Title of a tutorial the information is pulled out from the dB and the correct information is placed into a template. For example.

Field 1: id="$title"
Field 2: id="$author"
Field 3: id="$email"
Field 4: id="$Description"
Field 5: id="$Tutorial"
Field 6: id="$category"
Field 7: id="$website"

Those are the names of the fields and what will be in the template for viewing them.

Eg of template:

<Banner>
<table start>
Title: $title
Autor: $author
Description: $description
<table end>

$tutorial

To find out more about this tutorial visit $website for more information or email $author on $email

End Template


How would I accomplish this?

Thanks for your help
--
--InFnit

#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 29 November 2004 - 06:17 PM

<form action="upload_page.php" name="submit" method=POST ENCTYPE="multipart/form-data">
<table align="center"><tr> 
	<td>Title: </td>
	<td><input name="title" type="text" maxlength="30"></td>
  </tr>
	<tr>
	<td>Author: </td>
	<td><input name="author" type="text" maxlength="30"></td>
  </tr>
	<tr>
	<td>E-mail: </td>
	<td><input name="email" type="text" value="[email protected]">
  </td>
  </tr>
    </tr>
	<tr>
	<td>Website: </td>
	<td><input name="website" type="text" value="http://">
  </td>
  </tr>
	<tr>
	<td>Tutorial: </td>
	<td><input name="tutorial" type="file"><br>
	(must be in html form)
  </td>
  </tr>
	<tr>
	<td>Description: </td>
	<td><textarea name="description" cols="30" rows="3"></textarea></td>
  </tr>
	<tr>
	<td>Category: </td>
	<td><select name="cat">
  <option value=""></option>
  <option value="1">cat_name
  <option value="2">cat_name2
  </select></td>
  </tr>
	<tr>
	<td colspan=2 align="center">
  <input type="submit" value="Submit" name="submit"></td>
  </tr></table>
</form>

<?
include('config.php');

/* following if statments check to see if everything's filled out */ 
if($_POST['submit']){
	if(!$_POST['title'] || !$_POST['author'] || !$_POST['description']){
  die("Error! : Info Missing, make sure title, author, and description are filled out<br><a href=\"javascript: history.back(-2)\">back</a>");
	}
	if(!$_POST['email'] || !$_POST['website']){
  die ("Error! : No email or website entered<br><a href=\"javascript: history.back(-2)\">back</a>");
	}
	
	/* uploades the .html file for display */
	if($_FILES['tutorial']['type'] == "text/html"){
  if(file_exists("tutorials/".$_FILES['tutorial']['name'])){
  	die("".$_FILES['tutorial']['name']." already exists, please rename and try again.");
  }else{
  	copy($_FILES['tutorial']['tmp_name'], "tutorials/".$_FILES['tutorial']['name']) or die("Could not copy file.");
  	echo "Upload Complete...\n";
  }
	}else{
  die("Could Not Copy: Allowed file type- .html");
	}

	/* sets some variables and makes sure the description doesn't have any html in it */
	$file = $_FILES['tutorial']['name'];
	$des = htmlspecialchars($_POST['description']);

	/* uploads everything */
	$sql = mysql_query ("INSERT INTO `table_name`
	VALUES ('$_POST[title]', '$_POST[author]', '$des', 'tutorials/$file', '$_POST[website]', '$_POST[email]', '$_POST[cat]', '')")
	or die(mysql_error());
	echo "Submit Successful"; 
}
?>

<?
/* copy this into an install file and it will creat a database for you, or just modify the display code to match a current database
make sure with this table you use numbers for the catagory ids and tutorial ids
the file just be a path to the file on your server (i.e. /tutorials/abstract1.php) */
mysql_query("CREATE TABLE `table_name` (
  `title` varchar(255) NOT NULL default '',
  `author` varchar(255) NOT NULL default '',
  `description` text NOT NULL,
  `file` varchar(100) NOT NULL default '',
  `website` varchar(255) NOT NULL default '',
  `email` varchar(255) NOT NULL default '',
  `cat` int(11) NOT NULL default '',
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM PACK_KEYS=0 AUTO_INCREMENT=1;");
?>

<?
/* database connection info */
include('config.php');

/* cat = category of tutorial, id = unique id givin to the tutorial the user wants to view table_name = the name of the table the tutorials are on */
$sql = mysql_query("SELECT * FROM `table_name`
WHERE cat = '$cat' AND id = '$id'");

while($r=mysql_fetch_array($sql)){
	echo "<table><tr><td>
	Title: $r[title]
	Autor: $r[author]
	Description: $r[description]
	</td></tr></table>";
/* include the tutorial's file so you don't have to store it in the database */
include("$r[file]");

echo "To find out more about this tutorial visit $r[website] for more information or email $r[author] on $r[email]";
}
?>

should work... maybe not exactly to spec, but you should be able to figure out how to change some stuff or get some more help on it (p.s. no i didn't write it all just for you, i copied and pasted what i already had for my tutorial system)

p.s. pay attention to the different sets of php code, they should probably all be different pages

#3 ludwigw

ludwigw

    P2L Jedi

  • Members
  • PipPipPip
  • 823 posts
  • Gender:Male
  • Interests:Designer at Lee ter Wal.

Posted 30 November 2004 - 12:21 AM

Thanks :ph34r:

How do I know where one set starts and ends? And what would I call the different sets? O, last question - you have a demo?

#4 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 30 November 2004 - 11:03 AM

you can tell by the php tags <? ?>
i.e.
<?
blahblahblha
?>

<?
more blahblah
?>

you can probably name them what ever you want, as long as you keep track of what's where

the form and the first set of <? stuff ?> can be on the same page... adjusts the forms action to what you name it

the 2nd set of <? stuff ?> is your database config, it's optional to use, but you can just upload and view the file, then it should automaticly update the database for you (alter the table name first)

finally, the last set is to display everthing... no special name needed...

semi-similar demo can be found at http://graphic-desig...x.php?id=submit

#5 Gio

Gio

    Jedi In Training

  • Members
  • PipPip
  • 317 posts

Posted 30 November 2004 - 08:18 PM

This is more of a script request than a tutorial request though...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users