简体   繁体   English

如何从一个字段更改的表单数据中在MySQL中输入多行

[英]How to enter multiple rows in mySQL from form data where one field changes

I've got a form which asks some basic questions about a job, and then asks to person entering to list all those colleagues that were with them at the time. 我有一个表格,询问有关工作的一些基本问题,然后要求进入的人员列出当时与他们在一起的所有同事。 What I want to do is for all colleagues present to have a row entered in to the database - so all the other details remain the same except for their work_id (which is the identifier for the person). 我要为在场的所有同事在数据库中输入一行-因此所有其他细节都保持不变,除了他们的work_id(这是该人的标识符)之外。 My code below seems to submit 1 row to the db with the workID field empty 我的下面的代码似乎将1行提交到数据库,而workID字段为空

<?php
require_once('includes/dbconnect.php');

$varWorkID = $_POST['workid'];
$varDate = $_POST['date'];
$varType = $_POST['type'];
$varSuper = $_POST['supervisor'];
$varReference = $_POST['reference'];

//Remove last part of array as extra 1 sent through by form 
$workID = array_pop($varWorkID);

for ($i=0; $i < count($workID); $i++ )
{
    mysqli_query($conn,"INSERT INTO searches (workid,date,type,super,reference) VALUES('".$workID."','".$varDate."','".$varType."','".$varSuper."','".$varReference."')");
}
echo "Completed";

I think I'm fairly close but I just need to get workid to be populated with each of the Work IDs for each of the employees present. 我想我已经很接近了,但是我只需要获取工作ID即可为在场的每位员工填写每个工作ID。

Any assistance greatly appreciated 任何帮助表示赞赏

the problem here is that you're calling $workID = array_pop($varWorkID); 这里的问题是您正在调用$workID = array_pop($varWorkID); before you go into the loop for ($i=0; $i < count($workID); $i++ ) , so the PHP interpreter thinks you just want to iterate over the length of that one element. 在进入for ($i=0; $i < count($workID); $i++ )循环之前,PHP解释器认为您只想遍历该元素的长度。 what you actually wanted to do was pop an element off the array while inside the loop. 您真正想要做的是在循环从数组中弹出一个元素。

<?php
require_once('includes/dbconnect.php');

$varWorkID = $_POST['workid'];
$varDate = $_POST['date'];
$varType = $_POST['type'];
$varSuper = $_POST['supervisor'];
$varReference = $_POST['reference'];


// iterate over the count of the whole array $varWorkID = $_POST['workid']
for ($i=0; $i < count($varWorkID); $i++ )
{
    // now pop off the array inside the loop
    //Remove last part of array as extra 1 sent through by form 
    $workID = array_pop($varWorkID);
    mysqli_query($conn,"INSERT INTO searches (workid,date,type,super,reference) VALUES('".$workID."','".$varDate."','".$varType."','".$varSuper."','".$varReference."')");
}
echo "Completed";

now I think @Dima Fitiskin's comment proposes a better approach because you don't really need the expense of calling another function array_pop or throwing another variable on the stack $workID when you've already got an iterated reference to the index $i inside the loop. 现在我认为@Dima Fitiskin的评论提出了一种更好的方法,因为当您已经有一个对索引$i的迭代引用时,您实际上并不需要花费调用另一个函数array_pop或在$workID堆栈上抛出另一个变量的开销。环。

<?php
require_once('includes/dbconnect.php');

$varWorkID = $_POST['workid'];
$varDate = $_POST['date'];
$varType = $_POST['type'];
$varSuper = $_POST['supervisor'];
$varReference = $_POST['reference'];


// iterate over the count of the whole array $varWorkID = $_POST['workid']
for ($i=0; $i < count($varWorkID); $i++ )
{
    mysqli_query($conn,"INSERT INTO searches (workid,date,type,super,reference) VALUES('".$varWorkID[$i]."','".$varDate."','".$varType."','".$varSuper."','".$varReference."')");
}
echo "Completed";

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

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