【问题标题】:Insert multiple rows without inserting blanks sql php插入多行而不插入空格sql php
【发布时间】:2015-02-01 15:55:42
【问题描述】:

我有三个输入框,我想在一个表单/表格中重复十次,分别称为志愿者 ID、位置 ID 和备忘录。无需编写 10 个 sql 查询,我想重用相同的语句或循环它(我知道这很糟糕),因此它读取 10 个集合中的每一个,并在每个集合中插入一个新行。我还想允许用户输入 1 到 10 组,例如,如果他们只输入 2 组,它不会再插入 8 个空白行,但仍会提交他们输入的 2 行。如果这是口齿不清,我深表歉意,我已经为此苦苦琢磨了好几个星期,到处看了看,最后还是崩溃了,发到这里。

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

这是我当前代码的 sn-p,其中包含大部分相关信息。

<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));

【问题讨论】:

    标签: php html sql forms pdo


    【解决方案1】:

    首先,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]) - 空的 &lt;textarea&gt; 数据不会得到已发布,因此您需要确保它存在正确的索引,否则您最终可能会收到与志愿者/职位不相符的备忘录。

    你可以循环一个函数来创建你的输出;当 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();
    

    【讨论】:

    • 我相信name="foobar[]" 也适用于构建数组,避免显式索引。
    • 显然我的准备和执行有问题,我将您的第二个和第三个代码块添加到我的代码中,并将索引添加到我的志愿者 ID [0] 等,但现在出现无效参数错误。 $q = $dbo-&gt;prepare($sQuery); $q-&gt;execute(array(':volunteer_id'=&gt;$aVolunteers,':memo'=&gt;$aMemos, ':position_id'=&gt;$aPositions));
    • @user4032063 我们现在不使用命名参数,而是使用索引参数 - 尽管今天早上我确实发现索引参数从 1 而不是 0 开始所以我需要稍微调整答案......它当我写这篇文章时,昨晚有点晚了,来自我所在的世界。
    • @AndreaFaulds 带有 HTML 输入,尽管您 想要 显式索引 - &lt;textarea&gt; 数据如果为空则不会在 POST 中传递,因此您可以在此处了解情况,如果有 5 个数据集进来,而 4th memo 为空,则 5th memo 将成为 4th memo,而 如果您不使用显式索引,第 5 份备忘录 将是空的(而不是第 4 份)......如果这有意义:)
    猜你喜欢
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 2013-02-01
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多