单击引导按钮时,我想调用php插入查询

时间:2019-05-16 04:08:37

标签: php bootstrap-modal

我的问题是我想在单击引导程序上的按钮的同时调用PHP文件,但是由于我粘贴了引导程序的代码,PHP在这里不起作用,因此请PHP指导我解决方案 预先感谢

我已经厌倦了将按钮更改为表单,但所有这些都无法正常工作,但是我正在使用PHP 7和Bootstrap 4

<?php  include "includes/db.php"; ?>

<?php
// echo "<h1> Testing </h1>";
// $db = mysql_select_db("cateory", $connection); // Selecting Database from Server
function(){
        if (isset($POST['submit'])){
          // $connect=mysqli_query($connection,)
          $cat_title=$POST['cat_title'];
          if($cat_title==""||empty($cat_title)){
            echo "The field should not empty";

          }
          else{
            $query="INSERT INTO category (cat_title)";
            $query .=" VALUE ('{$cat_title}')";
            $create_category_query = mysqli_query ($connection,$query);
            if(!$create_category_query){
              die ('QUERY FAILED'.mysqli_error($connection));
            }
          }
         }
        }
        ?>```
  <!-- ADD CATEGORY MODAL -->
  <div class="modal fade" id="addCategoryModal">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">
        <div class="modal-header bg-success text-white">
          <h5 class="modal-title">Add Category</h5>
          <button class="close" data-dismiss="modal">
            <span>&times;</span>
          </button>
        </div>

        <div class="modal-body">

          <form action="" method="post">
            <div class="form-group">
              <label for="title">Title</label>
              <input type="text" class="form-control" name="title">
            </div>

        </div>
        <div class="modal-footer">
    <?php insert_categories();  ?>
    <button class="btn btn-success" type="submit" data-dismiss="modal">Save Changes</button>```

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

1 个答案:

答案 0 :(得分:0)

请像下面那样更改代码

<?php
if ( isset( $_POST['submit']) ) {
    // $connect=mysqli_query($connection,)
    $cat_title = $_POST['cat_title'];

    if($cat_title==""||empty($cat_title)){
        echo "The field should not empty";
    } else {
        $query="INSERT INTO category (cat_title)";
        $query .= " VALUE ('{$cat_title}')";
        $create_category_query = mysqli_query ($connection,$query);

        if(!$create_category_query){
            die ('QUERY FAILED'.mysqli_error($connection));
        }
    }
}
?>
<!-- ADD CATEGORY MODAL -->
<div class="modal fade" id="addCategoryModal">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">
            <div class="modal-header bg-success text-white">
            <h5 class="modal-title">Add Category</h5>
            <button class="close" data-dismiss="modal">
                <span>&times;</span>
            </button>
            </div>
            <div class="modal-body">
                <form action="" method="post">
                    <div class="form-group">
                        <label for="title">Title</label>
                        <input type="text" class="form-control" name="cat_title">
                    </div>
                    <div class="modal-footer">
                        <?php insert_categories();  ?>
                        <button class="btn btn-success" type="submit" data-dismiss="modal">Save Changes</button>```
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

您的代码存在问题

  • 您的HTML格式本身是错误的。您已经用div modal-body破坏了表单标签。您必须重新考虑HTML设计。

  • 无需将PHP代码包装在没有名称的函数中。 您只需要使用代码即可。

  • 在将数据保存/检查为title时,您已将输入命名为cat_title

  • 再次检查您的整个代码库。