问题描述
我有以下html提交表单.这种形式是PHP_SELF,并将输入数据作为数组存储在$ _POST中.
I have the following html submit form. This form is a PHP_SELF and stores the input data as an array in $_POST.
<html>
<body>
<form method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<fieldset>
<legend>Direct Bill Information:</legend>
DB #: <input type="int" name="db_num[]" /> DB Amount: <input type="int" name="db_amnt[]" /> </br>
DB #: <input type="int" name="db_num[]" /> DB Amount: <input type="int" name="db_amnt[]" /> </br>
DB #: <input type="int" name="db_num[]" /> DB Amount: <input type="int" name="db_amnt[]" /> </br>
DB #: <input type="int" name="db_num[]" /> DB Amount: <input type="int" name="db_amnt[]" /> </br>
DB #: <input type="int" name="db_num[]" /> DB Amount: <input type="int" name="db_amnt[]" /> </br>
DB #: <input type="int" name="db_num[]" /> DB Amount: <input type="int" name="db_amnt[]" /> </br>
DB #: <input type="int" name="db_num[]" /> DB Amount: <input type="int" name="db_amnt[]" /> </br>
DB #: <input type="int" name="db_num[]" /> DB Amount: <input type="int" name="db_amnt[]" /> </br>
DB #: <input type="int" name="db_num[]" /> DB Amount: <input type="int" name="db_amnt[]" /> </br>
DB #: <input type="int" name="db_num[]" /> DB Amount: <input type="int" name="db_amnt[]" />
</fieldset>
<input type="submit" value="submit" name="submit" />
</form>
</body>
</html>
我有一个具有表'db_num'和'db_amnt'的MySQL表,我想将数组中的所有输入数据插入表中.我环顾四周,爆破"似乎是最好的方法,但是经过大量研究,我仍然无法使它起作用.整个PHP的执行和完成没有错误,但是,它没有显示两列的所有10行,而是仅显示了两列中的2行,所有条目的值均为0.这是我正在使用的代码:
I have a MySQL table that has the fields 'db_num' and 'db_amnt', and I want to insert all the input data in the array into the table. I've looked around and 'implode' seems to be the best way, but after much research I still can't get it to work. The entire PHP executes and finishes without error, however, instead of displaying all 10 rows of both columns, it just displays 2 rows of the two columns with values = 0 for all entries. This is the code I'm using:
$sql2 = array();
foreach($_POST as key=>$value)
{
if(key != 'submit')
{
if($value != '')
{
$sql2[] = '("'.mysql_real_escape_string($value['db_num']).'", "'.$value['db_amnt'].'")';
}
}
}
mysql_query('INSERT INTO ' .$tablename_db. '(db_num, db_amnt) VALUES '.implode(',', $sql2));
我做了一些调试,并在foreach循环中回显了$ value,结果它只显示了三个术语; 数组",数组"和提交".进一步的检查表明,我可以从提交表单中获取值的唯一方法是是否通过$ _POST [db_num]和$ _POST [db_amnt]而不是仅通过$ _POST进行foreach循环.我不太确定为什么要这样做以及如何解决它.这是我的第一个PHP代码,正在学习中.我非常感谢我所能提供的任何帮助!
I did a little debugging and echoed the $value in the foreach loop, and it turns out that it's only displaying three terms; 'Array', 'Array', and 'submit'. Further checks showed that the only way I can get the values from the submit form to show is if I do a foreach loop through $_POST[db_num] and $_POST[db_amnt] instead of just $_POST. I'm not really sure why it's doing this and how to fix it. This is my first code in PHP and I'm learning as I go. I really appreciate any help that I can get!
推荐答案
尝试一下
$db_num = $_POST['db_num'];
$db_amnt = $_POST['db_amnt'];
$items = array_combine($db_num,$db_amnt);
$pairs = array();
foreach($items as $key=>$value){
$pairs[] = '('.intval($key).','.intval($value).')';
}
mysql_query('INSERT INTO ' .$tablename_db. '(db_num, db_amnt) VALUES '.implode(',',$pairs));
这篇关于使用Implode将HTML表单$ _POST数组插入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!