need help calling an id from db
I've been out of this for a while and I need help getting started again. I am trying to create a page that lists newsletters from a db by thier perspective dates of creation, then these will then be choices to view. The page displays a list that will grow as teachers create more newsletters.
I used one of Ben's tutorials to create the newsletter creation pages, just a big address book changed into text boxes, but everything in that tutorial was posted to self and not displayed to view on a new page. here is my code for the newsletterview page:
PHP Code:
<?php
$db = mysql_connect ( "localhost", "username", "password");
mysql_select_db("database", $db);
$result = mysql_query("SELECT * FROM newsletterkaren",$db);
printf("Newsletter: %s<br>\n", mysql_result($result,0,"date"));
/* here is where I need some help. I want viewers to click on
a date (link) and then get taken to that newsletter via. the id.
The db has a unique id for each new newsletter... how do I code
this to be a link ( and each additional newsletter created ) and
call the db for its id ..? */
?>
On top of this, I need to be able to post each newsletter to a new page. Here is the code for the newsletter creation page:
<?PHP
session_start();
session_destroy();
?>
<html><title>Newsletter</title>
<body bgcolor="#CCCCCC">
<div align="left"><a href="http://www.aquariancharterschool.com/teacherspages/mskarenspage.html">Back
to Ms. Karen's Class</a><br>
</div>
<div align="center"> <font size="4"><b>Ms. Karen's Newletter Maker</b></font></div>
<p><a href="http://www.aquariancharterschool.com"><br>
Back to Ms. Karen's Master Page </a></p>
<p>This is a plain but easy to use newsletter form. Edit current or old newsletters
by clicking on newsletter names, also delete newsletters by clicking the delete
button next to the corresponding newsletter.</p>
<h2><font size="3">Old/ Current Newsletters</font></h2>
<?php
$db = @mysql_connect("localhost", "site", "password");
mysql_select_db("database",$db);
if ($submit) {
// here if no ID then adding else we're editing
if ($id) {
$sql = "UPDATE newsletterkaren SET month='$month',date='$date', title1='$title1',text1='$text1',title2='$title2',t ext2='$text2',title3='$title3',text3='$text3',titl e4='$title4',text4='$text4' WHERE id=$id";
} else {
$sql = "INSERT INTO newsletterkaren (month,date,title1,text1,title2,text2,title3,text3 ,title4,text4) VALUES ('$month','$date','$title1','$text1','$title2','$t ext2','$title3','$text3','$title4','$text4')";
}
// run SQL against the DB
$result = mysql_query($sql);
echo "Record updated/edited!<p>";
} elseif ($delete)
{
// delete a record
$sql = "DELETE FROM newsletterkaren WHERE id=$id";
$result = mysql_query($sql);
echo "$sql Record deleted!<p>";
} else {
// this part happens if we don't press submit
if (!$id) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM newsletterkaren",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s</a> \n", $PHP_SELF, $myrow["id"], $myrow["date"]);
printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);
}
}
?>
<form method="post" action="<?PHP echo $PHP_SELF?>">
<?PHP
if ($id) {
// editing so select a record
$sql = "SELECT * FROM newsletterkaren WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$id = $myrow["id"];
$month = $myrow["month"];
$date = $myrow["date"];
$title1 = $myrow["title1"];
$text1 = $myrow["text1"];
$title2 = $myrow["title2"];
$text2 = $myrow["text2"];
$title3 = $myrow["title3"];
$text3 = $myrow["text3"];
$title4 = $myrow["title4"];
$text4 = $myrow["text4"];
// print the id for editing
?>
<input type=hidden name="id" value="<?PHP echo $id ?>">
<?PHP
}
?>
Month:
<input type="Text" name="month" value="<?PHP echo $month ?>"><br>
Date:<input type="Text" name="date" value="<?PHP echo $date ?>"><br>
Title 1:<input type="Text" name="title1" value="<?PHP echo $title1 ?>"><br>
Text 1:<br><TEXTAREA name="text1" wrap cols=100 rows=5><?PHP echo $text1 ?></TEXTAREA><br>
Title 2:<input type="Text" name="title2" value="<?PHP echo $title2 ?>"><br>
Text 2:<br><TEXTAREA name="text2" wrap cols=100 rows=5><?PHP echo $text2 ?></TEXTAREA><br>
Title 3:<input type="Text" name="title3" value="<?PHP echo $title3 ?>"><br>
Text 3:<br><TEXTAREA name="text3" wrap cols=100 rows=5><?PHP echo $text3 ?></TEXTAREA><br>
Title 4:<input type="Text" name="title4" value="<?PHP echo $title4 ?>"><br>
Text 4:<br><TEXTAREA name="text4" wrap cols=100 rows=5><?PHP echo $text4 ?></TEXTAREA><br>
<p>
<input type="Submit" name="submit" value="Enter information">
<a href="<?PHP echo $PHP_SELF?>">Clear and Refresh</a>
</form>
<?PHP
}
?>
The only errors I am getting are from the list page for the newsletters. It is funny, the second page here will allow me to add and edit the newsletters, but they display on the same page...I want them to have their own pages...
Thank you for any help.
Edit: Please use the correct syntaxing, and please read the rules before posting
Edit2: Broke that gigantic, table braking sentence in half.
__________________
bow wow!
Last edited by ejbe99 : September 2nd, 2002 at 01:27 PM.
|