
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>
Bookmarks