繁体   English   中英

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

[英]How to add checkbox in php to the data retrieved from the database

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

<?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);
?>

检查这段代码,我认为它会起作用

<?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);
 ?>

我在你的代码中修改了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>";

 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM