我使用php.net中的以下示例:
<?php
// configuration
$dbuser = "userone";
$dbpass = "useronepass";
// database connection
$dbh = new PDO("mysql:host=localhost;dbname=testone",$dbuser,$dbpass);
$stmt = $dbh->prepare("INSERT INTO myTable (one, two) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);
// insert one row
$name = 'one';
$value = 'two';
$stmt->execute();
?>
我已经验证了所有凭据,并且相同的连接在其他文件中有效,但我没有在字段中插入任何值,顺便说一下,这些是VARCHAR数据类型。
我错过了什么?
谢谢!
答案 0 :(得分:1)
在定义的参数值(bindParams()
和$name
)之前,您已按$value
绑定所有参数。
尝试这样:
$stmt = $dbh->prepare("INSERT INTO myTable (one, two) VALUES (:name, :value)");
// insert one row
$name = 'one';
$value = 'two';
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);
$stmt->execute();
答案 1 :(得分:1)
$name
和$value
<?php
// configuration
$dbuser = "userone";
$dbpass = "useronepass";
// database connection
$dbh = new PDO("mysql:host=localhost;dbname=testone",$dbuser,$dbpass);
$stmt = $dbh->prepare("INSERT INTO myTable (one, two) VALUES (:name, :value)");
// add
$name = 'one';
$value = 'two';
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);
/* remove
$name = 'one';
$value = 'two'; */
$stmt->execute();
?>