2017-03-01 11 views
-1

我有这样的HTML表单数据库插入数据不工作

<div class="box box-info"> 
     <div class="box-header with-border"> 
      <h3 class="box-title">Please Input Below Information</h3> 
     </div> 
     <!-- /.box-header --> 
     <!-- form start --> 
     <form method="POST" action="model.php" class="form-horizontal"> 
      <div class="box-body"> 
      <div class="form-group"> 
       <label for="inputEmail3" class="col-sm-2 control-label">Model Code *</label> 

       <div class="col-sm-10"> 
       <input type="text" name="ModelCode" class="form-control" id="inputEmail3" placeholder="e.g KTI"> 
       </div> 
      </div> 
      <div class="form-group"> 
       <label for="inputPassword3" class="col-sm-2 control-label">Model Name *</label> 

       <div class="col-sm-10"> 
       <input type="text" class="form-control" name="ModelName" id="inputPassword3" placeholder="e.g 0002"> 
       </div> 
      </div> 
      <div class="form-group"> 
      <label class="col-sm-2 control-label">Model Units *</label> 
      <div class="col-sm-10"> 
      <select class="form-control" name="ModelUnit" style="width: 100%;"> 
       <option value="" selected disabled>Choose Model Unit</option> 
       <?php 
       $sql = mysqli_query($con, "select departmentname from department"); 
       while ($row = mysqli_fetch_array($sql)) { 
       ?> 
       <option value="<?php echo $row['departmentname']; ?>"><?php echo $row['departmentname']; ?></option> 
       <?php 
       } 
       ?> 
      </select> 
      </div> 
      </div> 
      <div class="form-group"> 
      <label class="col-sm-2 control-label">Model Hour Theroy *</label> 
      <div class="col-sm-10"> 
      <select class="form-control" name="ModelHourT" style="width: 100%;"> 
       <option value="" selected disabled>Choose Model Hour</option> 
       <?php 
       $sql = mysqli_query($con, "select GroupTeory from grouptheory"); 
       while ($row = mysqli_fetch_array($sql)) { 
       ?> 
       <option value="<?php echo $row['GroupTeory']; ?>"><?php echo $row['GroupTeory']; ?></option> 
       <?php 
       } 
       ?> 
      </select> 
      </div> 
      </div> 
      <div class="form-group"> 
      <label class="col-sm-2 control-label">Model Hour Practice *</label> 
      <div class="col-sm-10"> 
      <select class="form-control" name="ModelHourP" style="width: 100%;"> 
       <option value="" selected disabled>Choose Model Practice</option> 
       <?php 
       $sql = mysqli_query($con, "select GroupPractical from grouppractical"); 
       while ($row = mysqli_fetch_array($sql)) { 
       ?> 
       <option value="<?php echo $row['GroupPractical']; ?>"><?php echo $row['GroupPractical']; ?></option> 
       <?php 
       } 
       ?> 
      </select> 
      </div> 
      </div> 
      <div class="form-group"> 
       <label for="inputEmail3" class="col-sm-2 control-label">Model Type *</label> 

       <div class="col-sm-10"> 
       <input type="text" name="ModelType" class="form-control" id="inputEmail3" placeholder="e.g KTI"> 
       </div> 
      </div> 
      <div class="form-group"> 
      <label class="col-sm-2 control-label">Semister *</label> 
      <div class="col-sm-10"> 
      <select class="form-control" style="width: 100%;" name="SemsterID"> 
       <option value="" selected disabled>Choose Model Type</option> 
       <?php 
       $sql = mysqli_query($con, "select Semester from semester"); 
       while ($row = mysqli_fetch_array($sql)) { 
       ?> 
       <option value="<?php echo $row['Semester']; ?>"><?php echo $row['Semester']; ?></option> 
       <?php 
       } 
       ?> 
      </select> 
      </div> 
      </div> 

      </div> 
      <!-- /.box-body --> 
      <div class="box-footer"> 
      <input type="submit" name="submit" class="btn btn-info pull-right" value="Submit" /> 
      </div> 
      <!-- /.box-footer --> 
     </form> 

     </div> 

,这是我的PHP插入功能

include("Connection.php"); 
if(isset($_POST['submit'])) 
{ 
echo "<script>alert($query1)</script>"; 
$ModelCode = mysqli_real_escape_string($con,$_POST['ModelCode']); 
$ModelName = mysqli_real_escape_string($con,$_POST['ModelName']); 
$ModelUnit = mysqli_real_escape_string($con,$_POST['ModelUnit']); 
$ModelHourT =mysqli_real_escape_string($con,$_POST['ModelHourT']); 
$ModelHourP = mysqli_real_escape_string($con,$_POST['ModelHourP']); 
$ModelType = mysqli_real_escape_string($con,$_POST['ModelType']); 
$SemsterID = mysqli_real_escape_string($con,$_POST['SemsterID']); 

$query1 = mysqli_query($con, "INSERT INTO 'model'     (ModelCode,ModelName,ModelUnit,ModelHourTheory,ModelHourPractical,ModelType,StageID) VALUES ('$ModelCode','$ModelName','$ModelUnit','$ModelHourT','$ModelHourP','$ModelType' ,'$SemsterID')"); 
echo "<script>alert($query1)</script>"; 
} 

,并没有工作....我没有不发生了什么 !!即使我一直在比较表的数据库的名称字段......但它仍然无法工作......任何人都知道什么是问题?

+0

请告诉我错误安慰? – Smit

+0

它没有给我任何错误...页面已被重新加载,但数据没有存储在数据库中...如果你看到我的最后一行我打印查询,但查询返回空。 – Jack

回答

0

你不应该使用表名的单引号(在这种情况下,单引号是文字值)

假设你的表被命名为模型,你应该使用

$query1 = mysqli_query($con, 
    "INSERT INTO model (ModelCode, ModelName,ModelUnit, 
         ModelHourTheory,ModelHourPractical,ModelType,StageID) 
      VALUES ('$ModelCode','$ModelName','$ModelUnit', 
         '$ModelHourT','$ModelHourP','$ModelType','$SemsterID')"); 
+0

我已经测试过一次...但它仍然不起作用 – Jack

+0

你确定$ _POST是正确的吗?尝试var_dump($ _ POST),并检查是否有提交条目 – scaisEdge

+0

打印这件事 http://www.webchinupload.com/f/2017-03/18fe55854b29596a5160ddb5827a6ab0.JPG – Jack