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



我是PHP新手,我正在创建一个为用户提供推荐的Airbnb网站。我的数据库中有三个表:users、houses和user_choice。我显示关于房子的数据像简单的html表和测试我显示每个房子的Id像一些房子,其中包括一个链接,用户可以点击并去其他页面查看更多关于房子的信息。

我想在表中写下每个用户感兴趣的房子。所以,这是我的逻辑:每次用户想要了解更多关于房子的信息时,他点击链接,之后我的代码应该在表user_choice&quot中插入一条记录,它将包含用户的id和他选择的房子的id。我该怎么做呢?

我代码:

<?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();
}
?>
我将感激你的帮助。

您可以在用户选择的房屋点击详细链接上使用count进行简单的逻辑。每一个用户点击详细链接,它将去新的页面,发送数据参数id的房子,并显示详细的房子。你可以在你的函数中做逻辑,动作是用updateOrCreate显示详细的房子(如果你使用php框架,如Laravel或CodeIgniter)数据库user_choice包含user_idhouse_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
];
}

然后,您可以根据有多少用户点击或选择每个房屋的详细信息,显示为用户推荐的房屋的表关系

最新更新