[英]Inserting form values in multiple row using php empty fields
I need a little help. 我需要一点帮助。 I have the following code.
我有以下代码。 I wish that when all fields are not completed, not to introduce the empty fields in the database.
我希望当所有字段都未完成时,不要在数据库中引入空字段。 Just upload the completed them.
只需上传完成的文件即可。
for($i = 0; $i <count($_POST)+3; $i++){
mysql_query("INSERT INTO `predare`(`id_factura`,`id_student`, `id_produs`, `cantitate`)
VALUES('".$_POST['id_factura'][$i]."','".$_POST['id_student'][$i]."', '".$_POST['id_produs'][$i]."', '".$_POST['cantitate'][$i]."');");
}
<input class="input-1" name="cantitate[]" id="cantitate" value="">
<input class="input-1" name="id_factura[]" id="id_factura" value="">
<input class="input-1" name="id_student[]" id="id_student" value="">
<input class="input-1" name="id_produs[]" id="id_produs" value="">
Here's how I would suggest doing it. 这是我建议这样做的方式。 First initialize your SQL string and a separator (for inserting multiple records with one query.)
首先初始化您的SQL字符串和分隔符(用于通过一个查询插入多个记录。)
$sql = "INSERT INTO `predare` (`id_factura`,`id_student`, `id_produs`, `cantitate`) VALUES ";
$separator = '';
Then loop over the submitted rows and append to the SQL string if the rows have all four values. 然后在提交的行上循环,如果行具有所有四个值,则追加到SQL字符串。
for ($i = 0; $i < count($_POST['cantitate']); $i++) {
$values = array();
foreach (array('id_factura', 'id_student', 'id_produs', 'cantitate') as $column) {
// only add values to the $values array if something has been entered
// (empty fields will be present as '', which evaluates to false)
if ($_POST[$column][$i]) $values[] = $_POST[$column][$i];
}
if (count($values) == 4) { // only add the values to the SQL if all fields are present
$sql .= "$separator ('" . implode("','", $values) . "')";
$separator = ',';
}
}
Then you can execute the SQL statement to insert all the complete records at once. 然后,您可以执行SQL语句一次插入所有完整记录。
If you wanted to include incomplete (but not empty) rows, you could use if ($values) {...
instead of if (count($values) == 4) {...
如果要包括不完整(但不为空)的行,则可以使用
if ($values) {...
代替if (count($values) == 4) {...
One benefit to setting it up this way is that it would be easier to convert the code to use a better database extension (such as PDO) with prepared statements instead of concatenating the values into the SQL like this. 以这种方式设置它的好处是,将代码转换为使用带有预准备语句的更好的数据库扩展(例如PDO)会更容易,而不是像这样将值串联到SQL中。
You need to go through the $_POST['id_factura'] array and You need to check the values before the insert statement like this: 您需要遍历$ _POST ['id_factura']数组,并且需要在插入语句之前检查值,如下所示:
for($i = 0; $i <count($_POST['id_factura']); $i++){
if(isset($_POST['id_factura'][$i]) && isset($_POST['id_student'][$i]) && isset($_POST['id_produs'][$i]) && isset($_POST['cantitate'][$i])){
mysql_query("INSERT INTO `predare`(`id_factura`,`id_student`, `id_produs`, `cantitate`)
VALUES('".$_POST['id_factura'][$i]."','".$_POST['id_student'][$i]."', '".$_POST['id_produs'][$i]."', '".$_POST['cantitate'][$i]."');");
}
}
Or you can check if the integer values are greater then 0... 或者您可以检查整数值是否大于0 ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.