Php mysqli异常,插入和读取数组

时间:2015-11-18 23:17:15

标签: php mysql arrays mysqli

所以我正在测试我的代码,基本上我将一些信息插入到数据库中,然后按时间顺序在数组中读取,因此首先查看插入的最新内容。

问题:插入新数据但由于某种原因出现在网站上的4位置,无法找到异常。

以下是插入查询:

<?php
//values to be inserted in database table
//session_start();
include('connect.php');


$title = $_POST['title'];
$tip= $_POST['tip'];

$query = "INSERT INTO tips (title, paragraph) VALUES(?, ?)";
$statement = $mysqli->prepare($query);

//bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
$statement->bind_param('ss', $title, $tip);

if($statement->execute()){
     print 'Success! ID of last inserted record is : ' .$statement->insert_id .'<br />'; 
     echo'<a href="membership.php" class="blar">Membership page</a>';
}else{
     die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$statement->close();
?>

以下是读取数据的代码:

$category = array(
    "dates" => array(),
    "paragraphs" => array(),
    "titles" => array()
);

if ($result = $mysqli->query("SELECT * FROM tips")){
    if($result->num_rows){
        while($row = $result->fetch_array(MYSQLI_ASSOC)){
        //printf ("%s %s %s\n",$row["date"], $row["title"], $row["paragraph"]); 
        $date[] = $row['date'];
        $paragraphs[] = $row['paragraph'];
        $titles[] = $row['title'];
        }
        }
        $result->free();
    }
?>

然后按顺序阅读这些内容:

<div class="cd-timeline-block">
            <div class="cd-timeline-img cd-picture">
                <img src="img/cd-icon-location.svg" alt="Location">
            </div> <!-- cd-timeline-img -->

            <div class="cd-timeline-content">
                <h2><?php echo $titles[count($titles)-1]?> </h2>
                <p><?php echo $paragraphs[count($paragraphs)-1]?></p>

                <span class="cd-date"><?php echo $date[count($date)-1]?></span>
            </div> <!-- cd-timeline-content -->
        </div> <!-- cd-timeline-block -->

        <div class="cd-timeline-block">
            <div class="cd-timeline-img cd-movie">
                <img src="img/cd-icon-location.svg" alt="Location">
            </div> <!-- cd-timeline-img -->

            <div class="cd-timeline-content">
                <h2><?php echo $titles[count($titles)-2]?></h2>
                <p><?php echo $paragraphs[count($titles)-2]?></p>

                <span class="cd-date"><?php echo $date[count($titles)-2]?></span>
            </div> <!-- cd-timeline-content -->
        </div> <!-- cd-timeline-block -->

这是前两个数组,无法理解异常在哪里,有什么指针?

1 个答案:

答案 0 :(得分:0)

更改您的查询以添加订单

SELECT * FROM tips order by date