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

我想为每个汽车产品添加“预订”按钮。但它只显示第一辆车的一个按钮。


$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>";

    }

没有错误。但我只想为每个汽车产品显示“预订”按钮。


宝慕林4294392
浏览 154回答 1
1回答

www说

这仅仅是因为您的按钮不在while 循环中!你也没有关闭第一个tr标签。正确的代码:$sql = "SELECT * FROM car";if($result = mysqli_query($link, $sql)){if(mysqli_num_rows($result) > 0){echo "<table>";&nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<th>Id</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<th>Name</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<th>Price(RM)</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<th>Colour</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<th>Mode</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<th>Image</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<th>Status</th>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<th>action</th>";<!-- Added this line -->&nbsp; &nbsp; &nbsp;echo "</tr>";<!-- Added this line -->while($row = mysqli_fetch_array($result)){&nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row['car_id'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row['car_name'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row['car_price'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row['car_colour'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row['car_mode'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td><img src='" . $row['car_image'] . "' height='100'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; width='100'></td>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row['car_status'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td><button onclick=\"book_car('" . $row['car_id'] .&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "')\">Book</button></td>";<!-- Replaced This line -->&nbsp; &nbsp; echo "</tr>";}echo "</table>";
打开App,查看更多内容
随时随地看视频慕课网APP