简体   繁体   English

PHP-在while循环上插入SQL

[英]PHP - Insert SQL on while loop

I generated a random monster team from a table, and limited by 6. Now, I also want to insert the team into user_team which contains the fields m1,m2,m3,m4,m5,m6 我从一个表中生成了一个随机的怪物团队,并限制为6。现在,我还想将团队插入到user_team中,其中包含字段m1,m2,m3,m4,m5,m6

Those represent Monster 1 to Monster 6. Now when I try to insert the generated monsters into the team, only the last Monster seems to be inserting into it while I all of my monsters to be inserted. 它们代表怪物1到怪物6。现在,当我尝试将生成的怪物插入到团队中时,似乎只有最后一个怪物正在插入其中,而我要插入所有的怪物。 http://prntscr.com/8zrj2 http://prntscr.com/8zrj2

$sql = "SELECT * from monsterdata ORDER BY RAND() LIMIT 6"; 
$result = mysql_query($sql);
// this checks if you have a result
if ($result == null) echo "No result";
else {  
    while (($row = mysql_fetch_array($result)) != false) {
$row = mysql_fetch_array($result);
{
// html code here
}
}

the Insert statement is $monster = $row['id']; $sql = "INSERT into user_team(m1,m2,m3,m4,m5,m6) VALUES('$monster','$monster','$monster','$monster','$monster','$monster')"; $result = mysql_query($sql); Insert语句为$monster = $row['id']; $sql = "INSERT into user_team(m1,m2,m3,m4,m5,m6) VALUES('$monster','$monster','$monster','$monster','$monster','$monster')"; $result = mysql_query($sql); $monster = $row['id']; $sql = "INSERT into user_team(m1,m2,m3,m4,m5,m6) VALUES('$monster','$monster','$monster','$monster','$monster','$monster')"; $result = mysql_query($sql);

Just don't know where/how to place it so it inserts the right values into the right columns. 只是不知道它在哪里/如何放置,因此它将正确的值插入正确的列。

If it were me, I would push the ids into an array and then use that like so: 如果是我,我会将ID推入数组,然后像这样使用它:

$monsterIds = array();
while(($row = mysql_fetch_array($result)) !== false) {
    $monsterIds[] = $row['id'];
}
mysql_query("INSERT INTO user_team (m1, m2, m3, m4, m5, m6) VALUES ('{$monsterIds[0]}', '{$monsterIds[2]}', '{$monsterIds[3]}', '{$monsterIds[4]}', '{$monsterIds[5]}')") or die(mysql_error());

Also, don't forget to use the triple equals when comparing row results so that you don't get caught by a weird bug where things evaluate to false that aren't actually false (=== is the way to go with many functions which might return either an array, integer, or a boolean depending on the outcome). 另外,在比较行结果时也不要忘记使用三等号,这样您就不会被一个奇怪的错误所困扰,在该错误中,事情评估为false并不是真的错误(===是使用许多函数的方式可能会根据结果返回数组,整数或布尔值)。

The values will be placed in the columns in the order they are supplied. 这些值将按照提供的顺序放置在列中。

Here is my example using PDO (php library): 这是我使用PDO(PHP库)的示例:

$DBH = new PDO("mysql:host=$db_host;dbname=$db_name", $username, $password);
$webContractList=$DBH->query('SELECT id,nume,data FROM user2')->fetchAll();

$STH=$DBH->prepare("INSERT INTO user (id,nume,data) VALUES (:id ,:nume , :data)");

foreach ($webContractList as $item){           
    $STH->execute(array(':id'=>$item['id'],
                ':nume'=>$item['nume'],
                ':data'=>date('Y-m-d',strtotime($item['data']))));
}

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

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