hi guys,
i’m sorry in advance if this will seem like a duplicate but i’m stuck, it was all fine before but now it’s broken. i’ve gone through the forums and looked at the solutions provided, tried them and that hasn’t worked for me. so now i’m here stuck not knowing what to do. i’m stuck in chapter6 i just added my include helper functions file at the top of my jokes.html.inc.php. this is the code as it stands:
<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/includes/helpers.inc.php';
?>
<!DOCTYPE html>
<html>
<head>
<title>Jokes Results</title>
</head>
<body>
<section>
<p><a href="?addjoke">Add your own joke</a></p>
<p>Here are all the jokes in the database?</p>
<?php
foreach($jokes as $joke):?>
<form action="?deletejoke" method="post">
<blockquote>
<p>
<?php
htmlOut( $joke['text'] );
?>
<input type="hidden" name="id" value=" <?php echo $joke[ 'id' ]; ?> ">
<input type="submit" value="Delete">
( by
<a
href="mailto:<?php htmlOut( $joke[ 'email' ] );?>"
><?php htmlOut( $joke[ 'name' ] );?></a>
)
</p>
</blockquote>
</form>
<?php endforeach; ?>
</section>
</body>
</html>
this is the code in my controller function page.
<?php
include $_SERVER['DOCUMENT_ROOT'].'/includes/magicquotes.inc.php';
if( isset( $_GET['addjoke'] ) )
{
include 'form.html.php';
exit();
}
/*try{
$pdo = new PDO('mysql:host=localhost;dbname=ijdb', 'ijdbUser', '6Vn2zZ8pK');
$pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo -> exec('SET NAMES "utf8"');
}catch( PDOexception $e ){
$error = 'Unable to connect to the database server'. $e-> getMessage();
include 'error.html.php';
exit;
}*/
/**
* We check to see if joketext isset
* if it is we try to insert the new joke
* into the joke table if there is an error
* we catch it.
* if there is none we refresh the page.
**/
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 adding submitted joke'. $e-> getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
/**
* We check to see if deletejoke isset
* if it is we then delete the item from the
* table and refresh the page.
**/
if( isset($_GET[ 'deletejoke' ]) )
{
include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
try
{
$sql = 'DELETE FROM joke
WHERE id= :id';
$deleteJoke = $pdo -> prepare( $sql );
$deleteJoke -> bindValue( ':id', $_POST[ 'id' ] );
$deleteJoke -> execute();
}catch(PDOException $e)
{
$error = 'Error Occured, Failed to delete Joke'. $e -> getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';
/**
* We try to select all the jokes in
* the joke table if there is an error
* we catch it.
**/
try
{
/* $sql = 'SELECT jokeText,id FROM joke'; */
$sql = 'SELECT joke.id, jokeText, name, email FROM joke
INNER JOIN author ON joke.authorId= author.id';
$result = $pdo -> query($sql);
}
catch( PDOException $e )
{
$error = 'Error Fetchin Jokes' . $e-> getMessage();
include 'error.html.php';
exit();
}
/*while($row = $result -> fetch()){
$jokes [] = array(
'id' => $row[ 'id' ],
'text' => $row[ 'jokeText' ],
'name' => $row[ 'name' ],
'email' => $row[ 'email' ]
);
}*/
$jokes = array();
foreach( $result as $row )
{
$jokes[] = array(
'id' => $row[ 'id' ],
'text' => $row[ 'jokeText' ],
'name' => $row[ 'name' ],
'email' => $row[ 'email' ]
);
}
include 'jokes.html.php';
?>
I’ve also tried running the sql query in my sql and i do get results back so i know it’s not because i don’t have any results in the database.
please can anybody help me out!