Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从表单向数据库表插入多行_Php_Mysql_Sql - Fatal编程技术网

Php 从表单向数据库表插入多行

Php 从表单向数据库表插入多行,php,mysql,sql,Php,Mysql,Sql,我有一张清单,经理们可以填写。在底部,他们输入一份材料清单,完成后员工可以在清单上签字。材料的数量可以长达20多行。以下是他们填写的行所包含的内容: <tr><td><button type="button" class="addRow">Add Row</button></td></tr> <tr><th>WO# / Date <br/>(if added

我有一张清单,经理们可以填写。在底部,他们输入一份材料清单,完成后员工可以在清单上签字。材料的数量可以长达20多行。以下是他们填写的行所包含的内容:

        <tr><td><button type="button" class="addRow">Add Row</button></td></tr>
        <tr><th>WO# / Date <br/>(if added or changed after Revision 1)<th>Component Name and Number</th><th>Finish Sizes</th><th>Material</th><th>Total # Pieces</th><th>Work Order</th><th>Notes</th><th>Work Order</th><th>Notes</th><th>Work Order</th><th>Notes</th></tr>
        <tr>
            <td><input type="text" name='wo_num_and_date'/></td>
            <td><input type="text" name='comp_name_and_num'/></td>
            <td><input type="text" name='finish_sizes'/></td>
            <td><input type="text" name='material'/></td>
            <td><input type="text" name='total_num_pieces'/></td>
            <td><input type="text" name='workorder_num_one'/></td>
            <td><textarea rows="2" cols="12" name='notes_one'></textarea></td>
            <td><input type="text" name='workorder_num_two'/></td>
            <td><textarea rows="2" cols="12" name='notes_two'></textarea></td>
            <td><input type="text" name='workorder_num_three'/></td>
            <td><textarea rows="2" cols="12" name='notes_three'></textarea></td>
        </tr>
我如何做到无论它们添加多少行,它都会将它们全部插入表中,而不仅仅是第一行

尝试Huseyin的答案后,VAR转储:

array (size=17)
  'submit' => string 'Submit!' (length=7)
  'posted_date' => string '2014-04-07' (length=10)
  'category' => string '1' (length=1)
  'revision' => string '4' (length=1)
  'revisionDate' => string '2014-04-07' (length=10)
  'per_wo_num' => string '2' (length=1)
  'wo_num_and_date' => string 'WO#5/2013-04-04' (length=15)
  'comp_name_and_num' => string 'Lift 2' (length=6)
  'finish_sizes' => string '2x2x2' (length=5)
  'material' => string 'P20' (length=3)
  'total_num_pieces' => string '1' (length=1)
  'workorder_num_one' => string '1' (length=1)
  'notes_one' => string 'OK' (length=2)
  'workorder_num_two' => string '2' (length=1)
  'notes_two' => string 'OK' (length=2)
  'workorder_num_three' => string '3' (length=1)
  'notes_three' => string 'NOT OK' (length=6) 

您可以在表单中使用数组符号化名称,并实现以下功能:

<tr>
    <td><input type="text" name='wo_num_and_date[]'/></td>
    <td><input type="text" name='comp_name_and_num[]'/></td>
    <td><input type="text" name='finish_sizes[]'/></td>
    <td><input type="text" name='material[]'/></td>
    <td><input type="text" name='total_num_pieces[]'/></td>
    <td><input type="text" name='workorder_num_one[]'/></td>
    <td><textarea rows="2" cols="12" name='notes_one[]'></textarea></td>
    <td><input type="text" name='workorder_num_two[]'/></td>
    <td><textarea rows="2" cols="12" name='notes_two[]'></textarea></td>
    <td><input type="text" name='workorder_num_three[]'/></td>
    <td><textarea rows="2" cols="12" name='notes_three[]'></textarea></td>
</tr>
您应该删除
类似于

*您的代码:(插入检查表修订版)*

VALUES ($job_num,
        $loggedin_id,
        $revision,
        $category,
        STR_TO_DATE('$posted_date', '%Y-%m-%d'), -- -> you should not give like this you should mention the `colname`
        $per_wo_num);";
 NULL,
    '$workorder_num_three',
    '$notes_three',
    NULL);";
您的更改应该如下所示:

*您的代码:(插入检查表\u组件\u库存)*

