我在php中相当新。我上线($ stmt-> bindParam(“:e1”,$ _POST ['eidosmetaf1']);)错误“未定义的变量: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 :(得分:1)
你在这里乱七八糟的东西!
你需要准备你的查询之前你绑定你的值,否则php应该如何知道它应该在当时不存在的查询中绑定值的位置和方式!您还要在每个if语句之前覆盖$query
变量。
所以你的代码应该是这样的:
这里我首先浏览一个数组$checkPostIndex
,它将占位符作为索引,POST变量索引作为值,我在array_filter()
检查设置了$_POST
个变量,哪些是不是,我用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);
?>
旁注:
我建议您在文件顶部添加error reporting,以帮助您查找错误。只有在升级时(不在生产中!):
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>
在连接后立即为您的PDO连接启用error mode:
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
答案 1 :(得分: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,准备查询,绑定参数,执行