【问题标题】:I'm trying to insert data into the databse, but I can't do it我正在尝试将数据插入数据库,但我做不到
【发布时间】:2022-01-15 23:46:35
【问题描述】:

我的网站出现错误,我重读了 30 次我的文字,但找不到错误,请您帮帮我吗?

-- 错误:--

警告:PDOStatement::execute(): SQLSTATE[HY093]: 无效参数编号:绑定变量的数量与第 9 行的 D:\Xamp\htdocs\monoShop\config\commandes.php 中的标记数量不匹配

-- 代码:--

<?php

 function ajouter($image, $nom, $prix, $desc)
{
   if(require("connexion.php"))
   {
     $req = $access->prepare("INSERT INTO produits (image, nom, prix, description) VALUES ('$image', '$nom', $prix, '$desc')");

     $req->execute(array($image, $nom, $prix, $desc));

     $req->closeCursor();
   }
}

function afficher()
{
    if(require("connexion.php"))
    {
        $req=$access->prepare("SELECT * FROM produits ORDER BY id DESC");

        $req->execute();

        $data = $req->fetchAll(PDO::FETCH_OBJ);

        return $data;

        $req->closeCursor();
    }
}

function supprimer($id)
{
    if(require("connexion.php"))
    {
        $req=$access->prepare("DELETE FROM produits WHERE id=?");

        $req->execute(array($id));

        $req->closeCursor();
    }
}

?>

【问题讨论】:

  • 您尝试在execute() 中绑定四个值,但您查询的标记为零。
  • 试试:"INSERT INTO produits (image, nom, prix, description) VALUES (?, ?, ?, ?)"
  • 谢谢它的工作,我爱你

标签: php pdo prepared-statement


【解决方案1】:
$req = $access->prepare("INSERT INTO produits (image, nom, prix, description) VALUES (:image', :nom', :prix, :desc')")
$req->bindValue("image", $image, PDO::PARAM_STR);
$req->bindValue("nom", $nom, PDO::PARAM_STR);
$req->bindValue("prix", $prix, PDO::PARAM_STR); // ou tu met PDO::PARAM_INT si c'est un nombre
$req->bindValue("desc", $desc, PDO::PARAM_STR);
$req->execute();

这使得使用 bindValue 更容易添加值,它将您输入的单词替换为变量。它也增加了安全性

【讨论】:

    猜你喜欢
    • 2020-08-06
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 2019-09-20
    相关资源
    最近更新 更多