sql update query didn't execute but return no

2019-09-09 21:22发布

问题:

$id = 1; //temp

$promoTitle = trim($_POST['promoTitle']);
$imageURL = trim($_POST['imageURL']);
$affLink = trim($_POST['affLink']);
$couponCode = trim($_POST['couponCode']);

$stmt = $db->prepare('UPDATE dashboard SET promoTitle=?, image=?, url=?, couponCode=? WHERE id=?');

$q = $stmt->bind_param('ssssi', $promoTitle, $imageURL, $affLink, $couponCode, $id);

if($result = $db->query($q)){
    echo "updated";
}else{
    echo mysql_errno();
}

stuck for half an hour and still couldn't find out what's wrong. I has return no error. My table look like this http://i.imgur.com/snwloav.png

回答1:

you can use this for updation in mysql from php

if(isset($_POST['submit'])){ try{

         $id = 1; //temp

        $promoTitle = trim($_POST['promoTitle']);
        $imageURL = trim($_POST['imageURL']);
        $affLink = trim($_POST['affLink']);
        $couponCode = trim($_POST['couponCode']);

        $stmt = $db->prepare('UPDATE  dashboard  SET promoTitle = :promoTitle, image = :image, url  = :url, couponCode=:couponCode WHERE id =:id');
        $stmt->execute(array(
          ':promoTitle' => $promoTitle,
          ':image' => $image,
          ':url' => $url,
          ':couponCode' => $couponCode,             
          ':id' =>$id                        
        ));

    //redirect to Your page page
   header('Location: page.php?action=updated');
   exit;
  }

  catch(PDOException $e) {
      echo $e->getMessage();
  }

}


回答2:

$sql = "UPDATE Applicant SET phone_number=?, street_name=?, city=?, county=?, zip_code=?, day_date=?, month_date=?, year_date=? WHERE account_id=?";

$stmt = $db_usag->prepare($sql);

$stmt->bind_param('sssssdddd', $phone_number, $street_name, $city, $county, $zip_code,     $day_date, $month_date, $year_date, $account_id);
$stmt->execute();

if ($stmt->errno) {
  echo "FAILURE!!! " . $stmt->error;
}
else echo "Updated {$stmt->affected_rows} rows";

$stmt->close();

php mysql bind-param, how to prepare statement for update query



标签: mysql sql mysqli