VALUES ($job_num,
        $loggedin_id,
        $revision,
        $category,
        STR_TO_DATE('$posted_date', '%Y-%m-%d'), -- -> you should not give like this you should mention the `colname`
        $per_wo_num);";
 NULL,
    '$workorder_num_three',
    '$notes_three',
    NULL);";
您应该这样更改:


下面是一个简单的例子:

<form action="" method='post'>
    <input type="text" name="myData[row1]" value='aaa'>
    <input type="text" name="myData[row2]" value='sss'>
    <input type="text" name="myData[row3]" value='ddd'>

    <input type="submit" name="submit">
</form>

if(isset($_POST['submit']))
{
    $array = $_POST['myData'];

    echo "<pre>";
    echo print_r($array);
    echo "</pre>";

    //insert 3 times,
    foreach ($array as $key => $value) {
        $sql = "INSERT INTO table1 (info) VALUES('$value')";
    }
}

OUTPUT:
Array
(
    [row1] => aaa
    [row2] => sss
    [row3] => ddd
)

如果(isset($_POST['submit']))
{
$array=$_POST['myData'];
回声“;
回波打印(阵列);
回声“;
//插入3次,
foreach($key=>$value的数组){
$sql=“插入表1(信息)值(“$value”)”;
}
}
输出:
排列
(
[row1]=>aaa
[row2]=>sss
[row3]=>ddd
)

您的代码对sql注入非常开放@AleksG是100000%正确的。网络就像有安全漏洞的瑞士奶酪,请不要添加到其中。使用一个框架,这样可以确保安全。你永远不知道“Johnny先生删除所有表”什么时候决定使用你的表单。@我知道,但这是一段代码,我假设有更多潜在的漏洞(或者代码将来可能会增长,产生更多的漏洞),框架会从一开始就解决这个问题。建立一个轻量级的框架需要几分钟的时间,这样可以节省宝贵的时间。@MikeOscarEcho我很感激你是新手,但我知道你在制造安全漏洞,这会让你的公司付出代价。您需要从一个安全的方法开始(不要在它正常工作时立即保护它,因为没有时间这样做)。当你得到一个原型的时候,管理层将把你转移到下一件事上,你的测试代码将成为生产代码,你将在你的职业生涯中支持它。我不是有意要拖累你,但请务必意识到,从正确的心态开始是成功的一半。@MikeOscarEcho从来没有足够的时间;)。听着,伙计,我已经学会了艰难的道路(这里的大多数人也一样),你希望你能回到过去,告诉自己以不同的方式去做。我想是活到老学到老,但有关管理的评论远比你想象的真实。原型=在管理层眼中发布候选人,记住这一点。我希望我有!如果你的回答是评论的话,那就太好了。因为OP希望动态插入多行。你的答案有错issue@HüseyinBABAL,哪里是打字错误我的意思是,你应该删除
。类型或语法问题。但是,OP希望向db插入多个动态行。我遇到了错误,我应该将for循环的结束括号放在哪里?在第24行的/home/toolplas/public_html/main/phplib/add_checklist.php中,为foreach()提供的参数无效,注意:未定义的变量:第75行/home/toolplas/public_html/main/phplib/add_checklist.php中的sql2,警告:mysqli_query():第7行/home/toolplas/public_html/main/phplib/add_checklist.php中的空查询我忘记添加结束括号。我现在正在分析你的错误;请您将
var\u dump($\u POST)
置于if条件下,以确定发送的表单名称。您是否已将
[]
置于我在回答中所述的表单元素名称之后?看起来你的html表单名称还是一样的。
 NULL,
    '$workorder_num_three',
    '$notes_three',
    NULL)";
<form action="" method='post'>
    <input type="text" name="myData[row1]" value='aaa'>
    <input type="text" name="myData[row2]" value='sss'>
    <input type="text" name="myData[row3]" value='ddd'>

    <input type="submit" name="submit">
</form>

if(isset($_POST['submit']))
{
    $array = $_POST['myData'];

    echo "<pre>";
    echo print_r($array);
    echo "</pre>";

    //insert 3 times,
    foreach ($array as $key => $value) {
        $sql = "INSERT INTO table1 (info) VALUES('$value')";
    }
}

OUTPUT:
Array
(
    [row1] => aaa
    [row2] => sss
    [row3] => ddd
)