简体   繁体   中英

PDO insert query not inserting data

I am trying to insert some data using PDO like below

$sql = "INSERT INTO tbl_category SET `category_title` = :cat_name  , `category_alias` = :category_alias , `category_status`= :cat_status, `category_parent_id` = $parent_id, "
            . "category_description =   '$cat_description'";
    $statement = $this->db->conn_id->prepare($sql);

    $statement->bindParam(':cat_name', $cat_name, PDO::PARAM_STR);
    $statement->bindParam(':cat_status', $cat_status, PDO::PARAM_STR);
    $statement->bindParam(':category_alias', $category_alias, PDO::PARAM_STR);
    $statement->bindParam(':parent_id', $parent_id, PDO::PARAM_INT);


if ($statement->execute()) {
        echo "executed"; exit;
        return $this->db->conn_id->lastInsertId();
    } else {
        echo "not executed"; exit;
    }

it always shows me "Not Executed", but when I run the query manually, it works fine

The problem is that you are mixing bind and strings.

Bind

$parent_id
$cat_description

You have typos my friend. Clean up your code.

   $sql = "INSERT INTO tbl_category SET `category_title` = :cat_name  , `category_alias` =     :category_alias , `category_status`= :cat_status, `category_parent_id` = :parent_id, category_description =   :cat_description";

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