dpjhq00684 2013-01-06 12:41
浏览 42

我的博客(索引)抛出一个未定义的变量,我的创建表单为空[关闭]

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>
  • 写回答

2条回答 默认 最新

  • dongzuan4491 2013-01-06 12:46
    关注

    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'])).

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