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.