0

我的数据库中显示了 2 个结果。这两个结果都代表汽车。我需要在下面创建的表单中显示这些结果。目前它成功显示一个结果,但我需要从这些表中显示几个结果。一旦我开始工作,我会添加更多的汽车。有任何想法吗?

在此处输入图像描述

<!DOCTYPE html>
    <?php
    session_start();
    ?>
    <html>
        <head>
            <meta charset="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <meta name="apple-mobile-web-app-capable" content="yes" />
            <meta name="apple-mobile-web-app-status-bar-style" content="black" />
            <title>
            </title>
            <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
            <link rel="stylesheet" href="my.css" />
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
            </script>
            <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
            </script>
            <script src="my.js">
            </script>
            <!-- User-generated css -->
            <style>
            </style>
            <!-- User-generated js -->
            <script>

                try {

        $(function() {

        });

      } catch (error) {
        console.error("Your javascript has an error: " + error);
      }
            </script>
         </head>
        <body>
            <!-- Home -->
            <div data-role="page" id="page1">
                <div data-theme="a" data-role="header">
                <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                        Back
                    </a>
                    <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
                     Home  
                    </a>
                    <h3>
                        Book Car
                    </h3>
               </div>

               <div data-role="content">
                    <h3>
                        Select the car to rent from the availability:
                    </h3>
                    <br />
            <?php

            {
                mysql_connect("localhost" , "" , "") or die (mysql_error());
                mysql_select_db("") or die(mysql_error());


                $pid=intval($_SESSION["User_id"]); 
                $query = "SELECT `car`, `details`, `price` FROM `Car`";

                //executes query on the database
                $result = mysql_query ($query) or die ("didn't query");

                //this selects the results as rows
                $num = mysql_num_rows ($result);    


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

                    $_SESSION['car'] = $row['car'];
                    $_SESSION['details'] = $row['details'];
                    $_SESSION['price'] = $row['price'];
                }
            }
            ?>  
    <html>
    <body>

    <form method="post" action="car.php">
     Car: <input type="text" name="country" value="<?php echo $_SESSION['car']; ?>" readonly><br>
     Details: <input type="text" name="country" value="<?php echo $_SESSION['details']; ?>" readonly><br>
     Price: <input type="text" name="country" value="<?php echo $_SESSION['price']; ?>" readonly><br>
     <img style="width: 130px; height: 100px" img alt="car"src="image/astra.jpg" /&gt;' />
      <input type="submit" value="Submit">

       Car: <input type="text" name="country" value="<?php echo $_SESSION['car']; ?>" readonly><br>
     Details: <input type="text" name="country" value="<?php echo $_SESSION['details']; ?>" readonly><br>
     Price: <input type="text" name="country" value="<?php echo $_SESSION['price']; ?>" readonly><br>
     <img style="width: 130px; height: 100px" img alt="car"src="image/audi.jpg" /&gt;' />
      <input type="submit" value="Submit">


    </form>

    </body>
    </html>
4

2 回答 2

0
                    $_SESSION['car'] = $row['car'];
                    $_SESSION['details'] = $row['details'];
                    $_SESSION['price'] = $row['price'];
                }

每一个新行都会覆盖旧行。

您可以在 while 循环中创建 html,如下所示:

while($row=mysql_fetch_assoc($result))
{
?>
<form method="post" action="car.php">
     Car: <input type="text" name="country" value="<?php echo $row['car']; ?>" readonly><br>
     Details: <input type="text" name="country" value="<?php echo $row['details']; ?>" readonly><br>
     Price: <input type="text" name="country" value="<?php echo $row['price']; ?>" readonly><br>
     <img style="width: 130px; height: 100px" img alt="car"src="image/astra.jpg" /&gt;' />
      <input type="submit" value="Submit">
</form> // now you makes a form for each car
<?php
}
于 2013-04-09T15:07:10.133 回答
0

您需要在循环中输出表单,因为此时您刚刚结束了会话变量,因此您将获得表中的最后一辆车。

<form method="post" action="car.php">

<?php while($row=mysql_fetch_assoc($result)) { ?>

  Car: <input type="text" name="country" value="<?php echo $row['car']; ?>" readonly><br>
  Details: <input type="text" name="country" value="<?php echo $row['details']; ?>" readonly><br>
  Price: <input type="text" name="country" value="<?php echo $row['price']; ?>" readonly><br>
  <img style="width: 130px; height: 100px" img alt="car"src="image/<?php echo $row['img']; ?>" /&gt;' />

<?php } ?>

<input type="submit" value="Submit">
</form>

我建议将图像名称存储在数据库中,如上所示。老实说,目前该表单是相当多余的,因为当您提交它时,您只会提交表单中的最后一辆车,您需要找到一种允许用户选择他们想要的汽车的方法。

编辑:选择特定汽车的代码

我只是很快把这些放在一起,如有任何错误,我深表歉意。

<form method="post" action="car.php">
  <table>
    <tr>
      <th>Image</th>
      <th>Name</th>
      <th>Details</th>
      <th>Price</th>
      <th></th>
    </tr>
  <?php while($row=mysql_fetch_assoc($result)) { ?>
    <tr>
      <td><img style="width: 130px; height: 100px" alt="<?php echo $row['car']; ?>" src="image/<?php echo $row['img']; ?>" /></td>
      <td><?php echo $row['car']; ?></td>
      <td><?php echo $row['details']; ?></td>
      <td><?php echo $row['price']; ?></td>
      <td><input type="radio" name="selected_car" value="<?php echo $row['car']; ?>" /></td>
    </tr>
  <?php } ?>
  </table>
  <input type="submit" value="Submit">
</form>

然后,当您提交时car.php,值$_POST['selected_car']将是用户选择的任何汽车。

我强烈建议在该表中添加一个 ID 列,并使用它来代替单选按钮中的汽车名称。

我还强烈建议您查看一些 PHP/MySQL 教程,以便您掌握基础知识。对于开始使用 PHP 非常有用。

于 2013-04-09T15:12:24.813 回答