PHP mysql 运行 INSERT 时出错
PHP mysql error when running INSERT
谁能帮我理解这个错误?
Fatal error: Call to a member function bind_param() on a non-object in C:\MAMP\htdocs\MyCMS\insert_posttwo.php on line 64
<?php
$mysqli = mysqli_connect("localhost", "root", "root", "mycms");
if (isset($_POST['submit'] )) {
$post_author = $_POST['post_author'];
$stmt = $mysqli->prepare ("INSERT INTO 'posts' ('post_author') VALUES(?)");
$stmt->bind_param('s', $post_auth);
$post_auth = $post_author;
$stmt->execute();
echo "<script>alert('Post has been published')</script>";
echo "<script>window.open('insert_post','_self')</script>";
$stmt->close();
}
?>
不要使用单引号 '
,而是使用反引号 `
来转义字段或 table 名称。
$stmt = $mysqli->prepare ("INSERT INTO `posts` (`post_author`) VALUES(?)");
对此进行更改(对于列,您必须使用反引号而不是单引号):
'posts'
至:
`posts`
另外你必须创建一个对象而不是程序方法否则你不能这样做所以使用这个:
$mysqli = new mysqli_connect("localhost", "root", "root", "mycms");
//^^^ See here so you create a object
而且你还必须像这样关闭你的连接:
$mysqli->close();
//^^^^^ Close the connection and not the stmt
谁能帮我理解这个错误?
Fatal error: Call to a member function bind_param() on a non-object in C:\MAMP\htdocs\MyCMS\insert_posttwo.php on line 64
<?php
$mysqli = mysqli_connect("localhost", "root", "root", "mycms");
if (isset($_POST['submit'] )) {
$post_author = $_POST['post_author'];
$stmt = $mysqli->prepare ("INSERT INTO 'posts' ('post_author') VALUES(?)");
$stmt->bind_param('s', $post_auth);
$post_auth = $post_author;
$stmt->execute();
echo "<script>alert('Post has been published')</script>";
echo "<script>window.open('insert_post','_self')</script>";
$stmt->close();
}
?>
不要使用单引号 '
,而是使用反引号 `
来转义字段或 table 名称。
$stmt = $mysqli->prepare ("INSERT INTO `posts` (`post_author`) VALUES(?)");
对此进行更改(对于列,您必须使用反引号而不是单引号):
'posts'
至:
`posts`
另外你必须创建一个对象而不是程序方法否则你不能这样做所以使用这个:
$mysqli = new mysqli_connect("localhost", "root", "root", "mycms");
//^^^ See here so you create a object
而且你还必须像这样关闭你的连接:
$mysqli->close();
//^^^^^ Close the connection and not the stmt