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?
$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
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 '%$searchtext%'";
}
Then there are only issues like SQL injection to worry about with the remaining code.
$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%’";
}
?>
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
$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%’";
}
?>
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
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