douqi3064 2019-05-26 03:37
浏览 36
已采纳

如何为每个汽车产品添加“书籍按钮”?

I want to add "book" button to each car product.But it only display only one button for first car only.

$sql = "SELECT * FROM car";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
    echo "<table>";
        echo "<tr>";
            echo "<th>Id</th>";
            echo "<th>Name</th>";
            echo "<th>Price(RM)</th>";
            echo "<th>Colour</th>";
            echo "<th>Mode</th>";
            echo "<th>Image</th>";
            echo "<th>Status</th>";
            echo "<td><button onclick=\"book_car('" . $row['car_id'] . 
            "')\">Book</button></td>";
    while($row = mysqli_fetch_array($result)){
        echo "<tr>";
            echo "<td>" . $row['car_id'] . "</td>";
            echo "<td>" . $row['car_name'] . "</td>";
            echo "<td>" . $row['car_price'] . "</td>";
            echo "<td>" . $row['car_colour'] . "</td>";
            echo "<td>" . $row['car_mode'] . "</td>";
            echo "<td><img src='" . $row['car_image'] . "' height='100' 
            width='100'></td>";
            echo "<td>" . $row['car_status'] . "</td>";

        echo "</tr>";
    }

There is no error.But i just want "book" button display for each car products.

  • 写回答

1条回答 默认 最新

  • drc4925 2019-05-26 04:25
    关注

    This is simply because your button is out of the while loop !
    Also you did not close first tr tag .
    Correct code :

    $sql = "SELECT * FROM car";
    if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
    echo "<table>";
        echo "<tr>";
            echo "<th>Id</th>";
            echo "<th>Name</th>";
            echo "<th>Price(RM)</th>";
            echo "<th>Colour</th>";
            echo "<th>Mode</th>";
            echo "<th>Image</th>";
            echo "<th>Status</th>";
            echo "<th>action</th>";<!-- Added this line -->
         echo "</tr>";<!-- Added this line -->
    
    while($row = mysqli_fetch_array($result)){
        echo "<tr>";
            echo "<td>" . $row['car_id'] . "</td>";
            echo "<td>" . $row['car_name'] . "</td>";
            echo "<td>" . $row['car_price'] . "</td>";
            echo "<td>" . $row['car_colour'] . "</td>";
            echo "<td>" . $row['car_mode'] . "</td>";
            echo "<td><img src='" . $row['car_image'] . "' height='100' 
            width='100'></td>";
            echo "<td>" . $row['car_status'] . "</td>";
            echo "<td><button onclick=\"book_car('" . $row['car_id'] . 
            "')\">Book</button></td>";<!-- Replaced This line -->
    
        echo "</tr>";
    }
    echo "</table>";
    


    I hope this helps you :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件