简体   繁体   中英

Insert multiple records one query

I'm trying to solve a problem which I've now for 2days. But with any kind of success

I want to create something where people can track their progress, so if somebody 'creates a progress' certain information need to be selected from the DB and inserted into other table.

for now I got this:

ID | ITEM       | MOBNAME | GAME | LOG_ID | LOG_NAME

1  | test test3 | spider  | TEST |    1   |  just a test

As you see, I need COLUMN item among each other not in 1 single row. like this:

ID | ITEM   | MOBNAME | GAME | LOG_ID | LOG_NAME

1  | test   | spider  | TEST |    1   |  just a test
1  | test3  | spider  | TEST |    1   |  just a test
1  | test4  | spider  | TEST |    1   |  just a test

This is my current code for selecting and inserting:

$sql = "INSERT INTO log_create(`name`, name2, game, monster, info)VALUES('$name', '$name2', '$game', '$mobname', '$info')";
$log = $db->query("SELECT * FROM log_mitem WHERE mobname = '" .$mobname. "' AND game = '" .$game. "'") or die($db->error);

if($log1 = $log->fetch_object())
    {
while($loco = $log->fetch_object())
    {
$item .= "$loco->itemname";
    }
$logss = "INSERT INTO log_drops(`item`, mobname, game, log_id, log_name)VALUES('$item', '$mobname', '$game', '$id', '$name')";
if($result1 = $db->query($logss));
}

Find below two example.

$sql="INSERT INTO table1 (ITEM,MOBNAME,GAME,LOG_ID,LOG_NAME)SELECT ITEM,MOBNAME,GAME,LOG_ID,LOG_NAME FROM table2 WHERE mobname ='".$mobname."' AND game ='".$game ."'";
mysql_query($sql);  

If you have same structure.

$sql="INSERT INTO table1(SELECT ITEM,MOBNAME,GAME,LOG_ID,LOG_NAME FROM table2 WHERE WHERE mobname ='".$mobname."' AND game ='".$game ."')";
mysql_query($sql);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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