gpt4 book ai didi

php - 如何同时向mysql数据库的两个不同表中插入数据?

转载 作者:行者123 更新时间:2023-11-29 07:33:45 25 4
gpt4 key购买 nike

我想同时将数据插入到mysql数据库中的两个不同的表中,但我的数据只插入到user_signup表中,而不插入到另一个表中。这是我的代码:

<?php
//If user is a ministry head
if($access_level == 'ministry_head')
{
$sql="INSERT INTO ministry_head (eemail,mobile_number,fname,mname,lname,access_level,region_name,district_name,ssn,district_pass,pass_con) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$region_name','$district_name','$ssn','$district_pass','$pass_con')";
$sql="INSERT INTO user_signup (eemail,mobile_number,fname,mname,lname,access_level,region_name,district_name,ssn,district_pass,pass_con) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$region_name','$district_name','$ssn','$district_pass','$pass_con')";
$_SESSION['eemail'] = $eemail;
header('Location: ministry_head_page.php');
}
//If user is a regional head
else if($access_level == 'regional_head')
{
$sql="INSERT INTO regional_head (eemail,mobile_number,fname,mname,lname,access_level,region_name,district_name,ssn,district_pass,pass_con) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$region_name','$district_name','$ssn','$district_pass','$pass_con')";
$sql="INSERT INTO user_signup (eemail,mobile_number,fname,mname,lname,access_level,region_name,district_name,ssn,district_pass,pass_con) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$region_name','$district_name','$ssn','$district_pass','$pass_con')";
$_SESSION['eemail'] = $eemail;
header('Location: regional_head_page.php');
}
//if user is general user
else if($access_level == 'general_user')
{
$sql="INSERT INTO general_user (eemail,mobile_number,fname,mname,lname,access_level,region_name,district_name,ssn,district_pass,pass_con) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$region_name','$district_name','$ssn','$district_pass','$pass_con')";
$sql="INSERT INTO user_signup (eemail,mobile_number,fname,mname,lname,access_level,region_name,district_name,ssn,district_pass,pass_con) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$region_name','$district_name','$ssn','$district_pass','$pass_con')";
$_SESSION['eemail'] = $eemail;
header('Location: general_user_page.php');
}
//if user is car company
else if ($access_level == 'car_company')
{
$sql="INSERT INTO car_company (eemail,mobile_number,fname,mname,lname,access_level,region_name,district_name,ssn,district_pass,pass_con) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$region_name','$district_name','$ssn','$district_pass','$pass_con')";
$sql="INSERT INTO user_signup (eemail,mobile_number,fname,mname,lname,access_level,region_name,district_name,ssn,district_pass,pass_con) VALUES ('$eemail','$mobile_number','$fname','$mname','$lname','$access_level','$region_name','$district_name','$ssn','$district_pass','$pass_con')";
$_SESSION['eemail'] = $eemail;
header('Location: car_company_page.php');
}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "";
mysqli_clos
e($con);
}
?>

最佳答案

将两个不同的字符串分配给一个变量:

$sql="INSERT INTO car_company ...";
$sql="INSERT INTO user_signup ...";

第二行覆盖第一行,因此它不会执行。作为解决方法,您可以这样做:

$sql1="INSERT INTO car_company ...";
$sql2="INSERT INTO user_signup ...";

后跟

mysqli_query($con,$sql1);
mysqli_query($con,$sql2);

进行适当的错误检查等等。

关于php - 如何同时向mysql数据库的两个不同表中插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31650669/

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