gpt4 book ai didi

php - 使用mysqli发布到数据库

转载 作者:可可西里 更新时间:2023-11-01 07:17:45 24 4
gpt4 key购买 nike

我正在学习 php 并尝试完成以下工作:

<?php
require_once("db_connect.php");

// TODO - Check that connection was successful.

$dname = $_POST["dname"];
$daddress = $_POST["daddress"];


$stmt = $mysqli->prepare("INSERT INTO test (dname, daddress) VALUES (?, ?)");

// TODO check that $stmt creation succeeded

// "s" means the database expects a string
$stmt->bind_param("s", $dname, $daddress);

$stmt->execute();

$stmt->close();

$mysqli->close();
?>

它仅适用于一个 bind_param 而不是 2。如果从代码中删除了 $daddress 则它会发布。该表单在数据库中有 26 个帖子,我目前正在处理 2 个以使其最少。

提交表单时出现以下错误。

Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of elements in type definition string doesn't match number of bind variables in /home/mymotorsportco/public_html/entry/actions/entry.php on line 15

最佳答案

根据 PHP manual :

types

A string that contains one or more characters which specify the types for the corresponding bind variables

i - corresponding variable has type integer

d - corresponding variable has type double

s - corresponding variable has type string

b - corresponding variable is a blob and will be sent in packets

您必须为要绑定(bind)的所有参数添加类型。所以如果第二个参数是一个字符串,你必须做

$stmt->bind_param("ss", $dname, $daddress);

关于php - 使用mysqli发布到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35235820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com