2

我遇到了这个错误:无法运行查询: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>
4

3 回答 3

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没有键。因此,请确保应设置您分配的变量。membersince$_POST

于 2014-08-14T09:52:09.687 回答
0

尝试使用readonly而不是disabled. disabled 字段值不能通过form标签发布。

<input class="text" name="membersince" 
       value=" <?php echo date("Y-m-d"); ?>" 
       type="text" readonly="readonly">
于 2014-08-15T07:12:43.677 回答
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”的拼写。它可能会帮助你。

于 2017-08-21T12:45:39.120 回答