gpt4 book ai didi

php - 将数据从一个表插入到另一个表中

转载 作者:行者123 更新时间:2023-11-29 10:32:30 25 4
gpt4 key购买 nike

我有一个如下所示的表(用户):

ID |    CODE   | Name
---+----------+---------
1 | 00A | Tom
2 | 12E | Steve
3 | A4B | Maria

现在我有其他表要插入数据,我需要插入user_id。问题是我只收到文档中的用户代码。我必须在用户表中搜索与该代码对应的 id。

我想我必须做这样的事情:

$code = 123 

$query="select from users where id=$code"

$user_id = $query

$sql = "insert into table values ($user_id)"

我知道 SQL/PHP 代码不完整,我只是在寻找正确的逻辑。也许有一种方法可以在同一查询中搜索和插入。

最佳答案

我必须在用户表中搜索与您的问题中的该代码对应的 ID 执行此操作。

$code = 123 

$query="select * from users where code=$code"; //your variable code if confusing me, but it is fetching id based on id but different variable name do this.

$query="select * from users where id=$code";

$runquery = mysqli_query($con, $query); //$con is your database connection parameter

$fetchuser = mysqli_fetch_array($runquery);

$idofuser = $fetchuser['id'];


$sql = "insert into table values ($idofuser)";
$coolrun = mysqli_query($con, $sql);

if ($coolrun){
echo "The query ran.";
}

关于php - 将数据从一个表插入到另一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47141160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com