php/mysql syntax help

php/mysql syntax help

am 15.02.2007 00:12:27 von edward_sanders

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("

$joketext " .
"" .
"Delete this Joke

");
}

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("

The joke to delete is number $deletejoke");
$sql = "DELETE FROM jokes " .
"WHERE ID=$deletejoke";
if (mysql_query($sql)) {
echo("

The joke has been deleted.

");
} else {
echo("

Error deleting joke: " .
mysql_error() . "

");
}
}

Thanks in advance for any help,
Bruce

Re: php/mysql syntax help

am 15.02.2007 02:35:47 von Rik

On Thu, 15 Feb 2007 00:12:27 +0100, edward_sanders =

wrote:

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

Is the 'ID' not in the query result, or doesn't it get passed?

> // Display the text of each joke in the paragraph
> while ( $row =3D mysql_fetch_array($result)) {

What is teh query you're fetching the result from?
What does a print_r($row) tell you?

> echo("

$joketext " .
> "" .
> "Delete this Joke

");

What's the resulting link in the HTML source?

> $deletejoke=3D$_GET['deletejoke'];


Please use an intval() on the GET variable here. Consider what will happ=
en =

if someone was to use '1 OR 1=3D1' as the GET variable to your page...
-- =

Rik Wasmus

Re: php/mysql syntax help

am 15.02.2007 02:39:52 von Rik

Oh, BTW: please look up what multiposting and crossposting is, in
particular the general consensus about multiposting...

--
Rik Wasmus