[英]Select Form - Link two tables in a third table
It's my first time here so please, be gentle...这是我第一次来这里,所以请温柔点...
My name is Maxime and I'm trying to learn PHP for few weeks now.我的名字是 Maxime,我正在尝试学习 PHP 几个星期。 I searched a lot on stack and Internet but I didn't find the solution, so I'm sorry if this is "basic" or if I don't explain very well.我在堆栈和互联网上搜索了很多,但我没有找到解决方案,所以如果这是“基本”或者我解释得不好,我很抱歉。 I'm looking for help!我正在寻求帮助!
This week, I'm trying something: I have 2 tables:本周,我正在尝试一些事情:我有 2 张桌子:
BARLIST律师
|-----------+--------+----------+-----+---------|
| barId | barName| Adresse | lat | long |
|-----------+--------+----------+---------------|
| 1 | Mike |50 street | 50 | 1.5 |
| 2 | John |51 street | 30 | 45 |
| 3 | Beth |52 street | 26 | 87 |
|-----------+--------+----------+---------------|
BEERLIST啤酒清单
|--------+----------+----------|
| beerId | beerName | typeBeer |
|--------+----------+----------|
| 1 | Leffe | blond |
| 2 | Affli | brune |
| 3 | Affli | blanche |
|--------+----------+----------|
BARBEER - The linking table I would like BARBEER - 我想要的链接表
|--------+----------+----------|
| Id | beerId | barId |
|--------+----------+----------|
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 2 |
|--------+----------+----------|
The idea is to let the user choose a bar and a beer from the tables through a select form, to add them into the third table barbeer .这个想法是让用户通过 select 表格从桌子中选择酒吧和啤酒,将它们添加到第三张桌子barbeer中。 So I've trying few things and recently here what I have.所以我尝试了一些东西,最近这里有我所拥有的。
Form with select content表格内容为 select
<form method="post" action="">
<label class="first-column ">Choose a bar</label><div class="second-column">
<select name="test">
<option>Select</option>
<?php
$sqli = "SELECT * FROM barList";
$result = mysqli_query($con, $sqli);
while ($row = mysqli_fetch_assoc(@$result)) {
?>
<option value="<?php echo $row['barId'];?>"><?php echo $row['barName'];?></option>';
<?php
}
?>
</select>
<label class="first-column ">Choose a beer</label><div class="second-column">
<select name="test1">
<option>Select</option>
<?php
$sqli = "SELECT * FROM beerList";
$result = mysqli_query($con, $sqli);
while ($row = mysqli_fetch_assoc(@$result)) {
# code...?>
<option value="<?php echo $row['beerId'];?>"><?php echo $row['beerName'];?></option>';
<?php
}
?>
</select>
<input type="submit" name="submit_4" value="Submit" />
It works great and I can see on my form the name of the beer and the bar.它很好用,我可以在我的表格上看到啤酒和酒吧的名称。 But now I would like to add the selection of the user into a third table, so I tried this:但是现在我想将用户的选择添加到第三个表中,所以我尝试了这个:
if(isset($_POST['submit_4'])){
if(empty($errors)){
try
{
$pdo = new PDO('mysql:host=localhost;dbname=whereismybeer;charset=utf8', 'root', '');
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
echo $_POST['test'];
echo $_POST['test1'];
$req= $pdo-> prepare("INSERT INTO barbeer JOIN barList, ON barbeer.barId = ? JOIN beerList, ON barbeer.beerId = ?");
$req->execute([$_POST['test'], $_POST['test']]);
die ('Yeah, It worked!');
}}
Also I tried with a variable like this我也试过这样的变量
$var = $_POST['test];
INSERT INTO [...] barbeer.barID = $var;
I had no error messages, just the "Yeah, It worked."我没有错误消息,只有“是的,它有效”。 but nothing on the barbeer table... So I'm stuck, Could you help me to find a solution?但理发台上什么都没有……所以我被困住了,你能帮我找到解决办法吗? ideas or anything useful?想法或任何有用的东西?
Thank you very much for the answer and the help!非常感谢您的回答和帮助!
EDIT: In another page, in the future, the idea will be to ask "where can I drink this beer?", and the website to show the list of the bar where I could drink this beer.编辑:在另一个页面中,未来的想法将是询问“我在哪里可以喝这种啤酒?”,网站会显示我可以喝这种啤酒的酒吧列表。 That's why I thought a Joint would be necessary.这就是为什么我认为联合是必要的。
EDIT 2: Ok, so my understanding of the JOIN function was wrong.编辑 2:好的,所以我对 JOIN function 的理解是错误的。 I don't need to call it now.我现在不需要打电话了。 I just need to put the beerID and the barID into the third table with a classic INSERT.我只需要用经典的 INSERT 将 beerID 和 barID 放入第三个表中。
INSERT INTO barbiere (barName, biereName) VALUES (?, ?);
You don't need to use join.你不需要使用加入。 You have the values of beerId, barId from the form.您从表单中获得了 beerId、barId 的值。 So just do a normal insert.所以只要做一个正常的插入。
The statement should be.声明应该是。
INSERT INTO barbeer (beerId, barId) VALUES (?, ?);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.