通过使用PHP和HTML点击链接将数据插入mysql表

xdnvmnnf  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(147)

我是PHP新手,正在创建一个Airbnb网站,为用户提供推荐。我的数据库中有三个表:用户,房子和user_choice.我显示关于房子的数据,就像简单的html表,为了测试,我显示每个房子的ID,就像房子的数量,其中包括一个链接,用户可以点击它,并转到其他页面,以查看更多关于房子的信息。
我想把用户感兴趣的每一栋房子写在表格里。所以,我的逻辑是这样的:每当用户想知道更多关于房子的信息时,他就点击链接,然后我的代码应该在“user_choice”表中插入一条记录,其中包含用户的id和他选择的房子的id。2我该怎么做呢?
我的代码:

<?php
include "db.php";
session_start();

    if (isset($_SESSION['Id_user']) && isset($_SESSION['Login'])) {

?>

<!DOCTYPE html>
<html lang="eu">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
    <title>Home page</title>
</head>
<body>
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">

            <div class="card mt-5">
                <div class="card-header">
                    <h4>Choose your house</h4>
                </div>
                <div class="card-body">

                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th>№ house</th>
                            <th>City</th>
                            <th>Location</th>
                            <th>Area Type</th>
                            <th>Price</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php

                        $query = "SELECT * FROM house_rent_dataset";
                        $query_run = $mysqli->query($query);

                        if(mysqli_num_rows($query_run) > 0)
                        {
                            foreach($query_run as $row)
                            {
                                ?>
                                <tr>
                                    <td><a href="about_house.php"><?= $row['Id_house']; ?></a></td>
                                    <td><?= $row['City']; ?></td>
                                    <td><?= $row['Locality']; ?></td>
                                    <td><?= $row['Area Type']; ?></td>
                                    <td><?= $row['Price']; ?></td>
                                </tr>
                                <?php
                            }
                        }
                        else
                        {
                            ?>
                            <tr>
                                <td colspan="4">No Record Found</td>
                            </tr>
                            <?php
                        }
                        ?>

                        </tbody>
                    </table>

                </div>
            </div>

        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
            <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

<?php
    }
    else {
        header("Location: index.php");
        exit();
    }
    ?>

我将感激你的帮助。

92dk7w1h

92dk7w1h1#

你可以用count在你点击用户选择的房子的详细链接上做简单的逻辑,并显示明细房屋。您可以在函数中创建逻辑,使该操作显示明细房屋(updateOrCreate(如果您使用PHP框架,如Laravel或CodeIgniter)将包含user_idhouse_id的数据库user_choice转换为user_id
让我给予一个例子

// this is function for display detail of house that user //selected or click
    public function detailHouse(Request $request){

      //this is the code for make user_choice (recommended house     //for user based on user selected) 
      data["user_id"] = $request->user_id;
      data["house_id"] = $request->house_id;
      data["count"] += 1;
      user_choice::updateOrCreate(data);
      
      //this is your code for display detail of house selected
      $house = house::where('id', $request->house_id)->first()
      return view('detail_house', [
        'data' => $house
      ];
    }

然后,您可以显示表格关系,显示给用户,根据有多少用户点击或选择每个房子的细节为用户推荐房子

相关问题