2015-08-24 167 views
0

我有下面的代碼,它應該添加一個數據庫值,它說它正在做,但由於某種原因沒有創建數據庫記錄。數據庫記錄沒有被創建?

每個$ values []數組項都有一個值。我收到的消息是「回聲」消息:「用戶成功創建,請確認您的電子郵件,然後才能登錄」。

(顯然沒有完成更好的驗證將完成並 加密和加鹽加ECT)

下面是代碼:

<?php 
//Login Script 
require("dbconnect.php"); 
require("functions.php"); 

$message = array(); 

//Check values 
$values = array("fname" => clean($_POST['fname']), 
       "sname" => clean($_POST['sname']), 
       "email" => clean($_POST['email']), 
       "reg" => clean($_POST['reg']), 
       "pass" => clean($_POST['password']), 
       "repass" => clean($_POST['repassword'])); 

$error = true; //If all tests pass there will be no error. 
//Check that there is a value in field 
$checks = false; 
if (strlen($values['fname']) > 2) { 
    if (strlen($values['sname']) > 2) { 
     if (strlen($values['email']) > 2) { 
       if (strlen($values['pass']) > 2) { 
        if ($values['repass'] == $values['pass']) { 
         //All tests pass 
         $error = false; 
        } else { 
         //Passwords don't match 
         exit("Passwords do not match"); 
        } 
       } else { 
        //No password entered 
        exit("You must enter a password"); 
       } 
     } else { 
      //No email entered 
      exit("Please enter a valid email address"); 
     } 
    } else { 
     //No second name entered 
     exit("Please enter your second name"); 
    } 
} else { 
    //No first name entered 
    exit("Please enter your first name"); 
} 

//Encrypt password and generate salt 
$values['salt'] = generateRandomString(32); 

if ($error == false) { 
    $user_exists = true; //Check if use exists 
    if ($userExist = $conn->prepare("SELECT id FROM users WHERE email=?")) { 
     $userExist->bind_param('s', $email); 
     $userExist->execute(); 
     $userExist->store_result(); 
     $numRows = $userExist->num_rows; 

     //If user exists don't create another one 
     if ($numRows < 1) { 
      //Connection already established, prepare bind. 
      if ($stmt = $conn->prepare("INSERT INTO users (firstname,secondname,email,password,reg,salt) VALUES (?,?,?,?,?,?)")) { 
       $stmt->bind_param("ssssss", 
           $values['fname'], 
           $values['sname'], 
           $values['email'], 
           $values['password'], 
           $values['reg'], 
           $values['salt']); 
       $stmt->execute(); //Add user 
      } else { 
       //Error creating user 
       printf("Error message: %s\n", $conn->error); 
      } 
      //Close the connections 
      $stmt->close(); 
      $conn->close(); 

      //User created 
      echo "User successfully created, please confirm your email before you can login"; 
     } else { 
      echo "User already exists with entered email"; 
     } 
     //Close connection 
     $userExist->close(); 
    } else { 
     //Error checking if user exists 
     printf("Error message: %s\n", $conn->error);  
    } 
} 
?> 
+2

您必須檢查$ stmt-> execute後的錯誤。你假設一切正常,但很有可能你得到一個SQL例外 – MiltoxBeyond

+1

不知道'clean()'做了什麼以及是否需要它。您正在使用準備好的語句 –

+1

如果語句已成功準備,則會顯示您成功創建的日誌消息。這並不一定意味着execute()調用是成功的 – e4c5

回答

0

我傻了錯字,我已經把$ values ['password']而不是$ values ['pass']!忘了檢查execute()是否已經正確執行,我現在已經完成了,謝謝你們!