I've been trying to learn how to make a form that
visitors can use to add, delete or modify information
on a MySQL table. I made such a form that worked when
I first jumped into MySQL a few weeks ago, but now I
can't seem to recreate it.

I'm following a tutorial at
http://hotwired.lycos.com/webmonkey/99/21/index3a_page6.html?tw=programming

However, I'm adapting it to my own table...

Database = world
Table = Continents
Rows:
CCode
Name1
Type1
Group
Hemisphere
ID1

The resulting form LOOKS great but doesn't work so
great. My table has just 12 rows, and I can see every
one of them represented:

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 if I
type something in the form and press submit, the
change isn't reflected in my MySQL table.

The weird thing is, in an earlier incarnation, I was
at least able to delete the Southern Ocean row - but
no other rows. Now that I've purged the last of the
error messages, I apparently can't add, delete or
modify anything.

I've struck out with three different PHP forums, so
I'm wondering if the problem might lie with my MySQL
table. The tutorial made extensive use of a field
named "id," which I replaced with "CCode." Does that
particular field have to be a primary key, foreign
key, etc.?

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

[PHP]
<?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>
[/PHP]



        
                
__________________________________
Do you Yahoo!?
Friends.  Fun.  Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/ 

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to