Let me start by saying, I'm very new to coding and I am trying to learn on my own (books and Internet no schooling).
With that being said, on to my problem......
I'm trying to add a blog to my web site from a book, PHP for Absolute Beginners by Jason Lengstorf, and so far all I get is errors.
I've added the code that I have so far along with the errors below.
According to the book, I am more than far enough along for it to work in a most basic form, but it doesn't.
I have checked all the code several times to make sure that I had no typing errors (fixed the ones I had) and it still doesn't work.
I have the SQL table setup like the book said, along with the future additions from later in the book...
***index.php***
CODE
<?php
/*
* Include the necessary files
*/
include_once 'inc/functions.inc.php';
include_once 'inc/db.inc.php';
//Open a database connection
$db = new PDO(DB_INFO, DB_USER, DB_PASS);
/*
*Figure out what page is being requested (default is blog)
*Perform basic sanitization on the variable as well
*/
if(isset($_GET['page']))
{
$page = htmlentities(strip_tags($_GET['page']));
}
else
{
$page = 'blog';
}
//Determine if an entry ID was passed in the URL
$id = (isset($_GET['id'])) ? (int) $_GET['id'] : NULL;
//Load entries
$e = retrieveEntries($db, $page, $id);
//Get the fulldisp flag and remove it from the array
$fulldisp = array_pop($e);
//Sanitize the entry data
$e = sanitizeData($e);
?>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="/css/default.css" type="text/css" />
<title>Simple Blog</title>
</head>
<body>
<h1>Simple Blog Application</h1>
<div id="entries">
<?php
//If the full display flag is set, show the entry
if($fulldisp==1)
{
?>
<h2><?php echo $e['title'] ?></h2>
<p><?php echo $e['entry'] ?></p>
<p class="backlink">
<a href="./">Back to Latest Entries</a>
</p>
<?php
} //End the if statement
//If the full display flag is 0, format linked entry titles
else
{
//Loop through each entry
foreach($e as $entry){
?>
<p>
<a herf="?id=<?php echo $entry['id'] ?>">
<?php echo $entry['title'] ?>
</a>
</p>
<?php
} // End the foreach loop
}//End the else
?>
<p class="backlink">
<a href="/admin.php">Post a New Entry</a>
</p>
</div>
</body>
</html>
***admin.php***
CODE
<!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>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="css/default.css" type="text/css" />
<title>Simple Blog</title>
</head>
<body>
<h1>Simple Blog Application</h1>
<form method="post" action="inc/update.inc.php">
<fieldset>
<legend>New Entry Submission</legend>
<label>Title
<input type="text" name="title" maxlength="150" />
</label>
<label>Entry
<textarea name="entry" cols="45" rows="10"></textarea>
</label>
<input type="submit" name="submit" value="Save Entry" />
<input type="submit" name="submit" value="Cancel" />
</fieldset>
</form>
</body>
</html>
***functions.inc.php***
CODE
<?php
function retrieveEntries($db, $page, $url=NULL)
{
/*
*If an entry ID was supplied, load the associated entry
*/
if(isset($id))
{
$sql = "SELECT title, entry
From entries
WHERE id=?
LIMIT 1";
$stmt = $db->prepare($sql);
$stmt->execute(array($_GET['id']));
//Save the returned entry array
$e = $stmt->fetch();
//Set the fulldisp flag for a single entry
$fulldisp = 1;
}
/*
*If no entry ID was supplied, load all entry titles for the page
*/
else
{
$sql = "SELECT id, page, title, entry
FROM entries
WHERE page=?
ORDER BY created DESC";
$stmt = $db->prepare($sql);
$stmt->execute(array($page));
$e = NULL; //Declare the variable to avoid errors
//Loop through returned results and store as an array
while($row = $stmt->fetch()){
$e[] = $row;
}
//Set the fulldisp flag for multiple entries
$fulldisp = 0;
/*
*If no entries were returned, display a default
*message and set the fulldisp flag to display a
*single entry
*/
if(!is_array($e))
{
$fulldisp = 1;
$e = array(
'title' => 'No Entries Yet',
'entry' => '<a href="/admin.php">Post an entry!</a>'
);
}
}
//Add the $fulldisp flag to the end of the array
array_push($e, $fulldisp);
return $e;
}
function sanitizeData($data)
{
//If $data is not an array, run strip_tags()
if(!is_array($data))
{
//Remove all tags except <a> tags
return strip_tags($data, "<a>");
}
//If data is an array, process each element
else
{
//Call sanitizeData recursively for each array element
return array_map('sanitizeData' , $data);
}
}
?>
***update.inc.php***
CODE
<?php
if($_SERVER['REQUEST_METHOD']=='POST'
&& $_POST['submit']=='Save Entry'
&& !empty($_POST['title'])
&& !empty($_POST['entry']))
{
//Include database credentials and connect to the database
include_once 'db.inc.php';
$db = new PDO(DB_INFO, DB_USER, DB_PASS);
//Save the entry into the database
$sql = "INSERT INTO entries (title, entry) VALUES (?, ?)";
$stmt = $db->prepare($sql);
$stmt->execute(array($title, $entry));
$stmt->closeCursor();
//Get the ID of the entry we just saved
$id_obj = $db->query("SELECT LAST_INSERT_ID()");
$id = $id_obj->fetch();
$id_obj->closeCursor();
//Send the user to the new entry
header('Location: ../?id='.$id[0]);
exit;
}
//If both conditions aren't met, sends the user back to the main page
else
{
header('Location: ../');
exit;
}
?>
***db.inc.php***
CODE
<?php
define('DB_INFO', 'mysql:host=localhost;dbname=simpleblog');
define('DB_USER', 'root');
define('DB_PASS', ' ');
?>
*****didn't include CSS file*****
The error I get when I 'save entry' on the ADMIN page....
QUOTE
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /www/zxq.net/m/y/f/myfirstsite/htdocs/web page stuff/simpleblog/inc/update.inc.php:11 Stack trace: #0 /www/zxq.net/m/y/f/myfirstsite/htdocs/web page stuff/simpleblog/inc/update.inc.php(11): PDO->__construct('mysql:host=loca...', 'root', ' ') #1 {main} thrown in /www/zxq.net/m/y/f/myfirstsite/htdocs/web page stuff/simpleblog/inc/update.inc.php on line 11
.....and save entry or cancel with the fields left blank.....
QUOTE
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /www/zxq.net/m/y/f/myfirstsite/htdocs/web page stuff/simpleblog/index.php:10 Stack trace: #0 /www/zxq.net/m/y/f/myfirstsite/htdocs/web page stuff/simpleblog/index.php(10): PDO->__construct('mysql:host=loca...', 'root', ' ') #1 {main} thrown in /www/zxq.net/m/y/f/myfirstsite/htdocs/web page stuff/simpleblog/index.php on line 10
.....as well as the error that I get just by going to the INDEX page.......
QUOTE
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /www/zxq.net/m/y/f/myfirstsite/htdocs/web page stuff/simpleblog/index.php:10 Stack trace: #0 /www/zxq.net/m/y/f/myfirstsite/htdocs/web page stuff/simpleblog/index.php(10): PDO->__construct('mysql:host=loca...', 'root', ' ') #1 {main} thrown in /www/zxq.net/m/y/f/myfirstsite/htdocs/web page stuff/simpleblog/index.php on line 10
What am I doing wrong?