Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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/MSSQL;根据数组索引的数据是否存在插入记录_Php_Sql_Sql Server - Fatal编程技术网

PHP/MSSQL;根据数组索引的数据是否存在插入记录

PHP/MSSQL;根据数组索引的数据是否存在插入记录,php,sql,sql-server,Php,Sql,Sql Server,我有一个PHP表单,其结构如下: <input type="text" name="code[]" id="code[]" value="<?php echo $code[4] ?>" /> <input type="text" name="period[]" id="period[]" value="<?php echo $period[4]; ?>" /> 两者都不为空(如果其中一个可以为空,只需将&&更改为|): for($i=0;$ipr

我有一个PHP表单,其结构如下:

<input type="text" name="code[]" id="code[]" value="<?php echo $code[4] ?>" />
<input type="text" name="period[]" id="period[]" value="<?php echo $period[4]; ?>" />
两者都不为空(如果其中一个可以为空,只需将
&&
更改为
|
):

for($i=0;$iprepare(“插入表(代码,周期)值(:code,:周期)”);
$data=array('code'=>$\U POST['code'][$i],'period'=>$\U POST['period'][$i]);
$new->execute($data);
}
}

如果您只想从表单中插入非空行,只需检查是否有填写的内容:

<?php for($i=0; $i<=4; $i++) {
    if(isset($_POST['code'][$i]) && $_POST['code'][$i] != '' && 
       isset($_POST['period'][$i]) && $_POST['period'][$i] != ''){
          $new = $con->prepare("INSERT INTO table (code, period)
          VALUES(:code, :period)");
          $data = array('code'=>$_POST['code'][$i],'period'=>$_POST['period'][$i]);
          $new->execute($data);
    }
} 
?>


请注意,如果发送空字段,则它们已设置(isset返回true),但它们是空字符串

无论是
code
还是
period
都不应为空?或者一个空字段是可以的?这两个字段都是必需的,因此它们要么都必须为空,要么都必须填写。
for($i=0; $i<=4; $i++) {
    if(!empty($_POST['code'][$i]) && !empty($_POST['period'][$i]) {
        $new = $con->prepare("INSERT INTO table (code, period) VALUES(:code, :period)");
        $data = array('code'=>$_POST['code'][$i],'period'=>$_POST['period'][$i]);
        $new->execute($data);
    }
}
<?php for($i=0; $i<=4; $i++) {
    if(isset($_POST['code'][$i]) && $_POST['code'][$i] != '' && 
       isset($_POST['period'][$i]) && $_POST['period'][$i] != ''){
          $new = $con->prepare("INSERT INTO table (code, period)
          VALUES(:code, :period)");
          $data = array('code'=>$_POST['code'][$i],'period'=>$_POST['period'][$i]);
          $new->execute($data);
    }
} 
?>