试图在mysql中从while循环(php)中插入数据

试图在mysql中从while循环(php)中插入数据,php,mysql,Php,Mysql,我正在做一个项目,允许医生给病人增加治疗。我的项目中增加了一些基本治疗方法 当医生想要给患者增加一种特殊治疗时,他可以得到基本治疗,并添加一些额外的文本或更改日期(例如:患者可以在第4天而不是第5天回家) 问题是,当我想保存while循环时,只有最后一个条目会被保存。注意我不希望更新基本治疗,而是将其保存在另一个表中 当我看到以下代码时:这显示了医生可以编辑的基本治疗方法 <?php echo '<form action="" method="post">';

我正在做一个项目,允许医生给病人增加治疗。我的项目中增加了一些基本治疗方法

当医生想要给患者增加一种特殊治疗时,他可以得到基本治疗,并添加一些额外的文本或更改日期(例如:患者可以在第4天而不是第5天回家)

问题是,当我想保存while循环时,只有最后一个条目会被保存。注意我不希望更新基本治疗,而是将其保存在另一个表中

当我看到以下代码时:这显示了医生可以编辑的基本治疗方法

<?php 
    echo '<form action="" method="post">';  

    while ($topic = $resul -> fetch_assoc())
    {

        $datumNew = strtotime($datum);
        $stopdate_extra=$datumNew+$topic['Dag']*84600;
        $stopdate_Nu=date("l", $stopdate_extra);
        $stopdate_Nu1=date("d", $stopdate_extra);
        $stopdate_Nu1a=date("M", $stopdate_extra);
        $stopdate_Nu2=date("Y", $stopdate_extra);
        $stopdate_db=date("Y-m-d",$stopdate_extra);

        echo "<div class='tijdlineElement'>";                   
        echo "<div class='tijdlineP2Element' ><input type='text' name='bescValue' value='" .$topic['Uur'].  "'/><br /><br /><textarea  maxlength='400' style='width:100%; height:70px;' name='uurValue' rows='4'>" . $topic['Beschrijving']. "</textarea></div>";
        echo "<div class='tijdlinePElement' style='background-color: gray;'>".  $stopdate_Nu  . '<br/> '.  $stopdate_Nu1  .' ' .  $stopdate_Nu1a  . '<br/>  '.  $stopdate_Nu2  . ' '."</div>";
        echo "<input type='hidden' name='dagValue' value='" .$stopdate_db.  "'/>"; echo "</div><br />";
      }
     echo '<br/><br/>
    <input id="btnbehandeling" name="btnbehandeling" value="voegtoe" type="submit" class="button" style="width: 101%" />
    </form>';
使用voegbehandlingtoetijdslijn

Public function VoegBehandelingtoeTijdslijn(){

            include 'PHP/Connection.php';

        $sql= "INSERT INTO tijdslijn
            (
            Beschrijving,
            User,
            Dag,
            Uur
            ) 
            VALUES 
            (

            '" . $conn-> real_escape_string($this -> m_sBeschrijving) . "',
            '" . $conn-> real_escape_string($this -> m_sRRN) . "',
            '" . $conn-> real_escape_string($this -> m_sDag) . "',
            '" . $conn-> real_escape_string($this -> m_sUur) . "'
            );";

                    if (!$conn-> query($sql)) {
        throw new Exception("Fout bij registartie");
    }


}

如果我想添加有2行的治疗“测试”,它将只保存我的最后一行。因此,我的问题是:如何保存while循环中的所有回显行?

在循环外定义一个数组,并推送其中的每个项目,然后再次保存。当您像这样做时,只保存/存储循环的最后一项。因为您一直在覆盖上一项

伪代码

$saveddata = array();
loop{
//with one or two pieces of data.
array_push($saveddata, $data);
//with multiple pieces of data it is recommended to save it in an object first.
//and then add these objects to the array.
}

现在,找到的每个数据段都添加到此数组中。再次通过此数组卸载数据循环

是的,它就是这样做的,您在循环之外有一个表单,如果循环有更多的值,那么在提交时它将从DOM中选取最后的元素,您可以使用数组作为元素名
name='bescValue[]'
等,然后使用循环通过设置数组索引中的值来调用add方法。
$saveddata = array();
loop{
//with one or two pieces of data.
array_push($saveddata, $data);
//with multiple pieces of data it is recommended to save it in an object first.
//and then add these objects to the array.
}