在模型中提交表单而不将数据保存到数据库

时间:2016-08-01 16:06:30

标签: php mysql twitter-bootstrap-3

我正在尝试将数据简单保存到数据库中。我有一个模态的表格。

当我尝试将数据保存到我的数据库时,它将无法保存。如果我将表单放在模态之外,它会将数据保存到数据库中。

我已经google了解决方案,但没有找到。提交按钮是我的形式,因此不会导致问题。

PHP代码:

<?php
ini_set('display_errors', 'On');   
error_reporting(E_ALL | E_STRICT); 

session_start();
include 'config.php';

if(!isset($_SESSION['user_id'])) {
    header("Location: login.php");
}

//read out data from db
$query_string = "SELECT * FROM tbl_Category";
$query = mysqli_query($con, $query_string);

//save data to db
if(isset($_POST['saveCategory'])){
  $category = mysqli_real_escape_string($con, $_POST['category']);
  $query = "INSERT INTO tbl_Category(category) VALUES('$category')"; 
  $result = mysqli_query($con, $query);

  if(!$result){
      echo mysqli_error();
  }
  else{
    $category = '';
    echo "YES";
  }
}

?>

我的config.php文件:

<?php
$con = mysqli_connect("localhost", "root", "root", "challenger") or die("Error " . mysqli_error($con));

// if($con){
//          echo"There is a connection";
//   }

?>

我的部分HTML代码:

 <!-- Page Content -->
 <div id="page-wrapper">
    <div class="container-fluid">
      <div class="row bg-title">
        <div class="col-lg-3 col-md-4 col-sm-4 col-xs-12">
          <h4 class="page-title">Category</h4>
        </div>
        <div class="col-lg-9 col-sm-8 col-md-8 col-xs-12">
          <ol class="breadcrumb">
            <li><a href="#">Category</a></li>
            <li><a href="#" class="active">Category</a></li>
          </ol>
        </div>
        <!-- /.col-lg-12 -->
      </div>
      <!-- /row -->
      <div class="row">
        <div class="col-sm-12">
          <div class="white-box">
            <h3 class="box-title m-b-0">Overview Category</h3>
            <button class="btn btn-danger btn-outline pull-right" data-toggle="modal" data-target="#addCategory">Add Category</button>
            <br><br><br>
            <table id="myTable" class="table table-striped">
              <thead>
                <tr>
                  <th>Number</th>
                  <th>Category</th>
                </tr>
              </thead>
              <tbody>
                <?php
                  while($row = mysqli_fetch_assoc($query)){
                    echo "<tr>";
                      echo "<td>".$row['category_id']."</td>";
                      echo "<td>".$row['category']."</td>";
                    echo "</tr>";  
                  }    
                ?>
              </tbody>
            </table>
          </div>
        </div>

            <!-- MODAL WINDOW -->
            <div class="modal fade" id="addCategory" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel1">
              <div class="modal-dialog" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel1">Add a new category</h4>
                  </div>
                  <div class="modal-body">
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                      <div class="form-group">
                        <label for="recipient-name" class="control-label">Category Name:</label>
                        <input type="text" class="form-control" id="recipient-name1" name="category">
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary" name="saveCategory" data-dismiss="modal">Create Category</button>
                      </div>
                    </form>
                  </div><!-- end modal body -->
                </div>
              </div>
            </div>
            <!-- END MODAL WINDOW -->

      </div>
      <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
  </div>
  <!-- /end Page Content -->

1 个答案:

答案 0 :(得分:0)

更改此

  $query = "INSERT INTO tbl_Category(category) VALUES('$category')"; 

到这个

  $query = "INSERT INTO tbl_Category(`category`) VALUES('$category')";