Jokelist.php on Kevin yank's book

Hy to everybody,
I’m trying to follow the booke step by step, and I find something curios when I try to run the jokelist.php page.
This is what appear when I run It:

Manage Jokes

Notice: Undefined index: aid in D:\Programmi\wamp\www\provephp\jokelist.php on line 29

Notice: Undefined index: cid in D:\Programmi\wamp\www\provephp\jokelist.php on line 34

Notice: Undefined index: searchtext in D:\Programmi\wamp\www\provephp\jokelist.php on line 40
Joke Text Options
Un vecchietto entra da un panettiere e chiede 5kg di pane; il panettiere gli dice: cos tanto? dopo le diventa duro; e il vecchietto: Allora me ne dia 10kg!! Edit | Delete
Un nero in negozio di elettrodomestici: vorrei una lavatrice; e il negoziante: Candy? Edit | Delete
New search

It looks like there is something wrong in “aid”, “cid” and “searchtext” command.
Could somebody help me?
Thanks in advance
Ciao
Alex

Hi Alex
The errors mean that you are trying to use a variable which hasnt yet been set up eg:



<?php echo $myVar; ?>

would generate an error as the $myVar variable hasnt been declared.

If I did


<?php
$myVar = 'Hello';

echo $myVar;
?>

Then it would print out the word Hello.

Likewise if you are checking for form variables BEFORE a form is submitted or checking for variables that dont exist in a form, the same error will occur
Can you post your code please?

Thak you Spikez.
this is the code:

$aid = $_POST[‘aid’];
if ($aid != ‘’) { // An author is selected
$where .= " AND authorid=‘$aid’";
}

$cid = $_POST[‘cid’];
if ($cid != ‘’) { // A category is selected
$from .= ‘, jokecategory’;
$where .= " AND id=jokeid AND categoryid=‘$cid’";
}

$searchtext = $_POST[‘searchtext’];
if ($searchtext != ‘’) { // Some search text was specified
$where .= " AND joketext LIKE ‘%$searchtext%’";
}
?>

It’s exactly the code I downloaded from this site to follow the book.
I quote only the part of the code that should generate the error, if you need the rest tell me.
Thanks again
ciao
Alex

So far so good :slight_smile:
That bit is fine, what does your form look like?

The $_POST index are causing the problem. When the page first starts and there is no data in $_POST, those notices will be generated.

The way to code around that issue is to set a default value, and then only if the $_POST index is set, assign it over the top of the default value.


$aid = '';
if (isset($_POST['aid']) {
    $aid = $_POST['aid'];
}

But you don’t want to do that, because you’ll need to repeat the same technique in many other places. Use a function to do the work for you, and your code then becomes clearer as well.


function get_post($index) {
    if (isset($_POST[$index])) {
        return $_POST[$index];
    }
    return '';
}

$aid = get_post('aid');
if ($aid != '') { // An author is selected
    $where .= " AND authorid='$aid'";
}

$cid = get_post('cid');
if ($cid != '') { // A category is selected
    $from .= ', jokecategory';
    $where .= " AND id=jokeid AND categoryid='$cid'";
}

$searchtext = get_post('searchtext']);
if ($searchtext != '') { // Some search text was specified
$where .= " AND joketext LIKE '&#37;$searchtext%'";
}

Then there are only issues like SQL injection to worry about with the remaining code.

Thanks again SpikeZ,
This is what appear on the browser when I run the script:

and this is the complete script:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<title>Joke CMS: Manage Jokes</title>
<meta http-equiv=“content-type”
content=“text/html; charset=iso-8859-1” />
</head>
<body>
<h1>Manage Jokes</h1>
<?php

$dbcnx = @mysql_connect(‘localhost’, ‘root’, ‘my password’);
if (!$dbcnx) {
exit('<p>Unable to connect to the ’ .
‘database server at this time.</p>’);
}

if (!@mysql_select_db(‘ijdb’)) {
exit('<p>Unable to locate the joke ’ .
‘database at this time.</p>’);
}

