[英]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进行插入
Better use prepare statement while insertion of data 插入数据时更好地使用prepare语句
$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.