我正在尝试将一堆数据插入数据库,但出现此错误:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0' at line 1
这是我的两个文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert new Page</title>
</head>
<body>
<form action="insert_page.php" method="get">
New Page Name:<br>
<input type="text" name="pagename">
<input type="submit" value="Insert" >
</form>
</body>
</html>
这是 insert_page.html,它只是一个文本框和一个按钮,用户可以在其中为要输入数据库的新页面选择新名称。
现在,这是按下按钮时调用的 PHP
<?php
$servername = "db.ist.utl.pt";
$username = "ist178349";
$password = "getrekt";
$dbname = "ist178349";
$pagename = $_POST['pagename'];
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO pagina (userid, pagecounter, nome, idseq, ativa)
VALUES (78349, 95002, " + $pagename + ",1151988, true)";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
但是,我收到了这个错误,我无法真正理解。有什么建议么?提前致谢。
最佳答案
您应该修复包含 +
的查询,使用 .
或将其省略并使用单引号,这在 PHP sql 查询字符串中是可以接受的。
$sql = "INSERT INTO pagina (userid, pagecounter, nome, idseq, ativa)
VALUES (78349, 95002, '$pagename', 1151988, true)";
关于php - 使用PHP修改数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34141711/