gpt4 book ai didi

php - 如何通过从表单获取变量来插入 pdo?

转载 作者:行者123 更新时间:2023-11-29 19:11:50 25 4
gpt4 key购买 nike

如何通过 PDO 从表单中插入内容,它给出以下错误:

Fatal error: Call to a member function bindParam() on string  

这是我的代码:

$name = $_POST['name'];
$name = $_POST['subname'];
$phone= $_POST['phone'];

$sql = "INSERT INTO customers(name,subname,phone)VALUES(:name,:subname,:phone)";
$sql->bindParam(':name', $name);
$sql->bindParam(':subname', $subname);
$sql->bindParam(':phone', $phone);
$sql->bindParam(':telefono', $telefono);
$sql->execute();

最佳答案

您必须使用 PDO 语句,并首先准备查询:

$name = $_POST['name'];
$name = $_POST['subname'];
$phone= $_POST['phone'];

$sql = "INSERT INTO customers(name,subname,phone)VALUES(:name,:subname,:phone)";
$sth = $dbh->prepare($sql);
$sth->bindParam(':name', $name);
$sth->bindParam(':subname', $subname);
$sth->bindParam(':phone', $phone);
$sth->bindParam(':telefono', $telefono);
$sth->execute();

关于php - 如何通过从表单获取变量来插入 pdo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43023137/

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