I’m working my way through the book (5th edition), and was able to successfully connect to my joke database, list the jokes, and add a new joke (p 96-130). When I added the code that would let you delete a joke (p 130-138), rather than display the joke the browser shows:
“Warning: htmlspecialchars() expects parameter 1 to be string, array given in C:\xampp\htdocs\connect\addjoke\jokes.html.php on line 14”
Line 14 is part of the ig (get_magic_quotes_gpc()) section, specifically:
$process[] = &$process[$key][stipslashes($k)];
I didn’t change anything in that section when I added the delete functionality. What did I miss?
<?php
if (get_magic_quotes_gpc())
{
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list($key, $val) = each($process))
{
foreach ($val as $k => $v)
{
unset($process[$key][$k]);
if (is_array($v))
{
$process[$key][stripslashes($k)] = $v;
$process[] = &$process[$key][stipslashes($k)];
}
else
{
$process[$key][stripslashes($k)] = stripslashes($v);
}
}
}
unset($process);
}
if (isset($_GET['addjoke']))
{
include 'form.html.php';
exit();
}
try
{
$pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbuser', 'mypassword');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
include 'output.html.php';
exit();
}
if (isset($_POST['joketext']))
{
try
{
$sql = 'INSERT INTO joke SET
joketext = :joketext,
jokedate = CURDATE()';
$s = $pdo->prepare($sql);
$s->bindValue(':joketext', $_POST['joketext']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error adding submitted joke: ' . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
if (isset($_GET['deletejoke']))
{
try
{
$sql = 'DELETE FROM joke WHERE id = :id';
$s = $pdo->prepare($sql);
$s->bindValue(':id', $_POST['id']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error deleting joke: ' . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
try
{
$sql = 'SELECT id, joketext FROM joke';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching jokes: ' . $e->getMessage();
include 'error.html.php';
exit();
}
while ($row = $result->fetch())
{
$jokes[] = array('id' => $row['id'], 'text' => $row['joketext']);
}
include 'jokes.html.php';
?>
It’s worth noting that in any PHP version you’ll be using these days get_magic_quotes_gpc() will return false and the entire block can be removed. The 6th edition of the book gives a bit more detail on it as well.
Sir I am Reading the Book Novice To Ninja 5th Edition and I m using Xampp…which has PHP & Mysql
But Sir Whenever I tried To Use Including Files as’
include $_SERVER[‘DOCUMENT ROOT’] .‘/includes/filename.inc.php’;
it always shows this msg
Notice: Undefined index: DOCUMENT ROOT in C:\xampp\htdocs\Life\Delete Query\index.php on line 2
Notice: Undefined index: DOCUMENT ROOT in C:\xampp\htdocs\Life\Delete Query\index.php on line 51
Notice: Undefined index: DOCUMENT ROOT in C:\xampp\htdocs\Life\Delete Query\index.php on line 68
Please Help Me
Does $_SERVER has removed From the PHP or Nor or Why is this Happening to Me
Thanks Sir But Let Me Tell you After Using
include $_SERVER[‘DOCUMENT_ROOT’].‘/include/filename.inc.php’;
Now It is Showing
Warning: include(C:/xampp/htdocs/includes/magicquotes.inc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Life\Delete Query\index.php on line 2
Warning: include(): Failed opening ‘C:/xampp/htdocs/includes/magicquotes.inc.php’ for inclusion (include_path=‘C:\xampp\php\PEAR’) in C:\xampp\htdocs\Life\Delete Query\index.php on line 2
Warning: include(C:/xampp/htdocs/includes/db.inc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Life\Delete Query\index.php on line 51
Warning: include(): Failed opening ‘C:/xampp/htdocs/includes/db.inc.php’ for inclusion (include_path=‘C:\xampp\php\PEAR’) in C:\xampp\htdocs\Life\Delete Query\index.php on line 51
Notice: Undefined variable: pdo in C:\xampp\htdocs\Life\Delete Query\index.php on line 55
Fatal error: Call to a member function query() on null in C:\xampp\htdocs\Life\Delete Query\index.php on line 55
<?php
include $_SERVER['DOCUMENT_ROOT'] .'/includes/magicquotes.inc.php';
if(isset($_GET['addjoke']))
{
include 'form.html.php';
exit();
}
if(isset($_POST['joketext']))
{
include $_SERVER['DOCUMENT_ROOT'] .'/includes/db.inc.php';
try
{
$sql='INSERT into joke SET joketext=:joketext,jokedate=CURDATE()';
$s=$pdo->prepare($sql);
$s->bindValue(':joketext',$_POST['joketext']);
$s->execute();
}
catch(PDOException $e)
{
$error='Error in Adding New Joke '.$e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
if(isset($_GET['deletejoke']))
{
include $_SERVER['DOCUMENT_ROOT'] .'/includes/db.inc.php';
try
{
$sql='DELETE FROM joke WHERE id=:id';
$s=$pdo->prepare($sql);
$s->bindValue(':id',$_POST['id']);
$s->execute();
}
catch(PDOException $e)
{
$error='Error Deleting Joke '.$e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
include $_SERVER['DOCUMENT_ROOT'] .'/includes/db.inc.php';
try
{
$sql='SELECT id,joketext FROM joke';
$result=$pdo->query($sql);
}
catch(PDOException $e)
{
$error='Error Fetching Jokes '.$e->getMessage();
include 'error.html.php';
exit();
}
foreach($result as $row)
{
$jokes[]=array('id'=>$row['id'],'text'=>$row['joketext']);
}
include $_SERVER['DOCUMENT_ROOT']. '/includes/jokes.html.php';
This is My Code…,
Warning: include(C:/xampp/htdocs/includes/magicquotes.inc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Life\Delete Query\index.php on line 2
Warning: include(): Failed opening ‘C:/xampp/htdocs/includes/magicquotes.inc.php’ for inclusion (include_path=‘C:\xampp\php\PEAR’) in C:\xampp\htdocs\Life\Delete Query\index.php on line 2
Warning: include(C:/xampp/htdocs/includes/db.inc.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Life\Delete Query\index.php on line 51
Warning: include(): Failed opening ‘C:/xampp/htdocs/includes/db.inc.php’ for inclusion (include_path=‘C:\xampp\php\PEAR’) in C:\xampp\htdocs\Life\Delete Query\index.php on line 51
Notice: Undefined variable: pdo in C:\xampp\htdocs\Life\Delete Query\index.php on line 55
Fatal error: Call to a member function query() on null in C:\xampp\htdocs\Life\Delete Query\index.php on line 55
include $SERVER[‘DOCUMENTROOT’].‘/include/filename.inc.php’;
crrect the path
ex: filname.inc.php is in ‘/help/include/’
so your code will be
include $SERVER[‘DOCUMENTROOT’].‘/help/include/filename.inc.php’;
hope this fix the issue
Fatal error: Cannot use isset() on the result of a function call (you can use “null !== func()” instead) in C:\xampp\htdocs\Life\Show Jokes using Template helpers\index.php on line 3
I m receiving this error after using the following code
<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/includes/magicquotes.inc.php';
if(isset($_GET('addjoke'))
{
include $_SERVER['DOCUMENT_ROOT'].'/includes/ form.html.php';
exit();
}
if(isset($_POST['joketext']))
{
include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
try
{
$sql='INSERT INTO joke SET joketext=:joketext';
$s=$pdo->prepare($sql);
$s->bindValue(':joketext',$_POST['joketext']);
$s->execute();
}
catch(PDOException $e)
{
$error='Error Adding new jokes '.$e->getMessage();
include $_SERVER['DOCUMENT_ROOT'].'/includes/error.html.php';
exit();
}
header('Location: .');
exit();
}
if(isset($_GET('deletejoke'))
{
include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
try
{
$sql='DELETE FROM joke WHERE id=:id';
$s=$pdo->query($sql);
$s->bindValue(':id',$_POST['id']);
$s->execute();
}
catch(PDOException $e)
{
$error='Error in Deleting Jokes '.$e->getMessage();
include $_SERVER['DOCUMENT_ROOT'].'/includes/error.html.php';
exit();
}
header('Location: .');
exit();
}
include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
try
{
$sql='SELECT joke.id,joketext,name,email FROM joke INNER JOIN author ON authorid=author.id';
$result=$pdo->query($sql);
}
catch(PDOException $e)
{
$error='Error Fetching Jokes '.$e->getMessage();
include $_SERVER['DOCUMENT_ROOT'].'/includes/error.html.php';
exit();
}
foreach($result as $row)
{
$jokes[]=array('text'=>$row['joketext'],
'id'=>$row['id'],
'name'=>$row['name'],
'email'=>$row['email'];
)
}
include $_SERVER['DOCUMENT_ROOT'].'/includes/jokes.html.php';
@Englishguru777: when you post code on the forums, you need to format it so it will display correctly. (I’ve edited your post above to format it.)
You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.
& whenever I try to run this code I got the searchform and when I choose any category and then click Search I got this Error
Error Showing Jokes related to Search criterian SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘:categoryid’ at line 1
& whenever I try to run this code I got the searchform and when I choose any author and then click Search I got this Error
Error Showing Jokes related to Search criterian SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘:authorid’ at line 1
and in case of searching via some text i got this
Error Showing Jokes related to Search criterian SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘:joketext’ at line 1
What to do now I have checked my code many times with the code archive and it is all same
And the funny thing is I received this error as links on clicking these links get me back on my JMD
isn’t the correct way to prepare and execute a prepared statement in PDO. You need to call prepare() first, and then call execute(). When you call query() like this, it will try to run the query with your placeholders, which will be giving you the syntax error.
Are you certain the array isn’t defined in an include / require ?
I usually do $some_arr = []; instead of $some_arr = array(); but anyway, if the array is getting used later on and the fetch fails for some reason it’s an unpleasant error waiting to happen.
Try setting the following and both errors and warnings will be displayed:
<?php
declare( strict_types=1,);
ini_set('display_errors', 'true'); // ONLY FOR DEVELOPING OTHERWISE FALSE
error_reporting(-1); // MAXIMUM ERROR AND WARNINGS
// Your script goes here
An existing array can be modified by explicitly setting values in it.
This is done by assigning values to the array, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets ([]).
$arr[key] = value;
$arr[] = value;
// key may be an integer or string
// value may be any value of any type
If $arr doesn’t exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize a variable by a direct assignment.