我在php中相当新。我有以下php脚本:
<?php
require("config.inc.php");
$query = "UPDATE customer
SET eidosmetaf1=:e1 , weight1=:w1 , startNomos1=:sn1 , startPoli1=:sc1 , start_lat=:slat1 , start_lng=:slng1 ,
finalNomos1=:fn1 , finalpoli1=:fc1 , final_lat=:flat1 , final_lng=:flng1 , depDate1=:dD1 , depTime1=:dT1 ,
specialservices1=:ex1 , comments1=:c1
WHERE username1=:un1 and weight1=:w1_old and comments1=:c1_old";
$query_params = array(
':un1' => $_POST['username1'],
':e1' => $_POST['eidosmetaf1'],
':w1' => $_POST['weight1'],
':sn1' => $_POST['startNomos1'],
':sc1' => $_POST['startPoli1'],
':slat1' => $_POST['start_lat'],
':slng1' => $_POST['start_lng'],
':fn1' => $_POST['finalNomos1'],
':fc1' => $_POST['finalpoli1'],
':flat1' => $_POST['final_lat'],
':flng1' => $_POST['final_lng'],
':dD1' => $_POST['depDate1'],
':dT1' => $_POST['depTime1'],
':ex1' => $_POST['specialservices1'],
':c1' => $_POST['comments1'],
':w1_old' => $_POST['weight_old'],
':c1_old' => $_POST['comments2_old']
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
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);
?>
但有时$ _POST ['xxxxxx']变量为空或null。如何只更新相应变量不为null或为空的列;提前致谢
答案 0 :(得分:1)
我的解决方案如下:
<?
$query = "UPDATE customer SET ";
if(isset($_POST['var1'])){
$stmt->bindParam(":v", $_POST['var1']);
$query .= "var1 = :v";
}
?>