【问题标题】:insert into mysql db with php while loop使用 php while 循环插入 mysql 数据库
【发布时间】:2012-04-23 16:22:44
【问题描述】:

我在尝试将多个条目插入 MySQL innodb 时遇到以下问题。

只有第一个插入存储在数据库中。 (虽然它的存储是正确的)

db数据正确,post数据正确,一切都经过测试 while 循环在我的测试中计数正确(没有插入)

即$r = 7 $s = 3 给了我 21 个插槽,这是正确的。

$l = $_POST['Lager'];
$r = $_POST['Reihe'];
$p = $_POST['platz'];
$s = $_POST['slots'];
$a = $_POST['art'];

echo( "test: " . $_POST['Lager'] . $_POST['Reihe'] . $_POST['platz'] . $_POST['slots'] . $_POST['art'] );

$i=0;
$n=0;
$counter =0;

while($i < $p)
{
$platz =("
 INSERT INTO 
    Lager (LagerNr,ReiheNr,PlatzNr,SlotNr,LagerArt,Stock)
 VALUES ('". $l ."','" . $r  . "','" . $i . "','". $n ."','" . $a . "','0')");
 mysql_query($platz);
 echo ($platz);

    // anzahl slots = $s
    while($n < $s)
    {

    $slot("
     INSERT INTO 
        Lager (LagerNr,ReiheNr,PlatzNr,SlotNr,LagerArt,Stock)
     VALUES ('". $l ."','" . $r  . "','" . $i . "','". $n ."','" . $a . "','0')");
     mysql_query($slot) OR print(mysql_error());

    $n++;
    $counter++;
    echo($slot);
    }
    $n = 0;
$i++;   
}
  echo ("\n" . $counter . " Slots erstellt");

mysql_close();

【问题讨论】:

  • 如果你需要在数据库中插入多行,你应该使用 PHP 中的implode() 函数。 See.

标签: php mysql loops insert while-loop


【解决方案1】:

我不完全了解您要做什么以及什么不起作用,但是:

$slot("
 INSERT INTO 
    Lager (LagerNr,ReiheNr,PlatzNr,SlotNr,LagerArt,Stock)
 VALUES ('". $l ."','" . $r  . "','" . $i . "','". $n ."','" . $a . "','0')");

似乎缺少一个“=”。

【讨论】:

  • 哦,我的错字花了我一些时间,非常感谢!抱歉打错字了!
【解决方案2】:
 $slot("
 INSERT INTO 
    Lager (LagerNr,ReiheNr,PlatzNr,SlotNr,LagerArt,Stock)
 VALUES ('". $l ."','" . $r  . "','" . $i . "','". $n ."','" . $a . "','0')");
 mysql_query($slot) OR print(mysql_error());

应该是:

 $slot = 
 ("INSERT INTO 
    Lager (LagerNr,ReiheNr,PlatzNr,SlotNr,LagerArt,Stock)
 VALUES ('". $l ."','" . $r  . "','" . $i . "','". $n ."','" . $a . "','0')");
 mysql_query($slot) OR print(mysql_error());

【讨论】:

    【解决方案3】:

    $slot 后面没有“=”,但 $platz 有。

    【讨论】:

      猜你喜欢
      • 2019-01-21
      • 1970-01-01
      • 2015-12-22
      • 2020-05-13
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 2022-01-07
      • 2019-04-30
      相关资源
      最近更新 更多