2015-11-02 98 views
1

(這只是我的代碼不工作) 我試圖爲我的網站創建一個registerform,但是,這是一切都失敗的地方。這些變量被綁定到我的查詢中(這部分工作)並且INSERT將被執行。因此,我期望'if($ statement-> execute())'回顯1,但是,當插入實際發生在數據庫中時,它不迴應任何內容(甚至不是0)。我看着執行和看到: 返回值PHP mysqli執行不返回值

返回TRUE成功或FALSE失敗。

所以這對我來說沒有意義......我希望有人能幫助我!

$username = filter_var($_POST["username"]); 
$password = filter_var($_POST["password"]); 
$email = filter_var($_POST["email"]); 
$firstname = filter_var($_POST["firstname"]); 
$lastname = filter_var($_POST["lastname"]); 


$statement = $mysqli->prepare("INSERT INTO user (username, password, email, firstname, lastname) VALUES (?, ?, ?, ?, ?)"); 
$statement->bind_param('sssss', $username, $password, $email, $firstname, $lastname); 
if($statement->execute()) { 
echo 1; 
} 
else { 
    echo 0; 
} 

編輯:這是js代碼

function registerUser() { 
     var username = $("#userName").val(); 
     $("#userName").val(""); 
     var password = $("#password1").val(); 
     $("#password1").val(""); 
     $("#password2").val(""); 
     var firstname = $("#firstName").val(); 
     $("#firstName").val(""); 
     var secondname = $("#secondName").val(); 
     $("#secondName").val(""); 
     var email = $("#email").val(); 
     $("#email").val(""); 
     var result = false; 

     $.post('../php/register.php', {'username':username, 'password':password, 'email':email, 'firstname':firstname, 'lastname':secondname}, function(data) { 
      if(data == 1) { 
       result = true; 
      } 
      else { 
       result = false; 
      } 
     }); 
     return result; 
    } 
+0

我們可以看到代碼的其餘部分,或許有什麼地方干擾 – 2015-11-02 01:08:09

回答

0

是具有有效的連接它(回波就不會因爲結果仍然是假的來槽)?如果對你有意義,試試以下幾點:

更新:應用try catch看看有什麼問題的隊友。

假設連接變量值默認:

try{ 
    $servername = "localhost"; 
    $username = "username"; 
    $password = "password"; 
    $dbname = "myDB"; 

    // Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 

    // Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 

    // prepare and bind 
    $stmt = $conn->prepare("INSERT INTO user (username, password, email, firstname, lastname) VALUES (?, ?, ?, ?, ?)"); 
    $statement->bind_param('sssss', $username, $password, $email, $firstname, $lastname); 

    . 
    . 
    } 
catch(PDOException $e) 
    { 
    echo "Error: " . $e->getMessage(); 
    } 
+0

他說,其插入到數據庫中,這樣的連接是沒有問題的 – 2015-11-02 01:09:54

+0

笑,我壞。看起來像閱讀障礙與年齡:( – Ash

+0

哈哈,這是沒有問題的。你正在嘗試幫助,應該永遠不會道歉:) – 2015-11-02 01:11:37