2015-03-30 106 views
0

我是相當新的php。我得到行($ stmt-> bindParam(「:e1」,$ _POST ['eidosmetaf1']);)錯誤「Undefined variable:stmt在「。這似乎是一個基本的錯誤,但我無法弄清楚。所以,任何幫助表示讚賞php - 未定義的變量:stmt in

<?php 

require("config.inc.php"); 

    $query = "UPDATE customer SET "; 
if(isset($_POST['eidosmetaf1'])){ 
$stmt->bindParam(":e1", $_POST['eidosmetaf1']); 
    $query .= "eidosmetaf1 = :e1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['weight1'])){ 
$stmt->bindParam(":w1", $_POST['weight1']); 
    $query .= "weight1 = :w1"; 
} 

    $query = "UPDATE customer SET "; 
if(isset($_POST['startNomos1'])){ 
$stmt->bindParam(":sn1", $_POST['startNomos1']); 
    $query .= "startNomos1 = :sn1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['startPoli1'])){ 
$stmt->bindParam(":sc1", $_POST['startPoli1']); 
    $query .= "startPoli1 = :sc1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['start_lat'])){ 
$stmt->bindParam(":slat1", $_POST['start_lat']); 
    $query .= "start_lat = :slat1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['start_lng'])){ 
$stmt->bindParam(":slng1", $_POST['start_lng']); 
    $query .= "start_lng = :slng1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['finalNomos1'])){ 
$stmt->bindParam(":fn1", $_POST['finalNomos1']); 
    $query .= "finalNomos1 = :fn1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['finalPoli1'])){ 
$stmt->bindParam(":fc1", $_POST['finalPoli1']); 
    $query .= "finalPoli1 = :fc1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['final_lat'])){ 
$stmt->bindParam(":flat1", $_POST['final_lat']); 
    $query .= "final_lat = :flat1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['final_lng'])){ 
$stmt->bindParam(":flng1", $_POST['final_lng']); 
    $query .= "final_lng = :flng1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['depDate1'])){ 
$stmt->bindParam(":dD1", $_POST['depDate1']); 
    $query .= "depDate1 = :dD1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['depTime1'])){ 
$stmt->bindParam(":dT1", $_POST['depTime1']); 
    $query .= "depTime1 = :dT1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['specialservices1'])){ 
$stmt->bindParam(":ex1", $_POST['specialservices1']); 
    $query .= "specialservices1 = :ex1"; 
} 

$query = "UPDATE customer SET "; 
if(isset($_POST['comments1'])){ 
$stmt->bindParam(":c1", $_POST['comments1']); 
    $query .= "comments1 = :c1"; 
} 


    try { 
     $stmt = $db->prepare($query); 
     $stmt->execute(); 
    } 
    catch (PDOException $ex) { 

     $response["success"] = 0; 
     $response["message"] = "Database Error2. Please Try Again!"; 
     die(json_encode($response)); 
    }  

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


?> 
+0

首先準備語句然後綁定你的價值觀 – Rizier123 2015-03-30 19:09:53

+0

準備好你的語句,然後綁定值。 – 2015-03-30 19:11:46

+0

抱歉,您的意思是移動「$ stmt = $ db-> prepare($ query);」在頂端; – johnnal 2015-03-30 19:13:12

回答

1

你在這裏搞了幾件事!

你綁定你的價值觀之前,您需要準備您的查詢,否則怎麼PHP的應該知道在哪裏以及如何應不當時存在的查詢值綁定!還要在每個if語句之前覆蓋$query變量。

所以,你的代碼應該是這個樣子:

在這裏,我先通過其持有的佔位符指數和POST變量指數值,在那裏我有array_filter()$_POST變量的設置和檢查數組$checkPostIndex哪些不是,我使用isset()來處理,並將其過濾掉。

在此之後,我循環訪問$checkedValues數組,該數組保存的數據與$checkPostIndex完全相同,但只能使用設置的POST變量。通過這個,我創建了查詢並創建了佔位符數組,該佔位符數組將佔位符保存爲索引,將POST變量值保存爲值。

然後,我只需要從查詢字符串中修剪最後一個逗號rtrim(),然後就可以執行查詢了。

<?php 

    require_once "config.inc.php"; 

    $query = "UPDATE customer SET "; 
    $checkPostIndex = ["e1" => "eidosmetaf1", "w1" => "weight1", "sn1" => "startNomos1", "sc1" => "startPoli1", "slat1" => "start_lat", "slng1" => "start_lng", "fn1" => "finalNomos1", 
    "fc1" => "finalPoli1", "flat1" => "final_lat", "flng1" => "final_lng", "dD1" => "depDate1", "dT1" => "depTime1", "ex1" => "specialservices1", "c1" => "comments1"]; 
    $bindValues = []; 

    $checkedValues = array_filter($checkPostIndex, function($v){ 
     return isset($_POST[$v]); 
    }); 

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

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

    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); 

?> 

旁註:

我會建議你在你的文件(S),可幫助您查找錯誤的頂部添加error reporting。只有當分期(不生產!):

<?php 
    ini_set("display_errors", 1); 
    error_reporting(E_ALL); 
?> 

也使error mode爲連接後立即您的PDO連接:

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
+0

也許你及彼查詢errer;) – 2015-03-30 19:47:10

+0

@superduperawesomephpexpert爲什麼? – Rizier123 2015-03-30 19:53:55

+0

非常詳細的答案。謝謝 – johnnal 2015-03-30 20:00:12

0
require("config.inc.php"); 
$query=array(); 
foreach(array('eidosmetaf1','...') as $k){ 
if(isset($_POST[$k])){ 
    $query[]= "$k = :$k"; 
} 
} 
$query = "UPDATE customer SET ".imolde(',',$query); 
$query .= "";#where clause is missing 
try { 

    $stmt = $db->prepare($query); 
    foreach(array('eidosmetaf1','...') as $k){ 
     if(isset($_POST[$k])){ 
      $stmt->bindParam(":$k", $_POST[$k]); 
     } 
    } 
    $stmt->execute(); 
} catch (PDOException $ex) { 
$response["success"] = 0; 
$response["message"] = "Database Error2. Please Try Again!"; 
die(json_encode($response)); 
}  

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

試試這樣:準備語句字符串創建PDO ,備查詢BIND PARAMS,EXECUTE