数据库中未显示内容

时间:2018-04-18 03:58:07

标签: php mysql if-statement

所以这是代码,根据类别假设用户点击该类别的按钮,那么该类别下的所有事件都必须被公开,所以这就是我作为代码给出的没有错误或任何东西,但数据没有显示,最后一部分,但最后一个部分正在工作

<?php
global $row2;
if(isset($_POST['category']))
{
  if($_POST['category']== 'Healthcare')
  {
    while($row2= mysqli_fetch_array($result)) 
    {

    } 
  }
  else if($_POST['category']== 'Finance')
  {

  } 
} 
else 
{

}

?>

1 个答案:

答案 0 :(得分:0)

你的问题不是,而且仍然没有措辞得很好。但我花了一分钟时间将代码重新编写为更易于管理的代码。

以下是一些提示:

首先,以您的网页显示您可能遇到的任何错误的方式转动您的错误报告。

其次,请阅读为查询使用预准备语句。如果你不熟悉,你将容易受到sql注入和xss攻击。您现在也可以花时间了解这一点,以便以后不必学习。学习真的不是那么糟糕。这是一个很好的链接,可以帮助您入门:

https://websitebeaver.com/prepared-statements-in-php-mysqli-to-prevent-sql-injection

第三,如果您发现自己反复使用相同的代码,则应评估代码的逻辑。对于您的博客文章html,您只需要编写一次,然后选择您要通过它的查询。

如果您要在多个页面上使用相同的帖子格式,请使用html创建一个文件并将其包含在您的代码中,并在需要时使用。如果您想对代码进行更改,这将为您节省大量时间。您只需在一个位置更改它以在多个页面中更改它。

最后,当问题解决时,将代码缩减为最简单的形式,让它工作,然后添加到它,直到你做对了。

我没有测试下面的代码,我对mysqli并不熟悉,但我认为它应该可行。我主要是在这里发布这个,所以你可以看到一个更好的方法,你可能想要达到的目的。无论如何,我希望它有所帮助,祝你好运。

<?php
//There is no $_POST['category'] in your form.  The if statement will
//not not show the healthcare or finance queries unless they are set in the $_POST['category'].
if(isset($_POST['category']) && $_POST['category']){

    $query = "select * from event where category = ? "; //<--Uses a palceholder for the category.

    $stmt = $conn->prepare($query); //<--Prepares the query.
    $stmt->bind_param('s', $_POST['category']); //<--Binds the category placeholder. 
    $stmt->execute(); //<--Runs the query.
    $results = $stmt->get_results(); //<-- Gets the results and holds them in $results.

} else {

    //The shows everything query
    $query = "select * from event";
    $stmt = $conn->prepare($query);
    $stmt->execute(); 
    $results = $stmt->get_results();

 }

//The all your inputs have to be inside your form tag.
//Put the form tag first.   
echo

'<form action="eventdetail.php" method="post" enctype="multipart/form-data">

<div class="events events-full event-list">

    <div class="container">';

    if($results){  //<--Check to see if the query has any data.

        while($row = $results->fetch_assoc()){  //<--Uses the results and generates an array
      //containing the results.

        //Loop over your html. Echos out one row for each iteration of the loop.
            echo
            '<div class="row">

                        <div class="col-md-9 col-sm-8"> 

                        <!--Blog Post Start-->

                            <div class="blog-post">

                                <div class="post-thumb">

                                    <div class="link-wrap"> <a href="#"><i class="fa fa-search"></i></a> <a href="#"><i class="fa fa-link"></i></a> </div>

                                    <img src="images/gallery/' . $row['event_image'] . '" alt="user">

                                    </div>

                                    <div class="event-text">

                                        <div class="event-counter">
                                        </div>

                                        <h4> <a href="#">' . $row['title'] . '</a> </h4>
                                        <p>'  . $row['descrption'] . '</p>

                                        <p><span class="glyphicon glyphicon-map-marker">' . $row['location'] . '</span></p>
                                        <p><span class="glyphicon glyphicon-grain">' . $row['organizer'] . '</span></p>
                                        <a class="nd" href="">

                                        <input type="hidden" value="' . $row['id'] . '" name="' . $row['id'] . '"/> 

                                    </div>

                                </div>  
                          </div>
                    </div>

            </div>';

        }

    }else{

        echo 'Your query did not find any results.';

    }

echo 
    '</div>
</div>

<button type="submit" class="btn btn-primary" name="detail" value="detail">Event Detail</button>

</form>'; //<--Close the from tag.
相关问题