我有一个包含6个文本输入的表单,供用户添加一些链接并添加它们做数据库。这6个输入中的每一个都将数据插入不同的行。这是我的代码:
public function insertImages($img1,$img2,$img3,$img4,$img5,$img6){
$myDb = $this->_controlPanel->getMyDb();
$query = "INSERT INTO galeria (img) VALUES ('$img1'), ('$img2'),('$img3'), ('$img4'),('$img5'), ('$img6')";
$result = $myDb->performQuery($query);
if (!$result) {
die('Something went wrong, try again: ' . mysql_error());
header( "Refresh:3; url=insertnot.php");
}
else {
header( "Refresh:1; url=admin.php");
}
}
和
if(!empty($_POST)){
$img1 = $_POST['img1'];
$img2 = $_POST['img2'];
$img3 = $_POST['img3'];
$img4 = $_POST['img4'];
$img5 = $_POST['img5'];
$img6 = $_POST['img6'];
try{
$log = new classes_UserManager($myControlPanel);
$insert = $log->insertImagens($img1,$img2,$img3,$img4,$img5,$img6);
}
catch (invalidArgumentException $e){
$e->getMessage();
}
}
?>
<div class="container">
<h2 style="color:#666; margin-top:15vh; text-align:center;"> Inserir Imagens </h2>
<form style="margin-top:10vh;" name="img" method="POST" action="">
<div class="row">
<div class="col-md-4">
<input placeholder="Imagem 1" class="form-control" type="text" name="img1" id="title" >
</div>
<div class="col-md-4">
<input placeholder="Imagem 2" class="form-control" type="text" name="img2" id="title" >
</div>
<div class="col-md-4">
<input placeholder="Imagem 3" class="form-control" type="text" name="img3" id="title" >
</div>
</div>
<br>
<br>
<div class="row">
<div class="col-md-4">
<input placeholder="Imagem 4" class="form-control" type="text" name="img4" id="title" >
</div>
<div class="col-md-4">
<input placeholder="Imagem 5" class="form-control" type="text" name="img5" id="title" >
</div>
<div class="col-md-4">
<input placeholder="Imagem 6" class="form-control" type="text" name="img6" id="title" >
</div>
</div>
<br>
<br>
<input type="submit" style="width:100%; margin:0 auto;" class="btn btn-primary form-control" name="submit" id="submit">
这样做效果很好,但是当用户只填充两三个输入时,如何避免输入空行?
答案 0 :(得分:0)
类似的东西:
if(!empty($_POST)) {
$images = array();
for ($i = 1; $i <= 6; $i++) {
if (!empty($_POST['img'.$i]))
$images[] = $_POST['img'.$i];
}
if (sizeof($images) > 0)
$insert = $log->insertImages($images);
...
}
和
public function insertImages($images) {
$myDb = $this->_controlPanel->getMyDb();
$imgStr = '';
foreach($images as $k => $v) {
$imgStr += "('$v'),";
}
$imgStr = rtrim($imgStr, ','); // remove trailing comma
$query = "INSERT INTO galeria (img) VALUES $imgStr";
...
}
答案 1 :(得分:0)
使用&#34; power&#34;连接。您的问题解决方案很少。
让我们考虑以下功能:
<?php
// get all non-empty fields for POST that
// have index like img<number>
function getNonEmptyValues ($inputs , $input_prefix, $inputs_count = 7)
{
$nonempty_inputs = array();
$prefix = isset($input_prefix) ? $input_prefix : '';
// you have to be sure each of your inputs
// is indexed as range 1..N
// with some prefix
for ($i = 1; $i < $inputs_count; $i++)
{
$value = isset($inputs[$prefix.$i]) ? $inputs[$prefix.$i] : '';
if(strlen($value))
{
$nonempty_inputs []= $value;
}
}
return $nonempty_inputs;
}
// get all non-empty fields for POST that
// have index like img<number>
// but ignore all fields after 1 missing,
// e.g. : img1, img2, img3 return array of length 3
// while img1, img2, img4 will return array of length 2
function getInOrder($inputs, $input_prefix, $inputs_count = 7)
{
$nonempty_inputs = array();
$prefix = isset($input_prefix) ? $input_prefix : '';
// you have to be sure each of your inputs
// is indexed as range 1..N
// with some prefix
for ($i = 1; $i < $inputs_count; $i++)
{
$value = isset($inputs[$prefix.$i]) ? $inputs[$prefix.$i] : '';
if(strlen($value))
{
$nonempty_inputs []= $value;
}
else
{
break;
}
}
return $nonempty_inputs;
}
function buildInsertQuery ($inputs) {
if (count($inputs) === 0) {
throw new \Exception("Missing inputs. Cannot build query.");
}
$query = "INSERT INTO galeria (img) VALUES ";
$insert_values = [];
foreach( $inputs as $value ) {
$insert_values []= "('".$value."')";
}
return $query . implode(",", $insert_values) . ";";
}
$_our_post = array(
"img1" => "kittenz",
"img2" => "doge",
"img4" => "me gusta"
);
$my_data = getNonEmptyValues($_our_post, "img");
echo buildInsertQuery($my_data);
echo "\n";
$my_data = getInOrder($_our_post, "img");
echo buildInsertQuery($my_data);
echo "\n";