2015-08-08 71 views
0

,我發現了以下錯誤:mysqli的命令不同步

「命令不同步,你無法運行此命令現在」

在此代碼:

$check = $dbc->prepare("SELECT field FROM table WHERE field = ?"); 
mysqli_stmt_bind_param($check, "s", $value1); 
mysqli_stmt_execute($check); 
mysqli_stmt_bind_result($check, $info); 
mysqli_stmt_fetch($check); 

if ($info == $static_value) { 

    $update = $dbc->prepare("UPDATE table SET field = 'valued' WHERE(field1 = ? AND field2 = ?)LIMIT 1"); 
    mysqli_stmt_bind_param($update, "ss", $value1, $value2); 
    mysqli_stmt_execute($update); 

代碼看起來正確,我錯了?

+0

你得到什麼錯誤? – Script47

+0

對不起,我現在編輯添加更多的信息,我得到的錯誤是:「命令不同步,你現在不能運行這個命令」 – BlackSys

+0

檢查我的答案。 – Script47

回答

1

update查詢看起來很錯。嘗試下面的下面,

你的老update

$update = $dbc->prepare("UPDATE table SET field = 'valued' WHERE(field1 = ? AND field2 = ?)LIMIT 1"); 
mysqli_stmt_bind_param($update, "ss", $value1, $value2); 
mysqli_stmt_execute($update); 

你的新$update

$update = mysqli_prepare($dbc, "UPDATE `yourTableName` SET `field` = 'valued' WHERE field1 = ? AND field2 = ?"); 

LIMIT can be used with UPDATE but with the row count only.

編輯1

你似乎是混合OOMySQLiprocedural,請閱讀this頁面。

編輯2

你有相當多的問題與您的代碼。

  1. 您試圖訪問$email$key當他們走出if的範圍,所以我增加了新的變數。

  2. 你繼續(如上所述)混合你的OOprocedural

  3. 我試圖執行$update時添加了一些調試。

    <?php 
    
    $email; 
    $key; 
    
    if (isset($_GET['email']) && preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $_GET['email'])) { 
        $email = $_GET['email']; 
    } 
    if (isset($_GET['key']) && (strlen($_GET['key']) == 32)) //The Activation key will always be 32 since it is MD5 Hash 
        { 
        $key = $_GET['key']; 
    } 
    
    
    if (isset($email) && isset($key)) { 
        $check_code = mysqli_prepare($dbc, "SELECT Activation FROM members WHERE Email = ?"); 
        mysqli_stmt_bind_param($check_code, "s", $email); 
        mysqli_stmt_execute($check_code); 
        mysqli_stmt_bind_result($check_code, $activation); 
        mysqli_stmt_fetch($check_code); 
    
        if ($activation == $key) { 
    
         // Update the database to set the "activation" field to null 
    
         $update = mysqli_prepare($dbc, "UPDATE `members` SET `Activation` = 'Activated' WHERE `Email` = ? AND `Activation` = ?"); 
         mysqli_stmt_bind_param($update, "ss", $email, $key); 
         mysqli_stmt_execute($update); 
    
         if (!mysqli_stmt_execute($update)) { 
          die("Error: " . mysqli_stmt_error($update)); 
         } 
    
    
         // Print a customized message: 
         if (mysqli_affected_rows($dbc) == 1) { //if update query was successfull 
    
          echo '<div class="success">Your account is now active. You may now <a href="login.php">Log in</a></div>'; 
    
         } else { 
          echo '<div class="errormsgbox">Oops !Your account could not be activated. Please recheck the link or contact the system administrator.</div>'; 
          echo '<br/> ' . $dbc->error; 
    
         } 
    
         mysqli_close($dbc); 
    
        } else { 
    
         echo "Parameters wrong, wrong link?"; 
    
        } 
    
    } else { 
        echo '<div class="errormsgbox">Error Occured .</div>'; 
    } 
    ?> 
    
+0

@BlackSys現在試試。 – Script47

+0

我有點困惑,所以我必須做什麼?根據你鏈接的頁面,是不是這兩個函數的行爲是一樣的?編輯:與上面相同的錯誤。我真的不明白,語法看起來正確 – BlackSys

+0

嘗試下面的代碼*你的新'$ update' *。 – Script47

0

的Wooo爲你混合程序和OOP開始,其次你還沒有關閉了連接,這是另一種錯誤:)

$check=$dcv->prepare('SELECT field FROM table where field=?'); 
$check->bind_param('s',$value1); 
$check->execute(); 
$check->bind_result($info); 
$check->fetch(); 
$check->close(); 
if($info==$static_value){ 
    $update=$dvc->prepare('UPDTATE table SET field='valued' WHERE field1=? AND field2=?'); 
    $update->bind_param('ss',$value1,$value2); 
    $result=$update->execute(); 
    $update->close(); 
}