插入多行而不插入空白sql php

时间:2014-12-03 22:13:21

标签: php html sql forms pdo

我有三个输入框,我想在一个名为volunteer_id,position_id和memo的表格/表格中重复十次。我不想编写10个sql查询,我想重用相同的语句或循环它(我知道它是坏的)所以它读取10个集合中的每一个并在每个集合中插入一个新行。我还想允许用户输入1到10组,所以如果他们只输入2组,那么它不会插入另外8个空白行,但仍然提交他们输入的2个。如果这种情况不明确,我会道歉,我已经抓了好几个星期,到处寻找,最后发生故障并张贴在这里。

所以,我想要完成的事情: 标记每个输入,以便可以重复使用10次 提交最多10条记录的插入,而不是空白

以下是我当前代码的一小部分,其中包含大部分相关信息。

<INPUT TYPE="text" NAME="volunteer_id" SIZE="7" value="volunteer_id"> <br/>
<INPUT TYPE="text" NAME="position_id" SIZE="7" value="position_id"> <br />
<TEXTAREA name="memo" cols="45" rows="2">Memo</TEXTAREA> <br />

$volunteer_id = $_POST['volunteer_id'];
$memo = $_POST['memo'];
$position_id = $_POST['position_id'];

$query = "INSERT INTO work(volunteer_id, memo, position_id) VALUES (:volunteer_id, :memo, :position_id)";

$q = $dbo->prepare($query);
$q->execute(array('':volunteer_id'=>$volunteer_id, ':memo'=>$memo, ':position_id'=>$position_id));

1 个答案:

答案 0 :(得分:3)

首先,PHP不能与具有相同名称的GET / POST变量进行精彩处理,每个变量都会覆盖之前的变量,因此您需要以下内容:

<label>Volunteer: <input type="text" name="volunteer_id[0]" size="7" /></label>
<label>Position: <input type="text" name="position_id[0]" size="7" /></label>
<label>Memo: <textarea name="memo[0]" cols="45" rows="2"></textarea></label>

这些应该按照收到的顺序编入索引,但为了安全起见,在生成HTML的代码中,您应该将索引添加到名称中(例如volunteer_id[0]) - 空<textarea>数据没有发布,所以你需要确保它存在正确的索引,否则你最终可能会得到没有与志愿者/职位对齐的备忘录。

您可以循环一个函数来创建输出;当PHP收到时,我会在$_POST中给你3个数组(我假设你的表单方法是“post”),如下所示:

//these will all be arrays
$aVolunteers = $_POST['volunteer_id'];
$aPositions = $_POST['position_id'];
$aMemos = $_POST['memo'];

接下来,构建查询:

//how many volunteers are there
// - text input fields are always posted even when empty
//   so counting volunteers should tell us how many data sets we've got
$iNumSets = count($aVolunteers);

//begin the query
$sQuery = "INSERT INTO work(volunteer_id, position_id, memo) VALUES";

//create an array of parameters to bind
$aBoundParams = array();

//loop to the total number of data sets
for($i = 0; $i < $iNumSets; $i++) {

  //append the "values" to the query string
  $sQuery .= "(?, ?, ?), ";

  //add the values to the bound parameters array
  //  assumes the database fields are NULLable
  $aBoundParams[] = !empty($aVolunteers[$i]) ? $aVolunteers[$i] : null;
  $aBoundParams[] = !empty($aPositions[$i]) ? $aPositions[$i] : null;

  //this one is the most important one to check !empty()
  //as it's possible $aMemos[$i] doesn't exist
  $aBoundParams[] = !empty($aMemos[$i]) ? $aMemos[$i] : null;
}

//trim the trailing ", "
$sQuery = substr($sQuery, 0, -2);

如果您的数据集已发布3次,那么现在应该有一个类似的查询:

INSERT INTO work(volunteer_id, position_id, memo) VALUES(?, ?, ?), (?, ?, ?), (?, ?, ?)

一个长度为9的数组,如下所示:

array(
  $aVolunteers[0],
  $aPositions[0],
  $aMemos[0],
  $aVolunteers[1],
  $aPositions[1],
  $aMemos[1],
  $aVolunteers[2],
  $aPositions[2],
  $aMemos[2]
)

您现在应该能够准备查询,绑定参数/值并执行它。

//prepare the statement
$oStmt = $dbo->prepare($sQuery);

//bind the parameters to the statement
//  parameters need to be bound by reference - note &$vParamValue
foreach($aBoundParams as $iParamIndex => &$vParamValue) {

  $oStmt->bindParam(

            //bound params/values are 1 indexed rather than 0
            ($iParamIndex + 1), 

            //value to bind
            $vParamValue,

            //basic variable type prep - assuming you're using PDO
            (is_int($vParamValue) ? PDO::PARAM_INT : PDO::PARAM_STR)

            );
}

//execute the statement
$oStmt->execute();