在同一页面上添加和查看功能

时间:2015-12-04 13:06:52

标签: php

嗨我刚接触到php并且我试图制作一个基于web的项目,我应该能够在同一页面上有一个添加功能和查看功能,但代码我现在正在工作,我只能使视图功能我我不知道是谁可以把这个添加放在同一个页面上,这是我想要做的事情:

enter image description here

这是我的代码我正在使用

<body>
    <?php

            $username = "root";
            $password = "";
            $servername = "localhost";
            $dbname = "smit";

            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);

            if ($conn->connect_error) 
            {
                die("Connection failed: " . $conn->connect_error);
            } 
            if (isset($_POST['submit']))
            { 
                $fTitle = $_POST['title'];
                date_default_timezone_set('Asia/Manila');
                $date = date('Y-m-d h:i:s');
                $usr = 'shamvil';

                if ($fTitle == '')
                {
                    // generate error message
                    $error = 'ERROR: Please fill in all required fields!';

                    header("Location: forum.php"); 
                }
                else
                {
                    // save the data to the database\

                    mysqli_query($conn, "INSERT INTO topics(Topic, User, tDate) VALUES ('".$fTitle."','".$usr."','".$date."')")
                    or die(mysqli_error($conn)); 

                    // once saved, redirect back to the view page
                    header("Location: forum.php"); 
                }
            }

    ?>
    <div class="container" style="margin-top:1%">
        <div class="panel panel-warning">
          <!-- Default panel contents -->
          <div class="panel-heading"><h3>Forum Topic List</h3></div>
              <div class="panel-body">
                  <div class="container">
                  <p>Please Select a forum you want to view.</p>
                  </div>
                    <?php
                            $username = "root";
                            $password = "";
                            $servername = "localhost";
                            $dbname = "smit";

                            // Create connection
                            $conn = new mysqli($servername, $username, $password, $dbname);

                            // Check connection
                            if ($conn->connect_error) {
                                die("Connection failed: " . $conn->connect_error);
                            }   

                            $sql = "SELECT * FROM topics ORDER BY tDate";
                            $result = mysqli_query($conn, $sql);
                                echo "<table class = 'table table-striped'>";
                                    echo "<thead>";
                                        echo "<tr>";
                                            echo "<td>Topics</td>";
                                            echo "<td>Author</td>";
                                            echo "<td>Date Created</td>";
                                            echo "<td style='max-width:150px !important'></td>";
                                        echo "</tr>";
                                    echo "</thead>";
                                    echo "<tbody>";
                                        while($row = mysqli_fetch_array($result)){
                                            echo "<tr>";
                                                echo "<td>".$row{'Topic'}."</td>";
                                                echo "<td>".$row{'User'}."</td>";
                                                echo "<td>".$row{'tDate'}."</td>";
                                                echo "<td style='max-width:150px !important'>View Topic</td>";
                                            echo "</tr>";
                                            }
                                    echo "</tbody>";
                                echo "</table>";

                            $conn->close();
                        ?>

                    <button class="btn btn-primary" onclick="tadd();">Create a discussion</button>
                    <section id="newd" style="display:none">
                    </br>
                    <div class="panel panel-info">
                      <!-- Default panel contents -->
                      <div class="panel-heading">Adding a topic</div>
                      <div class="panel-body">
                        <form action="index.php" method="post">
                            <div class="form-group">
                                <label for="tpic">Forum Topic</label>
                                <input type="text" class="form-control" name="title" id="tpic" placeholder="Forum Topic" requuired>

                            </div>
                            <input type="submit" name="submit" value="Submit">
                        </form>
                      </div>


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

        </div>
    </div>


    <script src="js/jquery.js"></script>

        <!-- Bootstrap Core JavaScript -->
        <script src="js/bootstrap.min.js"></script>


        <!-- Plugin JavaScript -->
        <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
        <script src="js/classie.js"></script>
        <script src="js/cbpAnimatedHeader.js"></script>

        <!-- Contact Form JavaScript -->
        <script src="js/jqBootstrapValidation.js"></script>
        <script src="js/contact_me.js"></script>

        <!-- Custom Theme JavaScript -->
        <script src="js/freelancer.js"></script>
        <script src="js/toggle.js"></script>
    </body>

任何想法?

1 个答案:

答案 0 :(得分:1)

由于它将是Add功能,涉及更改或创建服务器数据的更改,因此最好使用POST方法。

从开始,您可以构建类似:

<?php
  if (count($_POST)) // Request method is post
    send_data_to_server($_POST);
  else { ?>
    <!-- My HTML Here -->
<?php } ?>

在您的脚本中需要注意很多事情。您的代码容易受到SQL Injection的攻击。<​​/ p>