我的PHP代码中的错误

时间:2013-08-28 21:34:28

标签: php

我的代码出错了。如果我输错了用户,则会显示

  

用户确定

如果我输入正确的用户,它也会显示

  

用户确定

我不知道代码中的错误在哪里,所以请查看我的代码并让我知道我哪里出错了。

的login.php

<html>
  <body>
    <form action="list_work.php" method="post">
      username: <input type="text" name="username">
      password: <input type="text" name="password">
      <input type="submit">
    </form>
  </body>
</html>

List_work.php

<?php
  $username = $_POST["username"];
  $password = $_POST["password"];

  // Connect to the database
  $dbLink = new mysqli('localhost', 'sqldata', 'sqldata', 'balhaf');
  if(mysqli_connect_errno()) {
    die("MySQL connection failed: ". mysqli_connect_error());
  }

  mysqli_select_db($dbLink,"balhaf2");

  // Fetch the file information
  $query = "select *  from users WHERE username = '".$dbLink-  >escape_string($username)."'";

  $result = $dbLink->query($query);
  $company = false;
  if($result) {
  echo "user ok"."</br>";
  //Now get the result information
  $row = $result->fetch_object();  //will store the record in $row

 //Access what you need
  if($row) {
    $company = $row->company;  //variable name should match the field name in your  database
    echo $company; //See if you get the value stored in the database
  }
  } else {
   echo "worng user";
  }

  mysqli_select_db($dbLink,"balhaf");


  // Query for a list of all existing files
  $sql = "SELECT id, name, mime, size, created FROM $company";
  $result = $dbLink->query($sql);
  // Check if it was successfull
  if($result) {
    // Make sure there are some files in there
    if($result->num_rows == 0) {
      echo '<p>There are no files in the database</p>';
    } else {
      // Print the top of a table
      echo '<table border="1" align="center">
          <H2 align="center"> Report Table</H>

            <tr>
                <td><b>Name</b></td>
                <td><b>Mime</b></td>
                <td><b>Size (bytes)</b></td>
                <td><b>Created</b></td>
                <td><b>&nbsp;</b></td>
            </tr>';

    // Print each file
    while($row = $result->fetch_assoc()) {
        echo "
            <tr>
                <td>{$row['name']}</td>
                <td>{$row['mime']}</td>
                <td>{$row['size']}</td>
                <td>{$row['created']}</td>
                <td><a style='text-decoration:none;' href='get_file_work.php?id= {$row['id']}&company=$company'>Download</a></td>
            </tr>";
       }

       // Close table
       echo '</table>';
      }

     // Free the result
     $result->free();
     }
     else
     {
     echo 'Error! SQL query failed:';
     echo "<pre>{$dbLink->error}</pre>";
     }
     // Close the mysql connection
     $dbLink->close();
     ?>

2 个答案:

答案 0 :(得分:3)

您需要检查返回的行数: 改变:

if($result ){

要:

if($result  &&  $result->num_rows) {

P.S:$result = $dbLink->query($query);失败时返回FALSE(当您的SQL语句出现问题时)。除此之外,它将在true语句中返回与if相同的对象。

答案 1 :(得分:1)

更改以下内容:

if($result) {

要:

if( $result && mysqli_num_rows($result) > 0 ) {

$ result仍然是一个对象,因此$ result不是NULLFALSE0

修改

我第一次忽略了自己添加$ result。如果查询失败,则$result == FALSEmysqli_num_rows($result)会发出警告:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

...如果它只是一个mysqli_result对象。因此,首先检查$ result是否为false,我们可以防止错误发生。

注意:撤消的回答第一次是正确的,即使它是在我的之后:p