Results 1 to 3 of 3

Thread: Tutorial: The Basics of PHP/MySQL in One Script

  1. #1
    Registered+
    Join Date
    Jun 2005
    Location
    Hereford, UK
    Posts
    29
    Thanks
    0
    Thanked
    0 times in 0 posts

    Tutorial: The Basics of PHP/MySQL 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.

    Computer Technician -- Now a smug daddy!

  2. #2
    Bigger than Jesus Norky's Avatar
    Join Date
    Feb 2005
    Posts
    1,579
    Thanks
    1
    Thanked
    8 times in 8 posts
    Just a pointer, but you should use $_GET['p'] instead of just $p as some users may not have globals on.

    Also, instead of having if($p=='blah'), if($p=='blah2'), etc., a switch would be much neater.

  3. #3
    Registered+
    Join Date
    Jun 2005
    Location
    Hereford, UK
    Posts
    29
    Thanks
    0
    Thanked
    0 times in 0 posts
    see thats what I mean, i have never used $_GET because it confuses >me< and I've always read tutorials with $p

    Computer Technician -- Now a smug daddy!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. msn / internet explorer script error
    By grimpy in forum Software
    Replies: 0
    Last Post: 21-05-2005, 07:35 PM
  2. Script to compare number in text files?
    By wasabi in forum Software
    Replies: 5
    Last Post: 09-05-2005, 04:10 PM
  3. Replies: 3
    Last Post: 29-04-2004, 07:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •