[英]Extract columns from different tables and insert into another one
So basically I have been trying to insert columns into my third table from columns extracted from 2 tables in prior. 因此,基本上我一直在尝试从先前从2个表中提取的列中将列插入到我的第三个表中。
I have one info table which stores information of users, one image table which stores head photos of users, and one id table which is like a reference table,designed to display correct head photos associated to correct users when necessary(like user querying or simply displaying...) 我有一个信息表,用于存储用户信息,一个图像表,用于存储用户的头像,以及一个id表,类似于参考表,旨在在必要时显示与正确用户相关联的正确头像(例如用户查询或简单地显示...)
My code: 我的代码:
$result_1 = mysql_query("SELECT info_id FROM info WHERE info_name = '$_POST[name]'");
$result_2 = mysql_query("SELECT image_id FROM image WHERE image_name = '$_FILES[file][name]'");
$sql = "INSERT INTO id_table (main_id, id_info, id_image)
VALUES (NULL, $result_1, $result_2);";
if(!mysql_query($sql,$connect_database)){
die('Error: ',mysql_error());
}
So the codes above simply illustrated my idea: 因此,以上代码仅说明了我的想法:
1. get the id of info from info table 1.从信息表获取信息的ID
2. get the id of image from image table 2.从图像表中获取图像的ID
3. insert both ids into columns in id_table respectively 3.将两个ID分别插入id_table的列中
Then I got this error information: 然后我得到了这个错误信息:
Error: You have an error in your SQL syntax;
错误:您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id #4, Resource id #5 )' at line 1
检查与您的MySQL服务器版本相对应的手册,以在第1行的'id#4,Resource id#5)'附近使用正确的语法
I could not figure out where the problem is... 我不知道问题出在哪里...
$result_1
and $result_2
just contain pointers to the results. $result_1
和$result_2
仅包含指向结果的指针。 you still have to loop through them to get the actual values. 您仍然必须遍历它们以获取实际值。
Try this: 尝试这个:
$result_1 = mysql_query("SELECT info_id FROM info WHERE info_name = '$_POST[name]'");
$row1 = mysql_fetch_array($result_1); // get 1st result row
$result_2 = mysql_query("SELECT image_id FROM image WHERE image_name = '$_FILES[file][name]'");
$row2 = mysql_fetch_array($result_2); // get 2nd result row
$sql = "INSERT INTO id_table (main_id, id_info, id_image)
VALUES (NULL, '{$row1['info_id']}', '{$row1['image_id']}');";
if(!mysql_query($sql,$connect_database)){
die('Error: ',mysql_error());
}
In your code you just execute the query. 在您的代码中,您只需执行查询。 But you did not fetch the field or column value.
但是您没有获取字段或列的值。 you can fetch it by using mysql_fetch_array or mysql_fetch_row.
您可以使用mysql_fetch_array或mysql_fetch_row来获取它。
Am modify code to your exact need: 根据您的实际需要修改代码:
$result_1 = mysql_query("SELECT info_id FROM info WHERE info_name = '$_POST[name]'");
$row1 = mysql_fetch_row($result_1);
$infoid = $row1[0];
$result_2 = mysql_query("SELECT image_id FROM image WHERE image_name = '$_FILES[file][name]'");
$row2 = mysql_fetch_row($result_2);
$imageid = $row2[0]
$sql = "INSERT INTO id_table (main_id, id_info, id_image) VALUES (NULL, $infoid, $imageid);";
if(!mysql_query($sql,$connect_database)){ die('Error: ',mysql_error()); }
for more information refer :- http://in3.php.net/manual/en/function.mysql-fetch-row.php http://in3.php.net/manual/en/function.mysql-fetch-array.php 有关更多信息,请参见: -http : //in3.php.net/manual/zh/function.mysql-fetch-row.php http://in3.php.net/manual/en/function.mysql-fetch-array .php文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.