以下是我需要同步的三个表格。
- 表1是人(姓名,电子邮件等)
- 表2是汽车(品牌,颜色等)
- 表3是广告(一个广告包含表1和表2中的信息)
我基本上想要的是“表1”能够从“表2”中搜索数据。选择数据后,我希望将表1和表2中的数据输入表3中。
当用户登录时,他应该能够在搜索字段中搜索产品并选择一种产品 - 这就是完成并且正常工作。
问题在于我不知道如何获取搜索结果,在这个例子中它将是一辆汽车。然后通过提交按钮创建一个AD并在表3中输入数据,其中包含表1中的名称和电子邮件以及表2中的汽车模型和颜色。
以下是FORM的CODE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Live MySQL Database Search</title>
<style type="text/css">
body{
font-family: Arail, sans-serif;
}
/* Formatting search box */
.search-box{
width: 300px;
position: relative;
display: inline-block;
font-size: 14px;
}
.search-box input[type="text"]{
height: 32px;
padding: 5px 10px;
border: 1px solid #CCCCCC;
font-size: 14px;
}
.result{
position: absolute;
z-index: 999;
top: 100%;
left: 0;
}
.search-box input[type="text"], .result{
width: 100%;
box-sizing: border-box;
}
/* Formatting result items */
.result p{
margin: 0;
padding: 7px 10px;
border: 1px solid #CCCCCC;
border-top: none;
cursor: pointer;
}
.result p:hover{
background: #f2f2f2;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
/* Get input value on change */
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(inputVal.length){
$.get("adsToDb.php", {term: inputVal}).done(function(data){
// Display the returned data in browser
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
</head>
<br>
<br>
<p>Search the product you want to sell</p>
<body>
<div class="search-box">
<input type="text" autocomplete="off" placeholder="Search product..." />
<div class="result"></div>
</div>
</body>
<br>
<br>
<br>
<br>
<br>
<br>
<form action="createAd.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit" name="fields" value="sell this product" />
<input type="hidden" name="fieldsCount" value="<?php echo $fields+1 ?>" />
</form>
</html>
这是数据库搜索的代码
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
try{
$pdo = new PDO("mysql:host=localhost;dbname=DB", "", "");
// Set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
die("ERROR: Could not connect. " . $e->getMessage());
}
// Attempt search query execution
try{
if(isset($_REQUEST['term'])){
// create prepared statement
$sql = "SELECT * FROM products WHERE product_name LIKE :term";
$stmt = $pdo->prepare($sql);
$term = $_REQUEST['term'] . '%';
// bind parameters to statement
$stmt->bindParam(':term', $term);
// execute the prepared statement
$stmt->execute();
if($stmt->rowCount() > 0){
while($row = $stmt->fetch()){
echo "<p>" . $row['product_name'] . "</p>";
}
} else{
echo "<p>No matches found";
}
}
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
// Close connection
unset($pdo);
?>
答案 0 :(得分:0)
您可以使用隐藏字段来保存搜索结果的forloop中的汽车ID,并且一旦用户点击按钮添加到广告表,您已经拥有了登录用户ID并且可以从隐藏字段获取car_id
<form action="createAd.php" method="post">
Name: <input type="text" name="name">
<input type="submit" name="fields" value="sell this product" />
<input type="hidden" name="fieldsCount" value="<?php echo $fields+1 ?>" />
**<input type="hidden" name="car_id" value="<?php echo $car_id ?>" />**
</form>