2013-10-23 75 views
1

我遇到此錯誤:無法運行查詢:SQLSTATE [23000]:完整性約束違規:1048'membersince'列不能be null無法運行查詢:SQLSTATE [23000]:完整性約束違規:1048'membersince'列不能爲空

這裏我試圖運行我的註冊頁面。

這裏是代碼。我錯過了什麼?我檢查了我的數據庫表/輸入名稱都正確命名爲「membersince」,那麼究竟是怎麼回事? :(

<?php     
    require("config.php"); 
    if(!empty($_POST)) 
    { 


     // ===================================================== 

     // Check if the username is already taken 
     $query = " 
      SELECT 
       1 
      FROM users 
      WHERE 
       username = :username 
     "; 
     $query_params = array(':username' => $_POST['username']); 
     try { 
      $stmt = $db->prepare($query); 

      $result = $stmt->execute($query_params); 

     } 
     catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); } 
     $row = $stmt->fetch(); 
     if($row){ die("This username is already in use"); } 


     $query = " 
      SELECT 
       1 
      FROM users 
      WHERE 
       email = :email 
     "; 
     $query_params = array( 
      ':email' => $_POST['email'] 
     ); 
     try { 
      $stmt = $db->prepare($query);  
      $result = $stmt->execute($query_params); 
     } 
     catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage());} 
     $row = $stmt->fetch(); 
     if($row){ die("This email address is already registered"); } 



     $query = " 
      SELECT 
       1 
      FROM users 
      WHERE 
       nric = :nric 
     "; 
     $query_params = array(':nric' => $_POST['nric']); 
     try { 
      $stmt = $db->prepare($query);  
      $result = $stmt->execute($query_params); 
     } 
     catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); } 
     $row = $stmt->fetch(); 
     if($row){ die("This NRIC is already in use"); } 


       $query = " 
      SELECT 
       1 
      FROM users 
      WHERE 
       mobilenumber = :mobilenumber 
     "; 
     $query_params = array(':mobilenumber' => $_POST['mobilenumber']); 
     try { 
      $stmt = $db->prepare($query);  
      $result = $stmt->execute($query_params); 
     } 
     catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); } 
     $row = $stmt->fetch(); 
     if($row){ die("This Mobile Number is already in use"); } 





     //---------------------------------------------------- Add row to database 
     $query = " 
      INSERT INTO users ( 
       username, 
       password, 
       salt, 
       email, 
       name, 
       gender, 
       dateofbirth, 
       nric, 
       address, 
       postalcode, 
       mobilenumber, 
       profession, 
       membersince 



      ) VALUES ( 
       :username, 
       :password, 
       :salt, 
       :email, 
       :name, 
       :gender, 
       :dateofbirth, 
       :nric, 
       :address, 
       :postalcode, 
       :mobilenumber, 
       :profession, 
       :membersince 


      ) 
     "; 

     // Security measures 
     $salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647)); 
     $password = hash('sha256', $_POST['password'] . $salt); 
     for($round = 0; $round < 65536; $round++){ $password = hash('sha256', $password . $salt); } 
     $query_params = array( 
      ':username' => $_POST['username'], 
      ':password' => $password, 
      ':salt' => $salt, 
      ':email' => $_POST['email'], 
      ':name' => $_POST['name'], 
      ':gender' => $_POST['gender'], 
      ':dateofbirth' => $_POST['dateofbirth'], 
      ':nric' => $_POST['nric'], 
      ':address' => $_POST['address'], 
      ':postalcode' => $_POST['postalcode'], 
      ':mobilenumber' => $_POST['mobilenumber'], 
      ':profession' => $_POST['profession'], 
      ':membersince' => $_POST['membersince'], 
     ); 
     try { 
      $stmt = $db->prepare($query); 

      $result = $stmt->execute($query_params); 
     } 
     catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); } 
     header("Location: successful.php"); 
     die("Redirecting to successful.php"); 
    }        

?> 
<!-- Author: Michael Milstead/Mode87.com 
    for Untame.net 
    Bootstrap Tutorial, 2013 
--> 




<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 
    <link rel="shortcut icon" href="../../assets/ico/favicon.png"> 

    <title>Sign Up -</title> 

    <!-- Bootstrap core CSS --> 
    <link href="css/bootstrap.css" rel="stylesheet"> 

    <!-- Custom styles for this template --> 
    <link href="jumbotron.css" rel="stylesheet"> 


    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --> 
    <!--[if lt IE 9]> 
<script src="../../assets/js/html5shiv.js"></script> 
<script src="../../assets/js/respond.min.js"></script> 
<![endif]--> 
    </head> 

<body> 
    <?php 
if (empty($_SESSION['user'])) 
{ 
include_once("header.php"); 
} 
else  { 
     include_once("header2.php"); 
     }?> 