// The basic SELECT statement
$select = ‘SELECT DISTINCT id, joketext’;
$from = ’ FROM joke’;
$where = ’ WHERE 1=1’;

$aid = $_POST[‘aid’];
if ($aid != ‘’) { // An author is selected
$where .= " AND authorid=‘$aid’";
}

$cid = $_POST[‘cid’];
if ($cid != ‘’) { // A category is selected
$from .= ‘, jokecategory’;
$where .= " AND id=jokeid AND categoryid=‘$cid’";
}

$searchtext = $_POST[‘searchtext’];
if ($searchtext != ‘’) { // Some search text was specified
$where .= " AND joketext LIKE ‘%$searchtext%’";
}
?>

<table>
<tr><th>Joke Text</th><th>Options</th></tr>

<?php
$jokes = @mysql_query($select . $from . $where);
if (!$jokes) {
echo ‘</table>’;
exit(‘<p>Error retrieving jokes from database!<br />’.
'Error: ’ . mysql_error() . ‘</p>’);
}

while ($joke = mysql_fetch_array($jokes)) {
echo "<tr valign=‘top’>
";
$id = $joke[‘id’];
$joketext = htmlspecialchars($joke[‘joketext’]);
echo "<td>$joketext</td>
";
echo "<td><a href=‘editjoke.php?id=$id’>Edit</a> | " .
"<a href=‘deletejoke.php?id=$id’>Delete</a></td>
";
echo "</tr>
";
}
?>

</table>

<p><a href=“jokes.php”>New search</a></p>
</body>
</html>

What do you think?
Thanks also to pmw57, but Unluky I don’t understand exactly where I should change the script.
Let me know.
Ciao
Alex

Hi pmw57,
I try to change the php code with the one you wrote, but nothing i change.
Now instead of the error indicated above, the page give me only one row indicating an error on the row 47 of the code.
Do you have some other idea?
Thanks again
Ciao
Alex

Please show your code, and indicate which line is row 47

Here is my code, and the row 47 I think is the one displayed in red:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<title>Joke CMS: Manage Jokes</title>
<meta http-equiv=“content-type”
content=“text/html; charset=iso-8859-1” />
</head>
<body>
<h1>Manage Jokes</h1>
<?php

$dbcnx = @mysql_connect(‘localhost’, ‘username’, ‘mypassword’);
if (!$dbcnx) {
exit('<p>Unable to connect to the ’ .
‘database server at this time.</p>’);
}

if (!@mysql_select_db(‘ijdb’)) {
exit('<p>Unable to locate the joke ’ .
‘database at this time.</p>’);
}

// The basic SELECT statement
$select = ‘SELECT DISTINCT id, joketext’;
$from = ’ FROM joke’;
$where = ’ WHERE 1=1’;

$aid = $_POST[‘aid’];
if ($aid != ‘’) { // An author is selected
$where .= " AND authorid=‘$aid’";
}

$cid = $_POST[‘cid’];
if ($cid != ‘’) { // A category is selected
$from .= ‘, jokecategory’;
$where .= " AND id=jokeid AND categoryid=‘$cid’";
}

$searchtext = $_POST[‘searchtext’];
if ($searchtext != ‘’) { // Some search text was specified
$where .= " AND joketext LIKE ‘%$searchtext%’";
}
?>

<table>
<tr><th>Joke Text</th><th>Options</th></tr>

<?php
$jokes = @mysql_query($select . $from . $where);
if (!$jokes) {
echo ‘</table>’;
exit(‘<p>Error retrieving jokes from database!<br />’.
'Error: ’ . mysql_error() . ‘</p>’);
}

while ($joke = mysql_fetch_array($jokes)) {
echo "<tr valign=‘top’>
";
$id = $joke[‘id’];
$joketext = htmlspecialchars($joke[‘joketext’]);
echo "<td>$joketext</td>
";
echo "<td><a href=‘editjoke.php?id=$id’>Edit</a> | " .
"<a href=‘deletejoke.php?id=$id’>Delete</a></td>
";
echo "</tr>
";
}
?>

</table>

<p><a href=“jokes.php”>New search</a></p>
</body>
</html>

I said “I think” because I’m not sure I should count all the rows or just the rows with writen something on it.
Thanks again for the help.
ciao
Alex

Can you please post for us the actual error message that is given to you.

This is the message with my code:

Manage Jokes

Notice: Undefined index: aid in D:\Programmi\wamp\www\provephp\jokelist.php on line 29

Notice: Undefined index: cid in D:\Programmi\wamp\www\provephp\jokelist.php on line 34

Notice: Undefined index: searchtext in D:\Programmi\wamp\www\provephp\jokelist.php on line 40
Joke Text Options
Un vecchietto entra da un panettiere e chiede 5kg di pane; il panettiere gli dice: cos tanto? dopo le diventa duro; e il vecchietto: Allora me ne dia 10kg!! Edit | Delete
Un nero in negozio di elettrodomestici: vorrei una lavatrice; e il negoziante: Candy? Edit | Delete
New search

And This is what appear with your code inserted in:

Parse error: parse error in D:\Programmi\wamp\www\provephp\jokelist.php on line 47

to complete everithing I quote again the code with your suggest:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Joke CMS: Manage Jokes</title>
<meta http-equiv="content-type"
    content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Manage Jokes</h1>
<?php

$dbcnx = @mysql_connect('localhost', 'root', '');
if (!$dbcnx) {
  exit('<p>Unable to connect to the ' .
      'database server at this time.</p>');
}

if (!@mysql_select_db('ijdb')) {
  exit('<p>Unable to locate the joke ' .
      'database at this time.</p>');
}

// The basic SELECT statement
$select = 'SELECT DISTINCT id, joketext';
$from   = ' FROM joke';
$where  = ' WHERE 1=1';

function get_post($index) {
    if (isset($_POST[$index])) {
        return $_POST[$index];
    }
    return '';
}

$aid = get_post('aid');
if ($aid != '') { // An author is selected
    $where .= " AND authorid='$aid'";
}
 
$cid = get_post('cid');
if ($cid != '') { // A category is selected
    $from .= ', jokecategory';
    $where .= " AND id=jokeid AND categoryid='$cid'";
}

$searchtext = get_post('searchtext']);
if ($searchtext != '') { // Some search text was specified
$where .= " AND joketext LIKE '%$searchtext%'";
}
?>

<table>
<tr><th>Joke Text</th><th>Options</th></tr>

<?php
$jokes = @mysql_query($select . $from . $where);
if (!$jokes) {
  echo '</table>';
  exit('<p>Error retrieving jokes from database!<br />'.
      'Error: ' . mysql_error() . '</p>');
}

while ($joke = mysql_fetch_array($jokes)) {
  echo "<tr valign='top'>\
";
  $id = $joke['id'];
  $joketext = htmlspecialchars($joke['joketext']);
  echo "<td>$joketext</td>\
";
  echo "<td><a href='editjoke.php?id=$id'>Edit</a> | " .
      "<a href='deletejoke.php?id=$id'>Delete</a></td>\
";
  echo "</tr>\
";
}
?>

</table>

<p><a href="jokes.php">New search</a></p>
</body>
</html>

“$searchtext = get_post(‘searchtext’]);” is the line n° 47.
Let me know if you have some other idea.
Thanks again.
Ciao
Alex

I looked for a jokelist.php file but didn’t find one. Are you not using edition 4? Is this one of your “on your own” files?

In any case, look closely at the syntax of that line. You don’t see anything amiss]?

The original Script is on the first post. I downloaded it from this site.

In any case, look closely at the syntax of that line. You don’t see anything amiss]?

I’m sorry but my php knowledge is not so good to understand what is missed.
Could you help me?
Thanks
Ciao
Alex

Thanks mittineague I looked better and find the error.
Now it works :wink:
Ciao
Alex

Great!

If I had a dollar for every time a single . ; ’ " ( ) ` escaped my notice (cats walking on my keyboard don’t help any) and caused me a problem I could afford to take a nice vacation :wink: