【问题标题】:Call to a member function query() on a non-object in profile.php on line 82在第 82 行对 profile.php 中的非对象调用成员函数 query()
【发布时间】:2015-01-27 16:19:19
【问题描述】:

到目前为止,我正在努力回应用户的问题,我可以插入值,这可以通过 php 管理员工作,但不能通过用户个人资料页面上的表单工作?

错误说,“在第82行的profile.php中对一个非对象调用成员函数query()”该行的php代码如下

   $query1=$db->query("SELECT id,title,question,username FROM  QnA WHERE username='$dbusername'");

profile.php 的其余部分在下面

   <?php 
     session_start();
     $dusername=$_GET['username'];
    if (isset($dusername)){
        require('connect.php');

        $userquery =$db->query("SELECT id,firstname,username,lastname,email FROM users WHERE username = '".$dusername."'");
                while ($row =$userquery->fetch()){
                        $id=$row["id"];
                        $dbusername =$row["username"];
                        $dfirstname = $row["firstname"];
                        $demail =$row["email"];
                        $dlastname =$row["lastname"];

                        }

    }

 ?>
  <html>
   <head><title><?php  echo $dfirstname;?></title>
    <link rel="stylesheet" href="stylesheets/profile.css" type="text/css">
 </head>
 <body>
    <div id="container">

<?php
echo'
 <div id="qform"><center>
  <form action="ask.php" method="post">
   <b>Title</b>
   <br/>
   <input type ="text" name="title"/>
   <br/>
   <b>Question</b>
   <br/>
   <textarea name="question"></textarea>
   <br/>
   <b>This is to make sure your not a robot  2+2=</b>
   <input type="text" name="plus"/>
   <br/>
   <input type="submit" value="Submit"name="submit"/> 
   </form></center>
   </div>
    ';
?>  
<div id="questions">
 <?php 

   $query1=$db->query("SELECT id,title,question,username FROM  QnA WHERE username='$dbusername'");
  //$query2=$db->query("SELECT * FROM answers");

   while($asked=$query1->fetch()){
    if($asked['username']==$dbusername){
     echo '<div class="asked"><b>Title</b><br/><b> ',$asked['title'],'</b>   <hr/><br/><b>Question</b><br/><b>',$asked['question'],'</b></div><br/><br/>';

   }
     else if(!$asked['username']==$dbusername){
          error_reporting(E_ALL);
    echo 'No questions have been asked';
        }

 }
     ?>
 </div>
 </div>
 </center>
 </body>
</html>

表单在 profile.php 中,表单的操作在一个名为 ask.php 的单独文件中。

   <?php

   //form action below
   require('profile.php');
   session_start();
   require('connect.php');
   $plus=$_POST['plus'];
   $title=$_POST['title'];
   $question=$_POST['question'];
   $dusername=$_SESSION['username'];
         if(isset($_POST['submit'])){
          if(!empty($_POST['title']) && !empty($_POST['question'])&& $plus==4){
              $query="INSERT INTO `QnA` (id,title,question,username) VALUES (?,?,?,?)";
              $query=$db->prepare($query);
              $query->execute(array(' ',$title,$question,$dusername));
                echo'succes';
                header("Location: profile.php");
            }
         else{
             error_reporting(E_ALL);
             echo " Fill in all Slots or you gave the wrong answer to the security question";
            }
         }

?>

【问题讨论】:

    标签: php pdo login forms


    【解决方案1】:

    下面的代码根本没有返回 True:

    if(isset($_POST['username'])){
    

    这就是它在 else 语句中的原因。

    您正在使用 isset 函数作为用户名字段,但您应该检查提交按钮。尝试替换以下行:

    <input type="submit" value="Log In" />
    

    <input type="submit" value="Log In" name="submitbtn" />
    

    if(isset($_POST['username'])){
    

    if(isset($_POST['submitbtn'])){
    

    那么它应该可以工作了:)

    【讨论】:

    • 非常感谢,这是问题所在..上帝保佑
    【解决方案2】:

    您查询在ANDpassword=' 之间添加空格时出错,也许这应该可行

    $query="SELECT  * FROM  users WHERE username='".$username."' AND password='".$password."' LIMIT 1" ;
    

    调试你的错误尝试

    $res =  mysql_query($query) or die(mysql_error());
    

    请注意,此查询在 SQL 注入中易受攻击,出于安全目的,也许您应该尝试使用 mysqli 或 PDO。

    How can I prevent SQL injection in PHP?

    【讨论】:

    • 感谢关于 SQL 注入保护的指针,我用空格尝试了你所说的,但输出仍然是那个 Else 语句。 @RxV
    • 您是否尝试使用$res = mysql_query($query) or die(mysql_error()); 来调试您的错误
    • 我认为您的查询没有返回任何返回您的查询中有很多空格。尝试上面给出的查询。
    • 尝试回显mysql_num_rows($res) 会返回多少?
    • 它在回显 0 吗? @RxV
    【解决方案3】:

    试试这个:

    <form action="login.php" method="post" >
    <table>
     <tr>
       <td>Username: </td><td><input type="text" name="username" /></td>
     </tr>
     <tr>
        <td>Password: </td><td><input type="password" name="password" id ="password"/></td>
     </tr>
     </table>
       <input type="submit" value="Log In" /> &nbsp;  &nbsp; &nbsp; &nbsp;
       <input type="button" value="Register" onClick="location.href='register.php'" />
    </form>
    
    </body>
    </html>
    <?php
    
    
    require('connect.php');
    session_start();
     if ($_SESSION['username'])
    {
    header("Location: home.php");
     }
    else{
    if(isset($_POST['username'])){
     require('connect.php');
    
    
     //According to user's input
    
      $username=$_POST['username'];
      $password=$_POST['password'];
      $query="SELECT  * FROM  users WHERE username='".$username."'AND password='".$password."'";
    
     $res =  mysql_query($query);
    
     //check username and password for match
     if (mysql_num_rows($res) > 0){
    
     //Sets username to the comment session so no username has to be input
    
    
    $_SESSION['username']=$username;
    
    // jumps to secure page 
    
     header ("Location: home.php");
    
    }
    

    【讨论】:

      【解决方案4】:

      如上所述,您正在获取0 行值,您查询中可能有空格或其他内容,请在下面执行。

      回显这个查询

      SELECT  * FROM  users WHERE username='".$username."' AND password='".$password."'
      

      并在数据库中手动执行这个查询,看看这个查询发生了什么。并相应地更正您的查询。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-12
        • 2015-02-15
        • 2014-02-10
        • 2014-01-15
        • 1970-01-01
        • 2017-12-09
        相关资源
        最近更新 更多