|
Posted by edward_sanders on 02/15/07 22:43
Ok Steve,
I tried that echo statement you gave and the
results are not what I was expecting or can make
sense of at all. I get everything right with the code
except for the fact that the ID is not getting passed. I
checked the db from the mysql cmd prompt and got
this information, which is wierd because ID is spelled
just like it is my code. So that wasn't the problem.
Here are the fields which I got from
Describe jokes; :::
ID int(11) Not Null, Pri, auto_increment
JokeText type: text NULL
JokeDate type: date not null
AID type int(11) Null
The last row was added to link with a second table
created later to hold the joke authors/submitters.
Now the code itself so that one can see everything is as follows:
<?php
if (isset($_GET['addjoke'])) :
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="POST">
<p>Type your joke here:</p><br />
<TEXTAREA NAME="joke" ROWS=10 COLS=40 WRAP>
</TEXTAREA><BR />
<INPUT TYPE=SUBMIT NAME="submitjoke" VALUE="SUBMIT">
</FORM>
<?php
else:
//echo("This is a test <br />"); Used for debugging
// Connect to the database server
$dbcnx = mysql_connect("localhost", "user", "password");
// not the actual password above
if ( !$dbcnx) {
echo("<p>Unable to connect to the " .
"database server at this time.</p>");
exit();
} /* else { Used for debugging
echo("I connected");
} */
// Select the jokes database
if (!mysql_select_db("jokes", $dbcnx)) {
echo("<p>Unable to connect to the jokes" .
"database at this time.</p>");
exit();
}
// If a joke has been submitted,
// add it to the database
$joketext = $_POST['joke'];
if ("SUBMIT" == $_POST['submitjoke']) {
$sql = "INSERT INTO jokes SET " .
"JokeText='$joketext', " .
"JokeDate=CURDATE()";
if (mysql_query($sql)) {
echo("<p>Your joke has been added.</p>");
} else {
echo ("<p>Error adding submitted joke: " .
mysql_error() . "</p>");
}
}
// If a joke has been deleted,
// remove it from the database
if (isset($_GET['deletejoke'])) {
$deletejoke=$_GET['deletejoke'];
echo("<p>The joke to delete is number $deletejoke");
$sql = "DELETE FROM jokes " .
"WHERE ID=$deletejoke";
if (mysql_query($sql)) {
echo("<p>The joke has been deleted.</p>");
} else {
echo("<p>Error deleting joke: " .
mysql_error() . "</p>");
}
}
echo("<p>Here are all the jokes " .
"in our database: </p>");
// Request the text of all the jokes
$result = mysql_query("SELECT JokeText FROM jokes");
if ( !$result ) {
echo ("<p>Error performing query: " .
mysql_error() . "</p>");
exit();
}
echo("<blockquote>");
// Display the text of each joke in the paragraph
while ( $row = mysql_fetch_array($result)) {
$jokeid = $row["ID"];
echo ($row["ID"]);
$deletejoke=$jokeid;
$joketext = $row["JokeText"];
$thispage = $_SERVER["PHP_SELF"];
echo("<p>$joketext " .
"<A HREF='$thispage?deletejoke=$jokeid'>" .
"Delete this Joke</a></p>");
}
echo("</blockquote>");
// When clicked, this link will load this page
// with the joke submission form displayed.
?>
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>?addjoke=1">
Add a Joke, by clicking here!</a></p>
<?php
endif;
?>
>>>>
Thanks,
Bruce
"Steve" <no.one@example.com> wrote in message
news:FjPAh.1138$Ry.506@newsfe05.lga...
>
> "edward_sanders" <edward_sanders@bellsouth.net> wrote in message
> news:mCMAh.9466$6a.142@bignews4.bellsouth.net...
> | Hi,
> | This is a newbie question. I am using a text for learning
> php/mysql.
> | I am using PHP 5.2.x and mysql 5.
> | The example is that of a mysql
> | database of jokes. Before we get to joins there is a
> | simple table with 3 fields, ID field (primary key, integer),
> | JokeText, and JokeDate. In the program for some
> | reason the code is not retrieving the ID for each
> | row from the db. It gets the JokeText field just fine.
> | Let me include the snippets below.
> |
> | This is where each row (each joke) is to be displayed.
> | Note that the ID is used to give the option of deleting a
> | joke from the db. The link tag is supposed to get that
> | ID passed to the link but that isn't happening. Please help.
> |
> | // Display the text of each joke in the paragraph
> | while ( $row = mysql_fetch_array($result)) {
> | $jokeid = $row["ID"];
> | echo ($row["ID"]);
> | $deletejoke=$jokeid;
> | $joketext = $row["JokeText"];
> | $thispage = $_SERVER["PHP_SELF"];
> | echo("<p>$joketext " .
> | "<A HREF='$thispage?deletejoke=$jokeid'>" .
> | "Delete this Joke</a></p>");
> | }
> |
> | Then the code to delete the joke ( the row from the
> | db) is as follows:
> | // If a joke has been deleted,
> | // remove it from the database
> | if (isset($_GET['deletejoke'])) {
> | $deletejoke=$_GET['deletejoke'];
> | echo("<p>The joke to delete is number $deletejoke");
> | $sql = "DELETE FROM jokes " .
> | "WHERE ID=$deletejoke";
> | if (mysql_query($sql)) {
> | echo("<p>The joke has been deleted.</p>");
> | } else {
> | echo("<p>Error deleting joke: " .
> | mysql_error() . "</p>");
> | }
> | }
>
> dunno. in the middle of your while loop, insert:
>
> echo '<pre>' . print_r($row, true) . '</pre>';
>
> it could be something as simple as a capitalization problem with the key
> being accessed in the $row variable, i.e. $row['id'] instead of
> $row['ID'].
> spewing the variable to the browser will help pinpoint issues like that.
>
> finally, if this code is straight out of a book...throw it away now! read
> 'code complete' and you'll understand how many 'bad' things are going on
> in
> just that snippet - the rest must be equally dreadful. if you'd like (and
> supply your joke db table structure), i'll give you more of a stardardized
> and more manageable script to look at.
>
> hth
>
>
Navigation:
[Reply to this message]
|