2015-03-31 91 views
0

下面的腳本拋出我這個錯誤:無效的參數號SQLSTATE [HY093]:沒有定義的參數

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

錯誤來自這些行:

$query .= "WHERE username1=:un1"; 
$binValues["un1"] = $_POST['username1']; 

是否有與PHP的任何問題句法?

我完整的腳本:

<?php 

    require_once "config.inc.php"; 

    $query = "UPDATE customer SET "; 
    $binValues = []; 


    if(!empty($_POST['eidosmetaf1'])) { 
     $query .= "eidosmetaf1 = :e1"; 
     $binValues["e1"] = $_POST['eidosmetaf1']; 
    } 

    if(!empty($_POST['weight1'])) { 
     $query .= ",weight1 = :w1"; 
     $binValues["w1"] = $_POST['weight1']; 
    } 

    if(!empty($_POST['startNomos1'])){ 
     $query .= ",startNomos1 = :sn1"; 
     $binValues["sn1"] = $_POST['startNomos1']; 
    } 

    if(!empty($_POST['startPoli1'])) { 
     $query .= ",startPoli1 = :sc1"; 
     $binValues["sc1"] = $_POST['startPoli1']; 
    } 

    if(!empty($_POST['start_lat'])) { 
     $query .= ",start_lat = :slat1"; 
     $binValues["slat1"] = $_POST['start_lat']; 
    } 

    if(!empty($_POST['start_lng'])) { 
     $query .= ",start_lng = :slng1"; 
     $binValues["slng1"] = $_POST['start_lng']; 
    } 

    if(!empty($_POST['finalNomos1'])) { 
     $query .= ",finalNomos1 = :fn1"; 
     $binValues["fn1"] = $_POST['finalNomos1']; 
    } 

    if(!empty($_POST['finalPoli1'])) { 
     $query .= ",finalPoli1 = :fc1"; 
     $binValues["fc1"] = $_POST['finalPoli1']; 
    } 

    if(!empty($_POST['final_lat'])) { 
     $query .= ",final_lat = :flat1"; 
     $binValues["flat1"] = $_POST['final_lat']; 
    } 

    if(!empty($_POST['final_lng'])) { 
     $query .= ",final_lng = :flng1"; 
     $binValues["flng1"] = $_POST['final_lng']; 
    } 

    if(!empty($_POST['depDate1'])) { 
     $query .= ",depDate1 = :dD1"; 
     $binValues["dD1"] = $_POST['depDate1']; 
    } 

    if(!empty($_POST['depTime1'])) { 
     $query .= ",depTime1 = :dT1"; 
     $binValues["dT1"] = $_POST['depTime1']; 
    } 

    if(!empty($_POST['specialservices1'])) { 
     $query .= ",specialservices1 = :ex1"; 
     $binValues["ex1"] = $_POST['specialservices1']; 
    } 

    if(!empty($_POST['comments1'])) { 
     $query .= ",comments1 = :c1"; 
     $binValues["c1"] = $_POST['comments1']; 
     } 

     //error here 
     $query .= "WHERE username1=:un1"; 
     $binValues["un1"] = $_POST['username1'];  


     $query .= "and comments1=:c1_old"; 
     $binValues["c1_old"] = $_POST['comments2_old']; 


    try { 
     $stmt = $db->prepare($query); 
     $stmt->execute($binValues); 

    } catch (PDOException $ex) { 
     $response["success"] = 0; 
     $response["message"] = "Database Error2. Please Try Again!"; 
     echo $ex->getMessage(); 
     die(json_encode($response)); 
    }  

    $response["success"] = 1; 
    $response["message"] = "..............!"; 
    echo json_encode($response); 


?> 
+0

類似的問題:http://stackoverflow.com/q/29354264/3933332你不覺得? – Rizier123 2015-03-31 12:24:18

+0

* Hm ... *您可能需要在'where' ---'$ query。=「WHERE username1 =:un1」之前留出空格;' – 2015-03-31 12:26:37

+1

您好,Rizier,您是否永遠在線;哈哈哈。但我堅持:現在在特定的路線上是什麼問題; – johnnal 2015-03-31 12:27:16

回答

1

正如我已經解決了以前的代碼,我會建議你從我的答案使用代碼:https://stackoverflow.com/a/29354781/3933332因爲它是一個簡單得多閱讀和理解。

而且如果你用我的代碼,你可以添加你WHERE條款是這樣的:

<?php 

    //... 

    foreach($checkedValues as $k => $v) { 
     $query .= "$v = :$k,"; 
     $bindValues[$k] = $_POST[$v]; 
    } 

    $query = rtrim($query, ","); 

    //fixed code here 
    if(isset($_POST['username1'])) { 
     $query .= " WHERE username1=:un1"; 
     $bindValues["un1"] = $_POST["username1"]; 
    } 

    if(isset($_POST['comments1'])) { 
     $query .= " AND comments1=:c1_old"; 
     $bindValues["c1_old"] = $_POST["comments1"]; 
    } 


    try {   
     $stmt = $db->prepare($query); 
     $stmt->execute($binValues);   
    } catch (PDOException $ex) { 
     $response["success"] = 0; 
     $response["message"] = "Database Error2. Please Try Again!"; 
     echo $ex->getMessage(); 
     die(json_encode($response)); 
    }  

    //... 

?> 
+0

但我想我複製 - 粘貼你的代碼..在你更新之前 – johnnal 2015-03-31 12:45:27

+0

@johnnal然後回到我的最後一個答案,看看我的更新代碼:D然後,你也可以在這裏結合新代碼。 – Rizier123 2015-03-31 12:53:17

+0

好的,我會的。但問題依然存在:爲什麼我提供的特定代碼是錯誤的; – johnnal 2015-03-31 12:56:04

-1
$binValues[":un1"] = $_POST['username1'] 
+0

添加:不起作用。請注意,例如「$ binValues [」dD1「] = $ _POST ['depDate1'];」正在工作完美 – johnnal 2015-03-31 12:39:10

相關問題