SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need Help With Basic Add/Delete Forms

    I'm still fairly new to MySQL, PHP and databases. I'm now fairly comfortable with making and displaying MySQL tables and even basic table joins. Unfortunately, I seem to have forgotten one of the most basic lessons.

    One of the first tutorials I studied taught how to make PHP forms that allow you to enter and delete data remotely. In other words, a visitor could access a page with one of these forms, type in their name and address and press "Submit," and the information would be entered into your database.

    My form was working, but it appears that the MySQL table that went with it got deleted. So I tried to adapt it to a new table, and it just blew up in my face.

    I'm working with a database named "world" and a table named "continents." The table has six fields, with the following names:

    CCode
    Name1
    Type1
    Group
    Hemisphere
    ID1

    I may not even be using the correct form. I think what I ultimately want is a form that will let me do the following:

    1. Add or delete a row
    2. Enter, delete or modify text in any cell

    Below is all the code from my page at present. Can someone tell me how to modify it so it lets me manipulate rows and cells?

    PHP Code:
    <html>
    <?php
    if($submit)
    {
    $db mysql_connect("localhost""USERNAME""PASSWORD");
    mysql_select_db("world",$db);
    $sql "INSERT INTO continents (CCode, Name1, Type1, Group, Hemisphere, ID1)
    VALUES ('
    $CCode','$Name1','$Type1','$Group','$Hemisphere', '$ID1')";
    $result mysql_query($sql);
    echo 
    "Thank you! Information entered.\n";
    }
    else if(
    $update)
    {
    $db mysql_connect("localhost""USERNAME""PASSWORD");
    mysql_select_db("world",$db);
    $sql "UPDATE continents SET CCode='$CCode',
    Name1='
    $Name1', Type1='$Type1', Group='$Group', Hemisphere='$Hemisphere', ID1='$ID1' WHERE ID1=$ID1";
    $result mysql_query($sql);
    echo 
    "Thank you! Information updated.\n";
    }
    else if(
    $id)
    {
    $db mysql_connect("localhost""USERNAME""PASSWORD");
    mysql_select_db("world",$db);
    $result mysql_query("SELECT * FROM continents WHERE ID1=$ID1",$db);
    $myrow mysql_fetch_array($result);
    ?>
    <form method="post"action="<?php echo $PHP_SELF?>">
    <input type="hidden"name="id"value="<?php echo $myrow["ID"]?>">
    CCode:<input type="Text"name="CCode"value="<?php echo $myrow["CCode"]?>"><br>
    Name1:<input type="Text" name="Name1" value="<?php echo $myrow["Name1"]?>"><br>
    Type1:<input type="Text" name="Type1" value="<?php echo $myrow["Type1"]?>"><br>
    Group:<input type="Text" name="Group" value="<?php echo $myrow["Group"]?>"><br>
    Hemisphere:<input type="Text" name="Hemisphere" value="<?php echo $myrow["Hemisphere"]?>"><br>
    ID1:<input type="Text" name="ID1" value="<?php echo $myrow["ID1"]?>"><br>
    <input type="Submit" name="update" value="Update information"></form>
    <?
    }
    else
    {
    ?>
    <form method="post" action="<?php echo $PHP_SELF?>">
    CCode:<input type="Text" name="CCode"><br>
    Name1:<input type="Text" name="Name1"><br>
    Type1:<input type="Text" name="Type1"><br>
    Group:<input type="Text" name="Group"><br>
    Hemisphere:<input type="Text" name="Hemisphere"><br>
    ID1:<input type="Text" name="ID1"><br>
    <input type="Submit" name="submit" value="Enter information"></form>
    <?
    }
    ?>
    </html>
    Also, I have a related question. Now that I'm beginning to understand databases, it just occurred to me that I could create a MySQL table with cells for entire articles. For example, the Africa row could include a cell named "Article" that includes a ten-paragraph article about Africa.

    That way, none of my pages would have much between the <body></body> tags except a dynamic table displaying only the table cell that features the article matched to that page.

    The next step is to modify it so that a visitor or client can write an article about South America, access the page with the form, paste their South America article into the proper cell, and submit it.

    Does it sound like I'm on the right track?

    Thanks.

  2. #2
    SitePoint Zealot Pie's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First off, in your code why do you have:
    <?php
    <html>... remove the <?php

    Secondly you dont need to connect to the database per query.
    Can i reccomend reading a few more tutorialas before going out and doing this
    Zach Inglis
    Reality Art Studios Ltd
    ZachInglis.com Blog |
    Art / Code Chat & Tutorials | Work

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pie
    First off, in your code why do you have:
    <?php
    <html>... remove the <?php

    Secondly you dont need to connect to the database per query.
    Can i reccomend reading a few more tutorialas before going out and doing this

    Where do you see the word "remove"? I did a word search and can't find it.

    I'm using a tutorial at http://hotwired.lycos.com/webmonkey/...tw=programming, which seems to be pretty good. But it isn't working for me.

    This works:

    PHP Code:
    <?php
    $db 
    mysql_connect("localhost""USERNAME""PASSWORD");
    mysql_select_db("world",$db);
    $result mysql_query("SELECT * FROM continents",$db);
    printf("CCode: %s<br>\n"mysql_result($result,0,"CCode"));
    printf("Name1: %s<br>\n"mysql_result($result,0,"Name1"));
    printf("Type1: %s<br>\n"mysql_result($result,0,"Type1"));
    printf("Group: %s<br>\n"mysql_result($result,0,"Group"));
    printf("Group: %s<br>\n"mysql_result($result,0,"Hemisphere"));
    printf("Group: %s<br>\n"mysql_result($result,0,"ID1"));
    ?>
    It displays a the first value in each field. But I get lost at the next step:

    PHP Code:
    <?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>

    <?php
    if ($submit) {
      
    // process form
      
    while (list($name$value) = each($HTTP_POST_VARS)) {
        echo 
    "$name = $value<br>\n";
      }
    } else{
      
    // display form
      
    ?>
      <form method="post" action="<?php echo $PHP_SELF?>">
    CCode:<input type="Text" name="CCode"><br>
    Name1:<input type="Text" name="Name1"><br>
    Type1:<input type="Text" name="Type1"><br>
    Group:<input type="Text" name="Group"><br>
    Hemisphere:<input type="Text" name="Hemisphere"><br>
    ID1:<input type="Text" name="Group"><br>
      <input type="Submit" name="submit" value="Enter information">
      </form>
      <?php
    // end if
    ?>

    <?php
    if ($submit) {
      
    // process form
      
    $db mysql_connect("localhost""USERNAME""PASSWORD");
      
    mysql_select_db("world",$db);
      
    $sql "INSERT INTO continents (CCode,Name1,Type1,Group,Hemisphere,ID1) VALUES ('$CCode','$Name1','$Type1','$Group', '$Hemisphere', '$ID1')";
      
    $result mysql_query($sql);
      echo 
    "Thank you! Information entered.\n";
    } else{
      
    // display form
      
    ?>
      <form method="post" action="<?php echo $PHP_SELF?>">
    CCode:<input type="Text" name="CCode"><br>
    Name1:<input type="Text" name="Name1"><br>
    Type1:<input type="Text" name="Type1"><br>
    Group:<input type="Text" name="Group"><br>
    Hemisphere:<input type="Text" name="Hemisphere"><br>
    ID1:<input type="Text" name="Group"><br>
      <input type="Submit" name="submit" value="Enter information">
      </form>
    <?php
    // end if
    ?>
    </body>
    </html>
    The following looks more interesting, but it still doesn't work. I've tweaked and reconfigured the code, but even if the forms "thanks" me for submitting data, I never see any changes in the table named "continents."

    PHP Code:
    <?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>

    <?php
    $db 
    mysql_connect("localhost""USERNAME""PASSWORD");
    mysql_select_db("world",$db);
    if (
    $id) {
      if (
    $submit) {
        
    $sql "UPDATE continents SET CCode='$CCode',Name1='$Name1',Type1='$Type1',Group='$Group',Hemisphere='$Hemisphere',ID1='$ID1' WHERE ID1=$ID1";
        
    $result mysql_query($sql);
        echo 
    "Thank you! Information updated.\n";
      } else {
        
    // query the DB
        
    $sql "SELECT * FROM continents WHERE ID1=$ID1";
        
    $result mysql_query($sql);    
        
    $myrow mysql_fetch_array($result);
        
    ?>
        <form method="post" action="<?php echo $PHP_SELF?>">
        <input type=hidden name="id" value="<?php echo $myrow["id"?>">
        CCode:<input type="Text" name="CCode" value="<?php echo $myrow["CCode"?>"><br>
        Name:<input type="Text" name="Name1" value="<?php echo $myrow["Name1"?>"><br>
        Type:<input type="Text" name="Type1" value="<?php echo $myrow["Type1"?>"><br>
        Group:<input type="Text" name="Group" value="<?php echo $myrow["Group"?>"><br>
        <input type="Submit" name="submit" value="Enter information">
        </form>
        <?php
        
    }
    } else {
      
    // display list of employees
      
    $result mysql_query("SELECT * FROM continents",$db);
      while (
    $myrow mysql_fetch_array($result)) {
        
    printf("<a href=\"%s?id=%s\">%s %s</a><br>\n"$PHP_SELF$myrow["CCode"], $myrow["Name1"], $myrow["Typel"]);
      }
    }
    ?>

    </body>
    </html>

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the latest update:

    My latest modifications have removed the last of the error messages, and the form looks better than ever. But it has lost all functional value! I can't even delete the last row now.

    Africa continent (DELETE)
    Antarctica continent (DELETE)
    Australia continent (DELETE)
    Eurasia continent (DELETE)
    Middle East continent (DELETE)
    North America continent (DELETE)
    South America continent (DELETE)
    Arctic Ocean ocean (DELETE)
    Atlantic Ocean ocean (DELETE)
    Indian Ocean ocean (DELETE)
    Pacific Ocean ocean (DELETE)
    Southern Ocean ocean (DELETE)

    ADD A RECORD

    [The following precede form input boxes]
    CCode:
    Name1:
    Type1:
    Group:
    Hemisphere:
    ID1:

    * * * * * * * * * *

    Everything above is linked, except the last six lines, which merely identify form input boxes.

    If I click the linked word "Africa," it takes me to this URL:

    http://localhost/geowebworks/php/exp3.php?CCode=caf

    That looks right; caf (for "continent-Africa") is the value for the CCode field on the Africa row. But nothing works.

    I posted the code below. Do you have any idea what's wrong?

    PHP Code:
    <?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>

    <body>
    <?php
    $db 
    mysql_connect("localhost""USERNAME""PASSWORD");
    mysql_select_db("world",$db);
    if (
    $submit) {
      
    // here if no ID then adding else we're editing
      
    if ($CCode) {
        
    $sql "UPDATE Continents SET CCode='$CCode',Name1='$Name1',Type1='$Type1',Group='$Group',Hemisphere='$Hemisphere',ID1='$ID1' WHERE CCode=$CCode";
      } else {
        
    $sql "INSERT INTO Continents (CCode,Name1,Type1,Group,Hemisphere,ID1) VALUES ('$CCode','$Name1','$Type1','$Group','$Hemisphere','$ID1')";
      }
      
    // run SQL against the DB
      
    $result mysql_query($sql);
      echo 
    "Record updated/edited!<p>";
    } elseif (
    $delete) {
        
    // delete a record
        
    $sql "DELETE FROM Continents WHERE CCode=$CCode";    
        
    $result mysql_query($sql);
        echo 
    "$sql Record deleted!<p>";
    } else {
      
    // this part happens if we don't press submit
      
    if (!$CCode) {
        
    // print the list if there is not editing
        
    $result mysql_query("SELECT * FROM Continents",$db);
        while (
    $myrow mysql_fetch_array($result)) {
          
    printf("<a href=\"%s?CCode=%s\">%s %s</a> \n"$PHP_SELF$myrow["CCode"], $myrow["Name1"], $myrow["Type1"], $myrow["Group"], $myrow["Hemisphere"], $myrow["ID1"]);
          
    printf("<a href=\"%s?CCode=%s&delete=yes\">(DELETE)</a><br>"$PHP_SELF$myrow["CCode"]);
        }
      }
      
    ?>
      <P>
      <a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>
      <P>
      <form method="post" action="<?php echo $PHP_SELF?>">
      <?php
      
    if ($id) {
        
    // editing so select a record
        
    $sql "SELECT * FROM Continents WHERE CCode=$CCode";
        
    $result mysql_query($sql);
        
    $myrow mysql_fetch_array($result);
        
    $id $myrow["CCode"];
        
    $Name1 $myrow["Name1"];
        
    $Type1 $myrow["Type1"];
        
    $Group $myrow["Group"];
        
    $Hemisphere $myrow["Hemisphere"];
        
    $ID1 $myrow["ID1"];
        
    // print the id for editing
        
    ?>
        <input type=hidden name="CCode" value="<?php echo $CCode ?>">
        <?php
      
    }
      
    ?>
      CCode:<input type="Text" name="CCode" value="<?php echo $CCode ?>"><br>
      Name1:<input type="Text" name="Name1" value="<?php echo $Name1 ?>"><br>
      Type1:<input type="Text" name="Type1" value="<?php echo $Type1 ?>"><br>
      Group:<input type="Text" name="Group" value="<?php echo $Group ?>"><br>
      Hemisphere:<input type="Text" name="Hemisphere" value="<?php echo $Hemisphere ?>"><br>
      ID1:<input type="Text" name="ID1" value="<?php echo $ID1 ?>"><br>
      <input type="Submit" name="submit" value="Enter information">
      </form>
    <?php
    }
    ?>
    </body>
    </html>


Bookmarks

Posting Permissions

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