WebmasterForums.NET - Webmaster Forums & Directory
 

Go Back   WebmasterForums.NET - Webmaster Forums & Directory > Design and Development > Site Programming and Coding
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Site Programming and Coding Have a problem with some code on your site? Ask our other members for advice and help.

Reply
 
Thread Tools Display Modes
Old 02-27-2006, 05:03 AM   #1
Durinthiam
Junior Member
 
Join Date: Feb 2006
Posts: 4
Durinthiam is on a distinguished road
Default Basic PHP/Mysql Tutorial in one script

When I was learning PHP i found it very difficult to find two people who used the same layout for code in their tutorials, so now I have written an annotated script for all people wanting to use PHP to view/edit/delete from a MySQL Database

first make an database called mydatabase in mysql

then execute this command in SQL

Code:
CREATE TABLE `quotes` (
  `id` int(11) NOT NULL auto_increment,
  `quote` text NOT NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=6 ;
What that does is:
A: makes a table named quotes
B: makes a row named id that is an integer value with auto_increment (each time a new entry is added it is assigned a unique number)
C: makes a row called quote that is raw text

now to connect to the database, make a file named db.php and enter in
PHP Code:
<?
mysql_pconnect
("localhost","username","password"); // host,username,password, host usually doesnt need to be changed from localhost but change username and password to what your MySQL username and password is
mysql_select_db("mydatabase"); // Select Database
?>
This file, when included on a page will connect to the mysql database.

Now make a file named index.php, this is the core script. Copy and paste the following code,

PHP Code:
<? include("db.php"); //include the file "db.php" which connects to the database, using an include saves alot of time when you have alot of pages connecting to the same database ?> 
<html>
<head>
<title>I Can Quote!</title>
</head>

<body>
<? if(!$p) { //if the page is index.php, then display the following ?>
<form name="form1" method="post" action="index.php?p=add">
  <input type="text" name="quote">
  <br>
  <input name="Submit" type="submit" class="s1" value="Submit">
</form><br>
<a href="index.php?p=quotes">View Quotes</a>
<? //End of display
if ($p == "add") { //if the page is "index.php?p=add" then do the following
mysql_query("INSERT INTO quotes (id,quote)"."VALUES ('NULL', '$quote')"); // Insert into the table "quotes", the quote ID number and quote
//how do we know if it went in? well now we display the following 
?>
Congratulations, the quote was added to the database. returning to main page
<META HTTP-EQUIV="Refresh" CONTENT="2; URL=index.php">
<? //End of Display
if ($p == "quotes") { //if the page is "index.php?p=quotes" then do the following


$result mysql_query("select * from quotes");
while(
$r=mysql_fetch_array($result))
{    
   
$id=$r["id"];
   
$quote=$r["quote"];
   
// fetch the data from table Quotes
   // ...aaaand display it
?>
<? 
echo "$quote"?> :: <a href="index.php?p=edit&id=<? echo "$id"?>">Edit Quote</a> :: <a href="index.php?p=delete&id=<? echo "$id"?>">Delete Quote</a><BR>
<?
}
//end if
if ($p == "delete") {
mysql_query("delete from quotes where id=$id"// If the page is "delete" then delete the quote that matches ID number ?>
The Quote Was Deleted 
<META HTTP-EQUIV="Refresh" CONTENT="2; URL=index.php">

<? 
if (
$p == "edit") { 
$result2 mysql_query("select * from quotes where id=$id"); //select row from quotes table with specified ID
while($r2=mysql_fetch_array($result2))
{    
   
$id=$r2["id"];
   
$quote=$r2["quote"]; ?>

<form name="form2" method="post" action="index.php?p=editquote&id=<? echo $id ?>">
  <input type="hidden" name="mid" value="<? echo $id ?>">
  <input name="quote" type="text" value="<? echo $quote ?>">
  <br>
  <input name="Submit" type="submit" class="s1" value="Submit">
</form>
<? } }
if (
$p == "editquote") { 
$sql "UPDATE quotes SET quote='$quote' WHERE id='$id'"//update the row with new quote where the row ID = $ID

$result MYSQL_QUERY($sql);
echo 
mysql_error();
?>
done forwarding back to mainpage<META HTTP-EQUIV="Refresh" CONTENT="2; URL=index.php">
<?
//endif
//Thats it!
?>
</body>
</html>
This script is the barebone for any news system on a website, from what you have just made you can adapt it to have more fields and become a complete script to suite your needs

If you think I can expand on the annotations, please let me know.
Durinthiam is offline   Reply With Quote
Old 03-04-2006, 07:07 PM   #2
PDZ
Member
 
Join Date: Mar 2006
Posts: 52
PDZ is on a distinguished road
Default

Ah nice little script here, can be useful to beginners. I had made a tutorial like this on my old website I can put it up if I find it.
PDZ is offline   Reply With Quote
Old 03-04-2006, 07:38 PM   #3
John D
Senior Member
 
Join Date: Jan 2002
Posts: 402
John D will become famous soon enoughJohn D will become famous soon enough
Default

Very nice post

Did you do this yourself?

Added good rep
__________________

DeuceAce - A mature poker forum
Read my blog!
John D is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 02:51 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0
WebmasterForums.NET - Webmaster Forums & Directory