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

php - 如何在mysql数据库中同时插入两个不同的表中的数据?,第1张

我想同时在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); 
} 
?>

最佳答案:

2 个答案:

答案 0 :(得分:2)

您可以为一个变量分配两个不同的字符串:

$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);

进行适当的错误检查和所有。

答案 1 :(得分:0)

$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')";

OR

$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'); 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');";

multi_query($conn,$sql);

你需要使用mysqli_ *。假设$ conn是你的mysqli连接

here

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复