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

时间:2017-03-25 23:27:55

标签: php mysql pdo insert

如何通过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();

1 个答案:

答案 0 :(得分:0)

您必须使用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();