简体   繁体   English

我想将表单中的数据插入表中,然后从另一个表中选择另一个数据并使用PHP插入到第三张表中

[英]I want to insert a data from a form into a table, and select another data from another table and insert into third table using PHP

I have three related tables “users”, “category” and “interest_area”; 我有三个相关的表“用户”,“类别”和“ interest_area”; and I want to insert a data from a form into “users” table, and select another data from “category” table and insert into “interest_area” table using PHP. 我想将表单中的数据插入“用户”表中,并从“类别”表中选择另一个数据,然后使用PHP插入“ interest_area”表中。

The error it shows is: 它显示的错误是:

Error: INSERT INTO users(user_id, first_name, last_name, higher_education, user_name, pass_word) VALUES('' , '87878787' , 'iuiu' , 'iuiu' , 'root' , '');INSERT INTO interest_area (category_id) SELECT category_id FROM category WHERE category_name = 'ASP'; 错误:INSERT INTO用户(user_id,first_name,last_name,higher_education,user_name,pass_word)VALUES('','87878787','iuiu','iuiu','root',''); INSERT INTO interest_area(category_id)SELECT category_id从类别WHERE category_name ='ASP'; Erreur de syntaxe pr s de 'INSERT INTO interest_area (category_id) SELECT category_id FROM category ' la ligne 2 在'INSERT INTO interest_area(category_id)中选择类目ID从FROM类别'-la ligne 2

My PHP code is: 我的PHP代码是:

<?php
   if (isset($_POST["interest_area"])){             
   $f_name = $_POST["firstname"];
   $l_name = $_POST["last_name"];
   $h_education = $_POST["higher_education"];
   $i_area = $_POST["interest_area"];
   $email = $_POST["email"];
   $u_name = $_POST["user_name"];
   $p_word = $_POST["pass_word"];

  $sql = "INSERT INTO users(user_id, first_name, last_name,   higher_education, user_name, pass_word)
  VALUES('' , '$f_name' , '$l_name' , '$h_education' , '$username' ,  '$password');";

  $sql .= "INSERT INTO interest_area (category_id)
    SELECT category_id FROM category
    WHERE category_name = '$i_area';";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";} 
else { echo "Error: " . $sql . "<br>" . $conn->error;} 
}
?>

You have to run two mysqli_query for insertion 您必须运行两个mysqli_query进行插入

mysqli_query mysqli_query

Better use prepare statement while insertion of data 插入数据时更好地使用prepare语句

prepare statement 准备陈述

$f_name = $_POST["firstname"];
$l_name = $_POST["last_name"];
$h_education = $_POST["higher_education"];
$i_area = $_POST["interest_area"];
$email = $_POST["email"];
$u_name = $_POST["user_name"];
$p_word = $_POST["pass_word"];
$user_id = $_POST["user_id"];

$user_id should not be blank if it ur primary key then data can't be inserted; $user_id如果它是您的主键,则不能插入数据;

 $sql1 = "INSERT INTO users(user_id, first_name, last_name,   higher_education, user_name, pass_word)
      VALUES('$user_id' , '$f_name' , '$l_name' , '$h_education' , '$u_name' ,  '$p_word')";

 $sql2 = "INSERT INTO interest_area (category_id)
     SELECT category_id FROM category WHERE category_name = '$i_area'";

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

您只需要将两个INSERT语句作为单独的$conn->query调用运行,而不是将它们串联为一个调用。

The Syntax error is here: 语法错误在这里:

$sql .= "INSERT INTO interest_area (category_id)
SELECT category_id FROM category
WHERE category_name = '$i_area';";

should be in curly... 应该是卷曲的...

$sql .= "INSERT INTO interest_area (category_id)
SELECT category_id FROM category
WHERE category_name = {$i_area};";

And two separate queries as stated... 以及如所述的两个独立的查询...

You need to use multi_query for multiple queries. 您需要对多个查询使用multi_query。

$sql = "INSERT INTO users(user_id, first_name, last_name, higher_education, user_name, pass_word)VALUES('' , '$f_name' , '$l_name' , '$h_education' , '$username' , '$password');";

$sql .= "INSERT INTO interest_area (category_id)
     SELECT category_id FROM category WHERE category_name = '$i_area'";

 mysqli_multi_query($con,$sql);
 mysqli_close($con);

暂无
暂无

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

相关问题 从db表1中选择数据并将其插入到php中的另一个db表2中 - Select data from db table 1 and insert it into another db table 2 in php PHP:从表单和同一表中的另一个表插入数据 - PHP: Insert data from form and another table in the same table 想要将数据从一个表插入到另一个在PHP中? - want to insert data from one table into another in php? PHP / SQL使用准备好的语句从表单插入数据并从另一个表插入数据 - PHP/SQL Insert data from form and insert data from another table using prepared statement 如何使用php将一个表中的选定数据插入另一个表? - How to insert selected data from a table into another table using php? 从另一个表以及MYSQL和PHP中的表单插入数据 - Insert data from another table and also from form in MYSQL and PHP 我想插入一个表中,从同一数据库中的另一个表中获取数据的PHP - i want to insert into a table, a data gotten from another table in the same database in php PHP 将数据从一个表插入到另一个表 - PHP Insert data from one table to another 如何合并来自表单和来自三个mysql表的数据,以使用php将数据插入另一个表中? - How can I combine data from form and from three mysql tables to insert data in another table with php? 如何使用SESSION数据来选择1个数据库条目,然后使用PHP和MySQL的表单中的数据插入到另一个表中? - How do I use SESSION data to SELECT 1 database entry and then INSERT into another table with data from a form as well with PHP and MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM