在PHP的帮助下从数据库中检索数据时出错

时间:2015-03-23 05:39:59

标签: php mysql

我正在观看教程并继续学习。首先检查我的代码

<body>
<header>
    ArticlePoster
</header>

<nav>       
</nav>

<aside>
    <h2>SideBar </h2>
</aside>

<section>
    <article>
        <?php
        include("connect.php");

        $query = "SELECT * FROM posts";
        $run = mysqli_query($con, $query);
        while($row = mysqli_fetch_array($run)) {
            if (isset($_POST['submit'])) {
                    $title = $row['post_Title'];
                    $date = $row['post_Date'];
                    $author = $row['post_Author'];
                    $image = $row['post_Image'];
                    $content = $row['post_Content'];
                                }
                            }
                            ?>      
                            <h2> <?php  echo $title; ?> </h2>
    </article>          
</section>

</body>

我正在关注的讲师正在使用MYSQL并且已弃用。在教程中一切正常,没有isset()函数。但是如果没有它,我的代码就无法运行。现在问题出在echo $ title变量上。

我得到的错误是&#34;未定义的变量&#34; 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

希望您的查询给出结果。然后请尝试此代码。

    include("connect.php");
    $query = "SELECT * FROM posts";
    $run = mysqli_query($con, $query);
    while($row = mysqli_fetch_array($run)) {
         $title = $row['post_Title'];
         $date = $row['post_Date'];
         $author = $row['post_Author'];
         $image = $row['post_Image'];
         $content = $row['post_Content'];
         echo "<h2>". $title ."</h2>";
    }

答案 1 :(得分:0)

这是我的代码,另一个如上所示。你可以检查一下@AzeezKallayi if(isset($ _ POST [&#39;提交&#39;])){

    $title = $_POST['Title'];
    $date = date('y/m/d');
    $author = $_POST['Author'];
    $content = $_POST['Content'];
    $image_name = $_FILES['Image']['name'];
    $image_type = $_FILES['Image']['type'];
    $image_size = $_FILES['Image']['size'];
    $image_tmp = $_FILES['Image']['tmp_name'];
if( $title == '' or $author == '' or $content == '' )
{
    echo "<script>alert ('Please fill all fields')</script>";
        exit();
}

    if($image_type == "image/jpeg" or $image_type == "image/png" or $image_type == "image/gif") {

        if($image_size <=50000) {
            move_uploaded_file($image_tmp, "images/$image_name");

        }
        else {  

            echo "<script>alert('Image should not be larger than 50kb')</script>";  
            }


    }

    else {  

            echo "<script>alert('image type is not valid')</script>";   

            }
        $query = "INSERT INTO posts (post_Title,post_Date,post_Author,post_Image,post_content) values ('$title','$date','$author','$image_name','$content')";

    if(mysqli_query($con, $query)) {

        echo "<h1>Post Has been Published</h1>";

    }
}