简体   繁体   English

php sql insert multiple

[英]php sql insert multiple

Im trying to convert data from one table to another, from the ITEMS_VEHICLES table to the VEHICLES table. 我试图将数据从一个表转换为另一个表,从ITEMS_VEHICLES表转换到VEHICLES表。 My code is this: 我的代码是这样的:

$sql_result = mysql_query("SELECT * FROM items_vehicles", $db); 
while ($rs = mysql_fetch_array($sql_result)) {
    if ($rs[motorbike] > 0) { mysql_query("INSERT INTO vehicles (type, player, hp) VALUES('1', '$rs[player]', '50')"); }
    if ($rs[banger] > 0) { mysql_query("INSERT INTO vehicles (type, player, hp) VALUES('2', '$rs[player]', '50')"); 
}

etc 等等

But the $rs[motorbike] and other fields i want them to insert that code the amount of times in that column. 但是$ rs [motorbike]和其他字段我希望他们在该列中插入该代码的次数。 Example, if $rs[motorbike] was '5', I want that insert query ran 5 times. 例如,如果$ rs [motorbike]为'5',我希望插入查询运行5次。

I'm in a bind, not sure how to do it. 我陷入困境,不知道怎么做。

Judging by your explanation, something like this: 从你的解释来看,这样的事情:

$sql_result = mysql_query("SELECT * FROM items_vehicles", $db); 
while ($rs = mysql_fetch_array($sql_result)) {
    for ($i = 1; $i <= $rs['motorbike']; $i++) { 
        mysql_query("INSERT INTO vehicles (type, player, hp) VALUES('1', '$rs[player]', '50')"); 
    }
    for ($i = 1; $i <= $rs['banger']; $i++) { 
        mysql_query("INSERT INTO vehicles (type, player, hp) VALUES('2', '$rs[player]', '50')");    
    }
    etc
}

用一个运行x次的for循环替换你的if语句,其中x$rs['motorbike']的大小。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM