当我在那里使用if else时,获取mysql的错误

时间:2016-04-14 12:51:16

标签: php mysql pdo

当我在那里使用if else时,

获取mysql的错误。我不知道我该怎么办,当我使用重复条件更新时,它不会担心我无法找到错误在哪里

  

这是我得到的错误。

     

错误:SQLSTATE [HY093]:参数号无效:参数不是   定义

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt=$conn->prepare("SELECT uniqueid FROM hotelcarttemp WHERE uniqueid=:uniqueid");
$stmt->execute(array(':uniqueid'=>$uniqueid));
$count=$stmt1->rowCount();
echo "count-".$count;
if($count>0)
   {
    $sql = "UPDATE hotelcarttemp SET `hotelname`='".$hotelname."',`roomtype`='".$roomtype."',`checkin`='".$checkin."',`checkout`='".$checkout."',`Country`='".$Country."',`Destination`='".$Destination."',`price`='".$price."' WHERE uniqueid='".$uniqueid."'";
    echo "sql- ".print_r($sql);
        $stmt = $conn->prepare($sql);
        // echo print_r($stmt);
        $stmt->execute();
   }
   else
   {
       $sql = "INSERT INTO hotelcarttemp (timestamp, packageid, uniqueid, hotelname, roomtype, checkin, checkout, Country, Destination, hoteldetail, price)
        VALUES ('"  
        .$timestamp."','"
        .$packageid."','"
        .$uniqueid."','"
        .$hotelname."','"
        .$roomtype."','"
        .$checkin."','"
        .$checkout."','"
        .$Country."','"
        .$Destination."','"
        .addslashes($hoteldetail)."','"
        .$price."'
        )"; 
        // echo "sql- ".print_r($sql);
        $stmt = $conn->prepare($sql);
        // echo print_r($stmt);
        $stmt->execute();
   }
}
catch(PDOException $e) {
  echo 'ERROR:' . $e->getMessage();
} here

1 个答案:

答案 0 :(得分:1)

您的SELECT查询条件为WHERE uniqueid=:uniqueid

你正在约束username

$stmt->execute(array(':username'=>$uniqueid));//:username invalid parameter

将此更改为

$stmt->execute(array(':uniqueid'=>$uniqueid));
相关问题