php 使用 PDO 插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10424377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting into database using a PDO
提问by DommyCastles
I'm trying to insert data into a database that I have on my server.
我正在尝试将数据插入到我服务器上的数据库中。
To connect to my database I have the following code:
要连接到我的数据库,我有以下代码:
<?php
$host = "/homes/49/jc192699/public_html/dbase/";
$database = "EduPro.db";
$dbhandle = new PDO("sqlite:".$host.$database);
try {
$dbhandle = new PDO("sqlite:".$host.$database);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
and this all works fine! I have included this into my code so that is not the issue.
这一切正常!我已将其包含在我的代码中,因此这不是问题。
Here is my code for inserting data into the database:
这是我将数据插入数据库的代码:
<?php
$sql = "INSERT INTO USERS (`userName`, `password`) VALUES (`test, `testy`)";
if ( $fetched = $dbhandle->query($sql)){
$fetched->execute(PDO::FETCH_BOTH);
echo "Success!";
}
else {
echo "Fail.";
}
?>
It is not inserting the data into the database for some reason. I know that the SQL statement is 100% correct as I have tested it elsewhere.
由于某种原因,它没有将数据插入数据库。我知道 SQL 语句是 100% 正确的,因为我已经在其他地方对其进行了测试。
Can anyone help me?
谁能帮我?
回答by Ibrahim Azhar Armar
you are opening two database connection. change your code to
您正在打开两个数据库连接。将您的代码更改为
$host = "/homes/49/jc192699/public_html/dbase/";
$database = "EduPro.db";
try {
$conn = new PDO("sqlite:".$host.$database);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
you should not use backquote instead using single quote in your query. and also column should not have any quotes.
您不应在查询中使用反引号而是使用单引号。并且列也不应该有任何引号。
try {
$sql = "INSERT INTO USERS (userName, password) VALUES ('test', 'testy')";
$sth = $conn->query($sql);
} catch(PDOException $e) {
echo $e->getMessage();
}
to fetch the records you need to use this
获取您需要使用的记录
try {
$sth = $conn->query('SELECT * FROM USERS');
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
//$rows contains the fetched records in an associative array
var_dump($rows);
} catch(PDOException $e) {
echo $e->getMessage();
}
learn more about PDO here http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
在此处了解有关 PDO 的更多信息http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
回答by Mike Purcell
Why are you back-ticking the values? Try just single quotes:
你为什么要回勾这些值?尝试单引号:
$sql = "INSERT INTO USERS (userName, password) VALUES ('test', 'testy')";
Also, check your PHP error log, it may give you some insight.
另外,请检查您的 PHP 错误日志,它可能会给您一些见解。
And according to the docs, I believe you have to use exec()vs query(), for doing non-select queries such as inserting, updating, and deleting. If you plan on passing in variables to the $sqlvariable, you should also read up on prepare.
根据文档,我相信您必须使用exec()vsquery()来执行非选择查询,例如插入、更新和删除。如果您计划将变量传递给$sql变量,您还应该阅读prepare。
回答by Anas Siddiqui
Here are some syntax rules to follow:
以下是一些要遵循的语法规则:
The SQL query must be quoted in PHP String values inside the SQL query must be quoted Numeric values must not be quoted The word NULL must not be quoted
SQL 查询必须在 PHP 中被引用 SQL 查询中的字符串值必须被引用 数字值不能被引用 单词 NULL 不能被引用
If(isset($_POST['submit'])) {
$stmt = $db->prepare("INSERT INTO usersdata (Firstname, Lastname,
Email,Passward)
VALUES (:name, :lstname, :email,:pass)");
// inserting a record
then Bind the Values with your variables it will work
然后将值与您的变量绑定它会起作用

