MySqli INSERT无法正常工作,没有错误

时间:2015-11-21 05:45:56

标签: php mysql database mysqli

我的代码似乎是正确的。显示一个表单,然后单击提交按钮时,应该执行此代码。如果文章存在,它会结束并说明但是如果文章不存在则不会插入数据库或发出错误或显示任何打印输出。

编辑:提供整个表格

<?php

if(!isset($_POST['submit']))
    {
    print '
<div class="container-fluid ui-widget">


<form class="form-horizontal" method="post" action="inputform.php" onsubmit="return confirm("Is all of your data correct?");">
<fieldset>

<!-- Article Input -->
<legend>Article Input</legend>

<!-- Journal Radio Buttons (value is journal ISSN)-->
<div class="form-group">
  <label class="col-md-4 control-label" for="journal_radio">Journal</label>
  <div class="col-md-4">
  <div class="radio">
    <label for="journal_radio-0">
      <input type="radio" name="journal_radio" id="journal_radio-0" value="2162-9730" checked="checked">
      MIS Quarterly
    </label>
  </div>
  <div class="radio">
    <label for="journal_radio-1">
      <input type="radio" name="journal_radio" id="journal_radio-1" value="1047-7047">
      Information Systems Research
    </label>
  </div>
  <div class="radio">
    <label for="journal_radio-2">
      <input type="radio" name="journal_radio" id="journal_radio-2" value="0742-1222">
      Journal of Management Information Systems
    </label>
  </div>
  <div class="radio">
    <label for="journal_radio-3">
      <input type="radio" name="journal_radio" id="journal_radio-3" value="1536-9323">
      Journal of the Association for Information Systems
    </label>
  </div>
  <div class="radio">
    <label for="journal_radio-4">
      <input type="radio" name="journal_radio" id="journal_radio-4" value="1476-9344">
      European Journal of Information Systems
    </label>
  </div>
  <div class="radio">
    <label for="journal_radio-5">
      <input type="radio" name="journal_radio" id="journal_radio-5" value="1365-2575">
      Information Systems Journal
    </label>
  </div>
  <div class="radio">
    <label for="journal_radio-6">
      <input type="radio" name="journal_radio" id="journal_radio-6" value="0963-8687">
      Journal of Strategic Information Systems
    </label>
  </div>
  <div class="radio">
    <label for="journal_radio-7">
      <input type="radio" name="journal_radio" id="journal_radio-7" value="0268-3962">
      Journal of Information Technology
    </label>
  </div>
  <div class="radio">
    <label for="journal_radio-8">
      <input type="radio" name="journal_radio" id="journal_radio-8" value="0167-9236">
      Decision Support Systems
    </label>
  </div>
  <div class="radio">
    <label for="journal_radio-9">
      <input type="radio" name="journal_radio" id="journal_radio-9" value="0378-7206">
      Information and Management
    </label>
  </div>
  </div>
</div>

<!-- Article Name Input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="article_name">Article Name:</label>  
  <div class="col-md-5">
  <input id="article_name" name="article_name" type="text" placeholder="Article Name" class="form-control input-md" required="">
    
  </div>
</div>

<!-- Author 1-->
<div class="form-group">
  <label class="col-md-4 control-label" for="author1_name">Author 1:</label>  
  <div class="col-md-4">
  <input id="author1_name" name="author1_name" type="text" placeholder="Author Name" class="form-control input-md" required="">
    
  </div>
</div>

<!-- Author 1 University -->
<div class="form-group">
  <label class="col-md-4 control-label" for="author1_university">University:</label>  
  <div class="col-md-4">
  <input id="author1_university" name="author1_university" type="text" placeholder="Author University" class="form-control input-md" required="">
    
  </div>
</div>

<!-- Author 2-->
<div class="form-group">
  <label class="col-md-4 control-label" for="author2_name">Author 2:</label>  
  <div class="col-md-4">
  <input id="author2_name" name="author2_name" type="text" placeholder="" class="form-control input-md">
    
  </div>
</div>

<!-- Author 2 University-->
<div class="form-group">
  <label class="col-md-4 control-label" for="author2_university">University:</label>  
  <div class="col-md-4">
  <input id="author2_university" name="author2_university" type="text" placeholder="" class="form-control input-md">
    
  </div>
</div>

<!-- Author 3-->
<div class="form-group">
  <label class="col-md-4 control-label" for="author3_name">Author 3:</label>  
  <div class="col-md-4">
  <input id="author3_name" name="author3_name" type="text" placeholder="" class="form-control input-md">
    
  </div>
</div>

<!-- Author 3 University-->
<div class="form-group">
  <label class="col-md-4 control-label" for="author3_university">University:</label>  
  <div class="col-md-4">
  <input id="author3_university" name="author3_university" type="text" placeholder="" class="form-control input-md">
    
  </div>
</div>

<!-- Author 4-->
<div class="form-group">
  <label class="col-md-4 control-label" for="author4_name">Author 4: </label>  
  <div class="col-md-4">
  <input id="author4_name" name="author4_name" type="text" placeholder="" class="form-control input-md">
    
  </div>
</div>

<!-- Author 4 University-->
<div class="form-group">
  <label class="col-md-4 control-label" for="author4_university">University:</label>  
  <div class="col-md-4">
  <input id="author4_university" name="author4_university" type="text" placeholder="" class="form-control input-md">
    
  </div>
</div>

<!-- Author 5-->
<div class="form-group">
  <label class="col-md-4 control-label" for="author5_name">Author 5:</label>  
  <div class="col-md-4">
  <input id="author5_name" name="author5_name" type="text" placeholder="" class="form-control input-md">
    
  </div>
</div>

<!-- Author 5 University-->
<div class="form-group">
  <label class="col-md-4 control-label" for="author5_university">University:</label>  
  <div class="col-md-4">
  <input id="author5_university" name="author5_university" type="text" placeholder="" class="form-control input-md">
    
  </div>
</div>

<!-- Author 6-->
<div class="form-group">
  <label class="col-md-4 control-label" for="author6_name">Author 6:</label>  
  <div class="col-md-4">
  <input id="author6_name" name="author6_name" type="text" placeholder="" class="form-control input-md">
    
  </div>
</div>

<!-- Author 6 University-->
<div class="form-group">
  <label class="col-md-4 control-label" for="author6_university">University:</label>  
  <div class="col-md-4">
  <input id="author6_university" name="author6_university" type="text" placeholder="" class="form-control input-md">
    
  </div>
</div>

<!-- Select Year Published -->
<div class="form-group">
  <label class="col-md-4 control-label" for="year">Year Published</label>
  <div class="col-md-4">
    <select id="year" name="year" class="form-control">
      <option value="2010">2010</option>
      <option value="2011">2011</option>
      <option value="2012">2012</option>
      <option value="2013">2013</option>
      <option value="2014">2014</option>
      <option value="2015">2015</option>
    </select>
  </div>
</div>

<!-- Is Security Checkbox -->
<div class="form-group">
  <label class="col-md-4 control-label" for="is_security"></label>
  <div class="col-md-4">
    <label class="checkbox-inline" for="is_security-0">
      <input type="checkbox" name="is_security" id="is_security-0" value="1">
      Security Related
    </label>
  </div>
</div>

<!-- Submit Form Button -->
<div class="form-group">
  <div class="col-md-4 control-label">
    <button id="submit" name="submit" class="btn btn-primary">Submit</button>
  </div>
</div>

</fieldset>
</form>


</div>';
} 
if(isset($_POST['submit'])){
		
		$dbHost = "localhost";
		$dbUsername = "admin";
		$dbPassword = "";
		$dbName = "webdb";

		// Create connection
		$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
		// Check connection
		if ($db->connect_error) {
		die("Connection failed: " . $db->connect_error);
		}

    $journal = $_POST['journal_radio'];
    $article_name = $_POST['article_name'];
    $author1 = $_POST['author1_name'];
    $author1uni = $_POST['author1_university'];
    $author2 = $_POST['author2_name'];
    $author2uni = $_POST['author2_university'];
    $author3 = $_POST['author3_name'];
    $author3uni = $_POST['author3_university'];
    $author4 = $_POST['author4_name'];
    $author4uni = $_POST['author4_university'];
    $author5 = $_POST['author5_name'];
    $author5uni = $_POST['author5_university'];
    $author6 = $_POST['author6_name'];
    $author6uni = $_POST['author6_university'];
    $year = $_POST['year'];
    $security = $_POST['is_security'];

      //check if article exist
      $articlequery = $db->query("SELECT Article_Title FROM Articles WHERE Article_Title = '$article_name'");
      if($articlequery->num_rows != 0) {
           print 'Article Already Exists';
      } else {
		//insert if doesn't exit	
		$articleinsert = "INSERT INTO Articles (ISSN, Year_Published, Is_Security, Article_Title)
		VALUES ('$journal',$year,$security,'$article_name')";

		if ($db->query($articleinsert) === TRUE) {
		echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
		} else {
		echo "<script type= 'text/javascript'>alert('Error: " . $articleinsert . "<br>" . $db->error."');</script>";
		}



          }
  		$conn->close();
       
          
    }
          
?>

我把它缩小到这个区域,但似乎无法找到问题......

$articleinsert = "INSERT INTO Articles (ISSN, Year_Published, Is_Security, Article_Title)
    VALUES ('$journal',$year,$security,'$article_name')";

    if ($db->query($articleinsert) === TRUE) {
    echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
    } else {
    echo "<script type= 'text/javascript'>alert('Error: " . $articleinsert . "<br>" . $db->error."');</script>";
    }

3 个答案:

答案 0 :(得分:0)

我认为

$conn->close(); should be $db->close();

答案 1 :(得分:0)

INSERT语句需要围绕所有值/变量使用单引号。将它们放在$ journal和$ year附近,表格完全正常。

感谢@Mihai的回答

答案 2 :(得分:0)

您的查询

 INSERT INTO Articles (ISSN, Year_Published, Is_Security, Article_Title)
        VALUES ('$journal',$year,$security,'$article_name')

转入

INSERT INTO Articles (ISSN, Year_Published, Is_Security, Article_Title)
    VALUES ('".$journal."','".$year."','".$security."','".$article_name."')

我希望它会对你有所帮助。假设它也不起作用, 请逐一检查您的输入数据。 也许这是错的。 并检查数据库中的输入数据类型(varchar长度)。