【问题标题】:PHP - Getting a member function error [closed]PHP - 获取成员函数错误[关闭]
【发布时间】: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


【解决方案1】:

应该是这样的:

//$con->mysql_query($sql,TRUE); //execute the update query
$result= mysql_query("SELECT * FROM article",$con); // get new updated table data

使用mysql_query(),mysql_query需要第一个参数作为你的数据库连接

阅读http://php.net/manual/en/function.mysql-query.php

此外,不推荐使用 mysql_connect,使用 mysqli 或 PDO,您应该准备并清理您的输入。

见:mysql_query

【讨论】:

  • 不要使用mysql...使用mysqli
  • @Afif 没有任何反应仍然是同样的错误
  • @SSingh 没有任何事情发生仍然是同样的错误
  • 你需要通过连接变量...查看更新版本
猜你喜欢
  • 1970-01-01
  • 2017-07-13
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-05
相关资源
最近更新 更多