Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 插入到由主键链接的表中_Mysql_Codeigniter_Join_Insert - Fatal编程技术网

Mysql 插入到由主键链接的表中

Mysql 插入到由主键链接的表中,mysql,codeigniter,join,insert,Mysql,Codeigniter,Join,Insert,我有两张有关系的桌子 +----------------------+ +-----------------+ | Shape | | Point_Values | +----------------------+ +-----------------+ | shape_id (KEY) | | id (KEY) | | shape_name (VARCHAR) | | point_x

我有两张有关系的桌子

+----------------------+      +-----------------+
|        Shape         |      |  Point_Values   |
+----------------------+      +-----------------+
| shape_id (KEY)       |      | id (KEY)        |
| shape_name (VARCHAR) |      | point_x (INT)   |
+----------------------+      | point_y (INT)   |
                              | shape_id (INT)  |
                              +-----------------+
如何在获取同一请求中的形状和点的同时构建一个插入,或者为CodeIgniter构建一个插入,这意味着当一个形状完成时,有一个点数组和形状名称/id。我必须在表中进行插入,但两个表中的形状id都匹配


应要求

Array ( [name] => "circle" [points] => Array ( [x] => Array ( [0] => 182 [1] => 190 [2] => 215 [3] => 240 [4] => 291 [5] => 328 [6] => 364 [7] => 391 [8] => 425 [9] => 459 [10] => 487 [11] => 512 [12] => 529 [13] => 540 [14] => 551 [15] => 560 [16] => 570 [17] => 581 [18] => 592 [19] => 604 [20] => 617 [21] => 628 [22] => 635 [23] => 639 [24] => 642 [25] => 642 [26] => 640 [27] => 630 [28] => 619 [29] => 606 [30] => 591 ) [y] => Array ( [0] => 165 [1] => 159 [2] => 150 [3] => 147 [4] => 144 [5] => 144 [6] => 147 [7] => 152 [8] => 162 [9] => 172 [10] => 184 [11] => 199 [12] => 211 [13] => 219 [14] => 225 [15] => 228 [16] => 231 [17] => 232 [18] => 233 [19] => 233 [20] => 231 [21] => 222 [22] => 204 [23] => 189 [24] => 156 [25] => 133 [26] => 120 [27] => 100 [28] => 85 [29] => 69 [30] => 54 ) ) ) 排列 ( [名称]=>“圆圈” [点]=>阵列 ( [x] =>阵列 ( [0] => 182 [1] => 190 [2] => 215 [3] => 240 [4] => 291 [5] => 328 [6] => 364 [7] => 391 [8] => 425 [9] => 459 [10] => 487 [11] => 512 [12] => 529 [13] => 540 [14] => 551 [15] => 560 [16] => 570 [17] => 581 [18] => 592 [19] => 604 [20] => 617 [21] => 628 [22] => 635 [23] => 639 [24] => 642 [25] => 642 [26] => 640 [27] => 630 [28] => 619 [29] => 606 [30] => 591 ) [y] =>阵列 ( [0] => 165 [1] => 159 [2] => 150 [3] => 147 [4] => 144 [5] => 144 [6] => 147 [7] => 152 [8] => 162 [9] => 172 [10] => 184 [11] => 199 [12] => 211 [13] => 219 [14] => 225 [15] => 228 [16] => 231 [17] => 232 [18] => 233 [19] => 233 [20] => 231 [21] => 222 [22] => 204 [23] => 189 [24] => 156 [25] => 133 [26] => 120 [27] => 100 [28] => 85 [29] => 69 [30] => 54 ) ) )
我建议使用基本算法,首先在
shape\u id(point\u value)
shape\u id(shape)

首先插入到形状表中

mysql_query("INSERT INTO shape SET shape_name='".$array['name']."'");
然后获取上次插入的id

$shape_id=mysql_insert_id();
然后转到点_值表

foreach( $array['points] as $k)
{

   $qry= "INSERT INTO point_value SET
                               point_x=(int)$array['points']['x'][$i],
                               point_y=(int)$array['points']['y'][$i],
                               shape_id=(int)$shape_id ";
   mysql_query($qry);
} 
我希望这能帮助你寻求帮助
谢谢

考虑到这个数组,这段代码应该可以很好地工作。它执行一次初始插入以从表形状中获取id,并批量插入所有点

function insertShape($s){
    mysql_query('INSERT INTO shape (name) VALUES ("'.$s['name'].'");');
    $id=mysql_insert_id();
    $i=0;
    for(;;){
        if(!isset($s['points']['x'][$i], $s['points']['y'][$i]))break;
        $inserts[]='('.$id.','.$s['points']['x'][$i].','.$s['points']['y'][$i].')';
        $i++;
    }
    mysql_query('INSERT INTO point_values (shape_id,x,y) VALUES '.implode(',',$inserts));
}

如果你的情况不需要对钥匙进行严格的测试,我可能会把它变成一个foreach。

你会展示你的阵列吗???@diEcho补充到问题中。然后将该数组解析到表中。您可以批量插入所有点,无需向服务器发送超过2个请求的垃圾邮件。@khez即使您的查询也会请求两次。若我们想插入到两个表中,那个么我们必须准确地调用服务器2次(AFAIK),即2次。正好两次。你做了31次。1表示形状,30表示点。考虑圆形的例子。