-4

コードでエラーが発生します。間違ったユーザーを入力すると、表示されます

ユーザーOK

正しいユーザーを入力すると、それも表示されます

ユーザーOK

コードのどこにエラーがあるのか​​わからないので、コードを見て、どこが間違っているのか教えてください。

ログイン.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();
     ?>
4

2 に答える 2

3

返された行数を確認する必要があります: 変更:

if($result ){

に:

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

PS: 失敗した場合 (SQL ステートメントに問題がある場合) に$result = $dbLink->query($query); 戻ります。FALSEそれ以外は、 ステートメントと同じオブジェクトを返しtrue ます。if

于 2013-08-28T21:41:07.233 に答える