简体   繁体   English

从不同的表中提取列并插入到另一个表中

[英]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.

相关问题 从不同的表中提取列并彼此相邻显示 - extract columns from different tables and display next to each other 从另一个表插入到表中 - insert into tables from another tables 从一个表中查找并提取数据并将其插入到 php 和 mysql 中的另一个表中 - find and extract data from one table and insert it into another in php and mysql 如何从一页将条目插入不同的表? - How to insert entries into different tables from one page? 如何将两个记录插入到不同的表中,首先使用insert,然后从insert php mysql中进行选择 - How to insert two records into different tables first one with insert and then with select from the insert php mysql 如何将表从一个数据库复制到其他服务器的另一个数据库 - How to copy tables from one DB to another DB Of Different server 将一个数据库中的数据插入另一台服务器上的另一个数据库中-未链接的服务器 - Insert data from one DB into another on a different server - servers not linked 减去不同表中的2列 - Subtract 2 Columns from different tables 从一个表到另一列的不同表中检索数据 - Retrieving Data From One Table To Another Table With Different Columns 有没有办法将一些值(从一种形式)插入到 Codeigniter 中的两个不同表中? - Is there a way to insert some values(from one form) into two different tables in Codeigniter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM