如何在php中添加复选框以从数据库中检索数据

时间:2017-03-08 18:49:19

标签: php html

我创建了一个数据库。在那个数据库中,我创建了一个表。然后我写下面的代码来从创建的表中检索数据。我检索了表中存在的所有元素。现在我想为检索到的列的所有行添加复选框。任何人都可以帮助我,并在下面的代码中建议更改。

<?php
    // Check connection
    if ($link === false) {
        die("ERROR: Could not connect. " . mysqli_connect_error());
    }

    // Attempt select query execution
    $sql = "SELECT * FROM fruits";
    if ($result = mysqli_query($link, $sql)) {
        if (mysqli_num_rows($result) > 0) {
           echo "<table>";
           echo "<tr>";
           echo "<th>PID</th>";
           echo "<th>Fruit Name</th>";
           echo "<th>Quantity(Kgs)</th>";
           echo "<th>Price</th>";
           echo "</tr>";
           while ($row = mysqli_fetch_array($result)) {
                echo "<tr>";
                echo "<td>" . $row['PID'] . "</td>";
                echo "<td>" . $row['Fruit Name'] . "</td>";
                echo "<td>" . $row['Quantity(Kgs)'] . "</td>";
                echo "<td>" . $row['Price'] . "</td>";
                echo "<td>" . $row["submitted"] . "<td><input type ='checkbox' value = '" . $row['PID '] . "'PID = '" . $row['PID '] . "'> </td></td>";
                echo "</tr>";
            }
            echo "</table>";
            // Free result set
            mysqli_free_result($result);
        } else {
            echo "No records matching your query were found.";
        }
    } else {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }

    // Close connection
    mysqli_close($link);
?>

2 个答案:

答案 0 :(得分:0)

检查此代码我认为它会起作用

<?php 
// Check connection
 if($link === false){
 die("ERROR: Could not connect. " . mysqli_connect_error());
  }

 // Attempt select query execution
 $sql = "SELECT * FROM fruits";
 if($result = mysqli_query($link, $sql)){
 if(mysqli_num_rows($result) > 0){ ?>
<table>
    <tr>
        <th>PID</th>
        <th>Fruit Name</th>
        <th>Quantity(Kgs)</th>
        <th>Price</th>
        <th>&nbsp;</th>
    </tr>
    <?php 
while($row = mysqli_fetch_array($result)){ ?>
    <tr>
        <td><?=$row['PID']?></td>
       <td><?=$row['Fruit Name']?></td>
        <td><?=$row['Quantity(Kgs)'] ?></td>
        <td><?=$row['Price']?></td>
        <td><?=$row["submitted"]?><input type ='checkbox'            
      value="<?=$row['PID']?>" PID="<?=$row['PID']?>"></td>
      </tr>
   <?php } ?>
</table>
<?php // Free result set
mysqli_free_result($result);
} else{
 echo "No records matching your query were found.";
 }
 } else{
  echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
 }

 // Close connection
 mysqli_close($link);
 ?>

答案 1 :(得分:0)

我在代码中修改了while循环。试试这个。

 while($row = mysqli_fetch_array($result)){

     echo "<tr>";
        echo "<td>" . $row['PID'] . "</td>";
        echo "<td>" . $row['Fruit Name'] . "</td>";
        echo "<td>" . $row['Quantity(Kgs)'] . "</td>";
        echo "<td>" . $row['Price'] . "</td>";
        echo "<td>" .$row["submitted"]. "<input type ='checkbox' value=" .$row["PID"]. "></td>";
    echo "</tr>";

 }