2017-02-04 76 views
0

我能夠插入和從mysql數據庫中選擇沒有任何問題,但無法更新記錄。更新語句不工作(正常語句以及準備語句)

最初我使用mysqli_query時沒有任何結果或錯誤。後來我嘗試了沒有結果的準備好的陳述。

分享我的代碼到這裏:

define('DB_SERVER', 'localhost'); 
define('DB_USERNAME', 'xxxx'); 
define('DB_PASSWORD', 'xxxx'); 
define('DB_DATABASE', 'xxxx'); 
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); 

//the function 
function SaveLineItem() { 
    $sql = "update lineitems set deliverycost=? "; 

    $tdetail = ""; 
    if($_POST['tdetail']){ 
     $sql .= ",tracking_details=?" ; 
     $tdetail = $_POST['tdetail']; 
    } 

    $sql .= " where id=?"; 

    $dc = $_POST['dc']; 
    $lineItemId = $_POST['lineItemId']; 
    echo $sql; 

    $stmt = mysqli_prepare($db, $sql); 
    echo "<br>after prepare statement"; 

    if($tdetail<>""){ 
     mysqli_stmt_bind_param($stmt,"isi",$dc1,$tdetail1,$lineItemId1); 
    }else { 
     mysqli_stmt_bind_param($stmt,"ii",$dc1,$lineItemId1); 
    } 

    echo "<br>after binding"; 

    $dc1 = $dc; 
    if($tdetail<>""){ 
     $tdetail1 = $tdetail; 
     echo "<br>detail param bound"; 
    } 

    $lineItemId1 = $lineItemId; 
    mysqli_stmt_execute($stmt); 

    echo "<br>statement executed"; 
    //header("location:../welcome.php"); 
} 

輸出是:

update lineitems set deliverycost=? ,tracking_details=? where id=? 
after prepare statement 
after binding 
detail param bound 
statement executed 

但在檢查表沒有及時更新。 從昨天開始嘗試,我錯過了什麼?

更新: 此代碼最初是在Windows中開發的,但後來我決定繼續在Ubuntu 16.04上工作。它似乎在Ubuntu甚至插入不工作。它可能是Ubuntu的安全相關的問題?

+0

使用'mysqli_stmt_affected_rows'來檢查受更新語句影響的行數。請記住,如果輸出正確,代碼可能不會從查詢部分失敗。檢查什麼參數正在通過及其價值。也許現在由於綁定錯誤而導致更新結果。最有可能的是,你應該在整數的'$ _POST'中使用intval時設置整數 –

回答

0

請嘗試此操作。當您在mysqli_stmt_bind_param中使用時,您有$dc1未定義。對於你$dc1持有更新記錄的id。如果這是空的,那就是爲什麼你不會在數據庫中看到任何改變。查詢運行但不影響任何行。

define('DB_SERVER', 'localhost'); 
define('DB_USERNAME', 'xxxx'); 
define('DB_PASSWORD', 'xxxx'); 
define('DB_DATABASE', 'xxxx'); 
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); 

//the function 
function SaveLineItem() { 
    $sql = "update lineitems set deliverycost=? "; 

    $tdetail = ""; 
    if($_POST['tdetail']){ 
     $sql .= ",tracking_details=?" ; 
     $tdetail = $_POST['tdetail']; 
    } 

    $sql .= " where id=?"; 

    $dc = intval($_POST['dc']); 
    $lineItemId = intval($_POST['lineItemId']); 
    echo $sql; 
    $stmt = mysqli_prepare($db, $sql); 
    echo "<br>after prepare statement"; 
    // define $dc1 here since you use it below. If it is undefined could resolve to an error or a wrong query. 
    $dc1 = $dc;  
    if($tdetail<>""){ 
     mysqli_stmt_bind_param($stmt,"isi",$dc1,$tdetail1,$lineItemId1); 
    }else { 
     mysqli_stmt_bind_param($stmt,"ii",$dc1,$lineItemId1); 
    } 

    echo "<br>after binding"; 


    if($tdetail<>""){ 
     $tdetail1 = $tdetail; 
     echo "<br>detail param bound"; 
    } 

    $lineItemId1 = $lineItemId; 
    mysqli_stmt_execute($stmt); 

    echo "<br>statement executed"; 
    mysqli_stmt_close($stmt); 
    echo "<br>statement closed"; 
    //header("location:../welcome.php"); 
} 
0

嘗試將execute語句放入一個變量中並在最後回顯它,以便查看您的查詢是否失敗。

然後嘗試將查詢的輸出放入phpmyadmin中,查看是否有錯誤。

+0

同樣的代碼在Windows 10中插入和更新的預期。最近我把Ubuntu升級到了16.04,所以恐怕它可以和它聯繫起來。添加Ubuntu標籤到這個問題。 –