简体   繁体   中英

i am getting error while inserting data into mysql using php(pdo)

<?php
      $stmt = $db->prepare("INSERT INTO slider(ZSLIDE_SLIDER_NO, ZSLIDE_TITLE, ZSLIDE_IMG, ZSLIDE_IMG_ALT, ZSLIDE_LINK, ZSLIDE_LINK_TARGET,ZSLIDE_COUNTRY_ID, ZSLIDE_STATUS) VALUES(:ZSLIDE_SLIDER_NO, :ZSLIDE_TITLE, :ZSLIDE_IMG, :ZSLIDE_IMG_ALT, :ZSLIDE_LINK, :ZSLIDE_LINK_TARGET,:ZSLIDE_COUNTRY_ID :ZSLIDE_STATUS)");

     $is_success = $stmt->execute(array(":ZSLIDE_SLIDER_NO" => $slider_no, ":ZSLIDE_TITLE" => $title, ":ZSLIDE_IMG" => $thumbimg_filename, ":ZSLIDE_IMG_ALT" => $alt, ":ZSLIDE_LINK" => $link, ":ZSLIDE_LINK_TARGET" => $link_target, ":ZSLIDE_COUNTRY_ID"=>$country_id, ":ZSLIDE_STATUS" => $activate_status));

        // print_r($is_success);exit;
        if($is_success)
        {
            echo "<script>alert('Added Successfully.');</script>";
        }
        else
        {
            echo "<script>alert('Failed to add.');</script>";
        }

Please correctly bind values to your parameters in the query. Like this :

$stmt->bindParam(':ZSLIDE_SLIDER_NO', $slider_no, PDO::PARAM_INT);

You were trying to bind the parameters while executing the query. This is not possible, so you have to bind the parameters before executing the query.

Also, As Alon Eitan correctly pointed out, you forgot to include a comma between your last two placeholders in your statement :ZSLIDE_COUNTRY_ID :ZSLIDE_STATUS

You missed comma here

:ZSLIDE_COUNTRY_ID :ZSLIDE_STATUS

correct:

:ZSLIDE_COUNTRY_ID,:ZSLIDE_STATUS

Possible errors:

  • check correct columns in DB for table slider
  • Check last error of SQL:
 else { echo "< script>alert('Failed to add: " . $db->errorInfor() . ");</ script>"; } 

problem

In binding, you have missin , :

:ZSLIDE_COUNTRY_ID :ZSLIDE_STATUS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM