Notice: Undefined variable: left_move2 in I:\_Misc\Programming\EasyPHP-12.1\www\blogg\index.php on line 106
This error should be related to the number of posts, if there are only one then there should be no controls, not sure what I did wrong here. Meaning I have no controls now and it all went wrong when I sorted my posts in ASC order.
<?PHP
//Connect to the database
include_once"connect.php";
//Get the id of the url and make it a php variable
if(isset($_GET['id']) && $_GET['id']) { //this will not give this notice
$id = $_GET['id'];
} else {
//Get the entries in the DB
$sql = mysql_query("SELECT * FROM blogposts ORDER BY id DESC");
while($row = mysql_fetch_array($sql)) {
$id = $row["id"];
}
}
//Prevent SQL injection
$id = mysql_real_escape_string($id);
//Connect to the table a fetch the entries we want
$sql = mysql_query("SELECT * FROM blogposts WHERE id='$id'");
while ($row = mysql_fetch_array($sql)) {
$title = $row["title"];
$message = $row["message"];
$time = $row["time"];
//Let's make the displayed time a bit more "professional" (Month Day, Year)
$time = strftime("%b %d, %y", strtotime($time));
}
/* Connect to the entries again in the DB so we can use controls at
the bottom and also use this to make the controls */
$sql = mysql_query("SELECT * FROM blogposts ORDER BY id DESC");
while ($row = mysql_fetch_array($sql)) {
$id2 = $row["id"];
}
//Make the code for the next and previous post
$up_1 = $id+1;
$down_1 = $id-1;
//If there is only one entry, then we don't want any controls
if ($id2 == 1) {
$left_move1 = '';
$left_move1 = '';
$right_move1 = '';
$right_move2 = '';
//Check to see if the function above is not true and decide what controls we want
} else if ($id == 1) {
$left_move1 = '<a href="?id=' . $id2 . '">Latest Article</a>';
$left_move2 = '<a href="?id=' . $up_1 . '">Next Article</a>';
$right_move1 = '';
$right_move2 = '';
//Again the same check
} else if ($id == $id2) {
$right_move1 = '<a href="?id=' . $down_1 . '">Previous Article</a>';
$right_move2 = '<a href="?id=1">Last Article</a>';
$left_move1 = '';
$left_move2 = '';
//If none of the querys above are true then display all controls
} else {
$left_move1 = '<a href="?id=' . $id2 . '">Latest Article</a>';
$left_move2 = '<a href="?id=' . $up_1 . '">Next Article</a>';
$right_move1 = '<a href="?id=' . $down_1 . '">Previous Article</a>';
$right_move2 = '<a href="?id=1">First Article</a>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head>
<meta http-equiv='content-type' content='application/xhtml+xml; charset=UTF-8' />
<title>
<?PHP
echo"$title";
?>
</title>
<!-- JAVASCRIPTS -->
<!-- CSS FILES -->
<link rel='stylesheet' type='text/css' href='css/default.css' />
</head>
<body>
<div id="wrapper">
<div id="header">
<?PHP
include_once"header.php";
?>
</div>
<div id="time">
<?PHP
echo"$time" . ' - ' . $title;
?>
</div>
<div id="content">
<?PHP
echo"$message";
?>
</div>
<div id="cleft">
<?PHP
echo"$left_move1";
?>
</div>
<div id="cleft2">
<?PHP
echo"$left_move2";
?>
</div>
<div id="cright">
<?PHP
echo"$right_move1";
?>
</div>
<div id="cright2">
<?PHP
echo"$right_move2";
?>
</div>
</div>
</body>
</html>
My create.php form is just a blank page, not sure what I did wrong here.
<?PHP
//Connect to the database
include_once"connect.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xmlns="http://www.w3.org/1999/html" xml:lang='en'>
<head>
<meta http-equiv='content-type' content='application/xhtml+xml; charset=UTF-8' />
<title>Create Article</title>
<!-- JAVASCRIPTS -->
<!-- CSS FILES -->
<link rel='stylesheet' type='text/css' href='css/default.css' />
</head>
<body>
<?PHP
if(isset($_SESSION['msg']) && $_SESSION['msg']){
echo $_SESSION['msg'];
}
if(isset($_POST['parse_var']) == "new") {
$title = mysql_real_escape_string($_POST['title']);
$message = mysql_real_escape_string($_POST['message']);
$time = mysql_real_escape_string($_POST['time']);
$sqlcreate = mysql_query("INSERT INTO blogposts (title, message, time) VALUES(now(),'$title', '$message')");
if ($sqlcreate) {
$_SESSION['msg'] = 'A new entry has been posted.';
} else {
$_SESSION['msg'] = 'Problems connecting to the server please try again later.';
}
?>
<form action="create.php" method="post" name="new">
Title: <input type="text" id="title" value="Title" />
<br />
<br />
Content: <textarea rows="8" cols="60"></textarea>
<br />
<input type="hidden" value="new" /></pre>
<input type="button" value="Submit" />
</form>
<?PHP
}
?>
</body>
</html>
The undefined index id error, means that you are accessing an array, but the index 'id' doesn't exist. In your code, that could be either the $_GET['id']
or the $row['id']
call. But it probably is with the $_GET['id']
call (because that is a user parameter). You can use isset()
to check if a variable or array index exists (with isset($_GET['id'])
).
Re-look at your index.php
's logic:
You sought to load posts by ID only. When no ID is supplied, you loop through all the available ID and load the last ID only. This logic is weird.
Re-coded:
//Get the id of the url and make it a php variable
if (isset($_GET['id']) && $_GET['id']) {
//Prevent SQL injection
$id = mysql_real_escape_string($_GET['id']);
//Connect to the table a fetch the entries we want
$sql = mysql_query("SELECT * FROM blogposts WHERE id='$id'");
} else {
// load all the blog posts
$sql = mysql_query("SELECT * FROM blogposts ORDER BY `time` DESC");
}
$posts = array();
while ($row = mysql_fetch_array($sql)) {
$post = array();
$post['title'] = $row["title"];
$post['message'] = $row["message"];
//Let's make the displayed time a bit more "professional" (Month Day, Year)
$post['time'] = strftime("%b %d, %y", strtotime($row['time']));
$posts[] = $post;
}
// ... other code to display posts from $posts
In your create.php
code, you mean to use Session for your $msg
variable instead to keep data between requests in PHP:
<?php
session_start();
if(isset($_SESSION['msg']) && $_SESSION['msg']){
echo $_SESSION['msg'];
}
if(isset($_POST['parse_var']) == "new") {
$title = mysql_real_escape_string($_POST['title']);
$message = mysql_real_escape_string($_POST['message']);
$time = mysql_real_escape_string($_POST['time']);
$sqlcreate = mysql_query("INSERT INTO blogposts (title, message, time) VALUES(now(),'$title', '$message')");
if ($sqlcreate) {
$_SESSION['msg'] = 'A new entry has been posted.';
} else {
$_SESSION['msg'] = 'Problems connecting to the server please try again later.';
}