猿问

将数组插入PDO(PHP):“ SQLSTATE [HY093]:无效的参数号”

通过PDO将数组插入带有PHP的MySQL数据库中对我不起作用。即使数组有41个值,它也会显示“无效的参数编号”;PDO命名为41列;并且准备好的查询有41个问号。


数组具有41个值(我用var_dump检查),如下所示:


$data = [

    'countrycode' => $_GET["id"],

    'country' => $_GET["country"],

    'region' => $_GET["region"],

    'law_db_region' => $_GET["law_db_region"],

    [... etc ...]

 ]

我的PDO代码如下(是的,这些是41列和41个问号):


$sql = "INSERT INTO awards_country

    (countrycode, country, region, law_db_region, law_db_national,

    law_db_national2, const_url, const_version, const_date, const_dummy,

    const_article, const_wording, const_article2, const_wording2, const_article3,

    const_wording3, law_dummy, law_act, law_article, law_source,

    law_date, law_act2, law_article2, law_source2, law_date2,

    law_act3, law_article3, law_source3, law_date3, web_dummy,

    web_url, web_organ, web_language, web_date, web_url2,

    web_organ2, web_language2, web_date2, wikipedia_url, wikipedia_date,

    comment)


    VALUES (

    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,

    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,

    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,

    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?

    )";

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

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

$stmt->execute($data);

不过,我得到的错误是:


致命错误:未捕获的PDOException:SQLSTATE [HY093]:无效的参数编号:在国家/地区插入代码.php:99中未定义参数


堆栈跟踪:#0 [...] country-insert.php(99):PDOStatement-> execute(Array)#1 {main}放在/data/web/e96483/html/awards/country-insert.php中99行


互换的青春
浏览 174回答 2
2回答

哔哔one

为了 ?参数,您不应使用关联数组,而应将值作为位置传递$data = [    $_GET["id"],    $_GET["country"],    $_GET["region"],     $_GET["law_db_region"],    [... etc ...] ]
随时随地看视频慕课网APP
我要回答