<div class="page-header"> 
     <div class="container"> 

    <h1> Register </h1> 


    <form action="register2.php" method="post" role="form"> 
    <div class="form-group"> 
     <label>Username:</label> 
     <input type="text" name="username" value="" maxlength="20" autofocus required/> 

     </div> 
       <div class="form-group"> 
     <label>Email: <strong style="color:darkred;">*</strong></label> 
     <input type="email" name="email" value="" maxlength="20" required/> 
       </div> 

          <div class="form-gtroup"> 
     <label>Password:</label> 
     <input type="password" name="password" value="" maxlength="20" required/> <br /><br /> 
           </div> 

     <div class="form-group"> 
      <label>Name:</label> 
     <input type="text" name="name" value="" maxlength="20" required/> 
          </div> 





           <div class="form-group"> 
           <label>Gender:</label> 
         <div class="radio"> 

    <label> 
    <input type="radio" name="gender" id="male" value="male" required> 
    Male 
    </label> 
</div> 
<div class="radio"> 
    <label> 
    <input type="radio" name="gender" id="female" value="female" required> 
    Female 
    </label> 
</div> 
      </div> 

           <div class="form-group"> 

           <label>Date of Birth:</label> 
           <input type="date" name="dateofbirth" value="" max="1997-12-31" required> 
            </div> 


         <div class="form-group">   
     <label>NRIC:</label> 
     <input type="text" name="nric" value="" maxlength="9" required/> 
           </div> 

           <div class="form-group"> 
     <label>Address:</label> 
     <input type="text" name="address" value="" maxlength="50" /> 
           </div> 

           <div class="form-group">  
     <label>Postal Code:</label> 
     <input type="text" name="postalcode" value="" maxlength="6" pattern="[1-9]{6}" title="Postal code must be 6 digit!" required/> 
          </div> 


           <div class="form-group"> 
     <div class="input-group"> 
    <span class="input-group-addon">+65</span> 
    <input type="text" name="mobilenumber" value="" class="form-control" placeholder="Mobile Number" maxlength="8" pattern="[1-9]{8}" title="Mobile Number must be 6 digit!" required> 
</div> 
</div> 



        <div class="form-group"> 
     <label>Profession:</label> 
     <input type="text" name="profession" value="" maxlength="20" required> 


       </div>   

         <div class="form-group"> 
      <input class="text" name="membersince" value=" <?php> $todaysdate = date("Y-m-d"); echo $todaysdate?>" type="text" disabled> 
         </div>                

     <input type="submit" class="btn btn-info" value="Register" /> 
     <input type="reset" class="btn btn-info" value="Reset">  


    </form> 








</div>  
</div>       

     <?php include_once("footer.php");?> 
</body> 
</html> 
+0

那麼在猜測$ _POST ['membersince']爲null時,你設置插入語句的參數。 –

+0

爲什麼在'membersince'輸入字段中的<?php'之後有一個右括號('>')? – Shell

回答

0

我有一個類似的問題,但我固定它,所以想和大家分享,可能是這將是有用的人......

通常當你遇到

完整性約束衝突:1048列NAME_OF_YOUR_COLUMN

此,如果變量沒有設置,你的情況一般發生,這是造成這一問題的關鍵是

<input class="text" name="membersince" 
     value=" <?php> $todaysdate = date("Y-m-d"); echo $todaysdate?>" 
     type="text" disabled> 

所以,如果你看到這裏,你是使用disabled屬性爲這一特定領域,因此,您form元素將不在此字段的值發送到服務器,因此......當您使用以下查詢

':membersince' => $_POST['membersince'] 

會導致null因爲有一個在$_POST陣列沒有membersince關鍵。所以確保你正在分配的變量應該被設置。

+1

不能確定-1 ..但是,1+。 –

+0

@JoshCrozier我最近連續下了兩次投票:)並且謝謝 –

0

嘗試使用readonly而不是disabled。禁用的字段值不能通過form標籤發佈。

<input class="text" name="membersince" 
     value=" <?php echo date("Y-m-d"); ?>" 
     type="text" readonly="readonly"> 
0

我面臨同樣的問題。我的表單中的「字段名稱」和數據庫的列名稱不同。我的問題是: "Integrity constraint violation: 1048 Column 'MoneyMethod' cannot be null"。 我的表單輸入標籤:{!! Form::select("MoneyMethod", $MoneymethodInfo, null,["class"=>"form-control MoneyMethod required","id"=>"MoneyMethod"]) !!},注意'MoneyMethod'的拼寫和我的數據庫表$table->string('MoneyMethod', 100);'MoneyMethod'的拼寫與表單相同,但在我的控制器中$riskfund->Moneymethod = Input::get('Moneymethod');仔細查看'Moneymethod'的拼寫與我的表單和數據庫的不同表列。糾正拼寫後,它現在正在工作。 因此,請檢查表單,控制器,數據庫表中'membersince'的拼寫。它可能會幫助你。

相關問題