简体   繁体   English

如何从另一个表的下拉列表中插入所选名称的ID?

[英]How to insert ID of a selected name from drop down list in another table?

I'm setting up a new simple website that it has two MySQL tables: 我正在建立一个新的简单网站,它有两个MySQL表:

Client_table: Client_table:

ID      Client_name
1      Alex
2      Bob
3      Clara

Order_table: Order_table:

ID      Client_name
1     Bob
2     Clara
3     Bob

Using the following PHP code, the Client_name is taken by dropdown list from Client_table and add it to Order_table : 使用以下PHP代码, Client_nameClient_name的下拉列表Client_table ,并将其添加到Order_table

<?php
require('db.php'); \\$con = mysqli_connect('host', 'user', 'pass', 'db');
$status = "";
if(isset($_POST['new']) && $_POST['new']==1){
$client_name =$_REQUEST['client_name'];
$ins_query="insert into Order_table (`Client_name`) values ('$Client_name')";
mysqli_query($con,$ins_query) or die(mysql_error());
$status = "Added Successfully";
}
?>
<form name="form" method="post" action="">
    <select type="text" name="Client_Name" />
        <?php
        $result = $con->query("select * FROM Client_table");
        while ($row = $result->fetch_assoc())
            { echo "<option value='".$row['id']."'>".$row['Client_name']."</option>";}
        ?>
    </select>
    <p>
        <input class="btn" name="submit" type="submit" value="Add" />
    </p>
</form>

How to insert the ID of Client as Client_id in Order_table? 如何在Order_table中插入Client的ID作为Client_id?

*NEW Order_table: *新订单表:

ID      Client_id      Client_name
1        2              Bob
2        3              Clara
3        2              Bob

You can select from Client_table when you're doing the INSERT to get the Client ID. 在执行INSERT以获得客户端ID时,可以从Client_table中选择。 You should also use a prepared statement to prevent SQL injection. 您还应该使用准备好的语句来防止SQL注入。

$ins_query= "INSERT INTO Order_table (Client_name, Client_id)
            SELECT Client_name, Client_id
            FROM Client_table
            WHERE Client_name = ?";
$ins_stmt = $con->prepare($ins_query) or die($con->error);
$ins_stmt->bind_param("s", $Client_name);
$ins_stmt->execute() or die($ins_stmt->error);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将从MySql填充的下拉列表中选择的整个行值插入另一个MySql表 - Insert entire row values selected from MySql populated drop down list into another MySql Table 如何将下拉列表中的其他选定值插入数据库 - How to insert different selected value from drop down list into database 使用 php mysql 在另一个表中插入选定下拉列表值的主键作为外键 - Insert Primary key of Selected Drop down list value as a foreign key in another table using php mysql 尝试将下拉列表中的数据作为外键插入另一个表中。 数据未插入 - Trying to insert data from a drop-down list as a foreign key in another table. Data not inserted 如何从下拉菜单中选择的数据插入数据库中的多个表 - How to insert the selected data from drop down menu into multiple table in database 如何获得从下拉菜单中选择的选项并将其插入到mysql表中 - How to obtain the option selected from the drop down menu and insert it into a mysql table 如何将下拉列表中的选定值插入另一个 MySQL 表 - How to insert selected value from dropdown list into another MySQL table 如何使用ajax从两个不同的下拉列表中获取每个选定项目的ID - how to get id of each selected item from two different drop down list using ajax 如何在下拉列表中显示所选项目名称? - How to show the selected item name in the drop down list? 如何在下拉列表中显示所选项目的名称? - How to show the selected item name in the drop down list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM