php显示记录未显示

时间:2017-03-30 13:19:19

标签: php

我正在尝试使用mysqli在php中显示记录。这是我的php显示记录的代码,它没有给出任何错误,但也没有显示任何记录。插入操作正在工作,但显示不工作这里缺少什么???

    <!DOCTYPE html>
<html>
<body>

<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

   movie_name<input type="text" name="movie_name"><br>
   movie_embedded_id<input type="text" name="movie_embedded_id"><br>
   rating_no<input type="text" name="rating_no"><br>
   movie_description<input type="text" name="movie_description"><br>
   <input type="submit" name="submit" value="Submit"> 

</form>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname="mydb";

// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

        if(isset($_POST['movie_name']) && $_POST['movie_name'] != "")
            {   
                $sql = "INSERT INTO movies (movie_name, movie_embedded_id, rating_no, movie_description)VALUES ('".$_POST['movie_name']."', '".$_POST['movie_embedded_id']."', '".$_POST['rating_no']."','".$_POST['movie_description']."')";


            if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
            } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
            }

        }

$conn->close();


?>


<?php

      if(isset($_POST['movie_name']) && $_POST['movie_name'] != "")
            {
$sql = "select * from movies where movie_id = ".$_GET["id"]."";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<table><tr><th>ID</th><th>movie_name</th><th>movie_embedded_id</th><th>rating_no</th><th>movie_description</th></tr>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<tr><td>" . $row["id"]. "</td><td>" . $row["movie_name"]. " </td><td>" . $row["movie_embedded_id"]. " </td><td>" . $row["rating_no"]. "</td><td> " . $row["rating_no"]. "</td></tr>";
     }
     echo "</table>";
} else {
     echo "0 results";
}

    }


  ?>


</body>
</html>

3 个答案:

答案 0 :(得分:0)

让我们试试这个

它会起作用

 <?php

        if(isset($_POST['movie_name']) && $_POST['movie_name'] != "")
            {
$sql = "select * from movies where movie_id = ".$_GET['id'];
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     echo "<table><tr><th>ID</th><th>movie_name</th><th>movie_embedded_id</th><th>rating_no</th><th>movie_description</th></tr>";
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<tr><td>" . $row["id"]. "</td><td>" . $row["movie_name"]. " </td><td>" . $row["movie_embedded_id"]. " </td><td>" . $row["rating_no"]. "</td><td> " . $row["rating_no"]. "</td></tr>";
     }
     echo "</table>";
} else {
     echo "0 results";
}

    }

答案 1 :(得分:0)

<?php

$movie = $_POST['movie_name'];
$id = $_GET['id'];

/* I wonder why POST and GET method in the same form : where do $vars come from ? */

echo"[ id is ($id) / movie is ($movie) ]";

/* then if $id isn't empty it should do the trick */

$sql = " SELECT * FROM movies WHERE movie_id=$id ";

?>

答案 2 :(得分:0)

我非常感谢大家的回应。在这里我已经找到了我的问题。只有在我添加id列时才会出现问题,添加额外的空列,不知道为什么。请仔细阅读此代码以获得更安全和优化。

    <div class="container">
<form class="form-horizontal" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  <div class="form-group">
    <label class="control-label col-sm-2" for="movie_name">movie_name:</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="movie_name" name="movie_name" placeholder="Enter movie name">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-2" for="movie_embedded_id">movie_embedded_id:</label>
    <div class="col-sm-10"> 
      <input type="text" class="form-control" id="movie_embedded_id" name="movie_embedded_id" placeholder="Enter embeded id">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-2" for="rating_no">rating_no:</label>
    <div class="col-sm-10"> 
      <input type="text" class="form-control" id="rating_no" name="rating_no" placeholder="Enter rating_no">
    </div>
  </div>
    <div class="form-group">
    <label class="control-label col-sm-2" for="movie_description">rating_no:</label>
    <div class="col-sm-10"> 
      <input type="text" class="form-control" id="movie_description" name="movie_description" placeholder="Enter movie_description">
    </div>
  </div>
  <div class="form-group"> 
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" name="submit" class="btn btn-default" >submit</button>
    </div>
  </div>
</form>
</div>
  <table class="table table-bordered">
    <thead>
      <tr>
<!--       <th>ID.<th> -->
       <th>movie_name</th>
       <th>movie_embedded_id</th>
       <th>rating_no</th>
       <th>movie_description</th>
       <th>Update</th>
      </tr>
    </thead>
        <tbody>
    <?php  
    include "db.php";
    include "movieinsert.php";

      $result ="select * from movies";
      $get=mysqli_query($conn, $result);

    while ($data = mysqli_fetch_array($get))
        {
    ?>
          <tr>
    <!--      <td><?php //echo "$data[0]"; ?><td> -->
          <td><?php echo "$data[1]"; ?></td>
          <td><?php echo "$data[2]"; ?></td>
          <td><?php echo "$data[3]"; ?></td>
          <td><?php echo "$data[4]"; ?></td>
          <td>
          <a href='mvieupdate.php?id=$data[0]' class="btn btn-info" role="button">Update</a>
          </td>
          </tr>
    <?php 
         }
     ?>
  <tbody>
   </table>