【发布时间】:2017-02-04 19:25:12
【问题描述】:
我想让这个表单从 url 获取 id 假设: http://localhost/x/y/index.php?id=1 并更新文章表中的数据,但是我收到错误: 致命错误:在第 38 行的 http://localhost/x/y/index.php 布尔值上调用成员函数 query(),这是代码中以 ** 开头和结尾突出显示的行。
<?php
include('db/db.php');
$id = $_GET['id'];
$select_db = "SELECT * FROM article WHERE ID=$id";
$run_news = mysql_query($select_db);
while($row=mysql_fetch_array($run_news)){
$post_id = $row['ID'];
$post_title = $row['title'];
$post_date = $row ['date'];
$post_author = $row['author'];
$post_keywords = $row['keywords'];
$post_image= $row['image'];
$post_content = $row['content'];
}
?>
<form action="" method="post">
<div>
<p><strong>ID:</strong> <?php echo $id; ?> </p>
<strong>Title: *</strong> <input type="text" name="title" value="<?php echo $post_title; ?>"/><br/>
<strong>Author: *</strong> <input type="text" name="author" value="<?php echo $post_author; ?>"/><br/>
<strong>Keywords: *</strong> <input type="text" name="keywords" value="<?php echo $post_keywords; ?>"/><br/>
<strong>image: *</strong> <input type="text" name="image" value="<?php echo $post_image; ?>"/><br/>
<strong>content: *</strong> <input type="text" name="content" value="<?php echo $post_content; ?>"/><br/>
<input type="submit" name="update" value="Submit">
</div>
</form>
<?php
// Code for UPDATE button
if (isset($_POST['update'])) {
$id = $_GET['id'];
$post_title = $_POST['title'];
$post_author = $_POST['author'];
$post_keywords = $_POST['keywords'];
$post_image = $_POST['image'];
$post_content = $_POST['content'];
$sql = "UPDATE article SET title='$post_title',author='$post_author',keywords=$post_keywords,content='$post_content' WHERE ID='$id'";
**$newdb = $con->query($sql,TRUE);**
$q = $newdb->query("SELECT * FROM article");
}
?>
【问题讨论】:
-
$con 在哪里?定义了吗?
-
我想它想说的是你的 $con 函数不是对象,而是一个布尔值(真或假)。你能提供你拿那个 $con 变量的代码吗?
-
你 $con 应该是一个必须有方法 query() 的对象。并且有一个方法 query() $con 必须是数据库连接对象
-
@SSingh $con=mysqli_connect("localhost","root","","namedb"); $con=mysql_connect("localhost","root",""); $con=mysql_select_db("namedb");
-
是的,你使用的是 mysql 而不是 mysqli,所以方法必须是 mysql_query() 而不仅仅是 query()。
标签: php html mysql database mysqli