我正在创建一个表单,将图像插入滑块,最多10张幻灯片。插入图像时,我需要选择可能只选择2,3或4张幻灯片。但是当我尝试插入少于10个图像时,它不会插入到数据库中。
这是我到目前为止所拥有的;
try {
//$max_size = 2097152; This is to do, not part of question
$allowed = array('jpg', 'jpeg', 'png');
$img_1 = $_FILES['slider_1']['name'];
$extn = strtolower(end(explode('.', $img_1)));
$temp = $_FILES['slider_1']['tmp_name'];
if(in_array($extn, $allowed) === true) {
$slider_1 = 'data/' . md5(time().uniqid()) . '.' . $extn;
move_uploaded_file($temp, $slider_1);
}
//This works upto this point
$insert = DB::getInstance()->insert('slider', array(
'main' => $main,
'slider_1' => $slider_1,
'slider_2' => $slider_2,
'slider_3' => $slider_3,
'slider_4' => $slider_4,
'slider_5' => $slider_5,
'slider_6' => $slider_6,
'slider_7' => $slider_7,
'slider_8' => $slider_8,
'slider_9' => $slider_9,
'slider_10' => $slider_10
));
Session::flash('success', '<p class="success">Inserted successfully</p>');
Redirect::to('test.php');
} catch(Exception $e) {
die($e->getMessage());
}
这是我的插入功能
public function insert($table, $fields = array()) {
$keys = array_keys($fields);
$values = null;
$x = 1;
foreach($fields as $value) {
$values .= "?";
if($x < count($fields)) {
$values .= ', ';
}
$x++;
}
$sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})";
if(!$this->query($sql, $fields)->error()) {
return true;
}
return false;
}
这是我的表格
<form id="contact-form" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data" autocomplete="off">
<label for="main"><span class="required">*</span> Main Image:</label>
<input type="file" name="main">
<label for="img_set_1"><span class="required">*</span> Image Set 1:</label>
<input type="file" name="slider_1">
<label for="img_set_2"><span class="required">*</span> Image Set 2:</label>
<input type="file" name="slider_2">
<label for="img_set_3"><span class="required">*</span> Image Set 3:</label>
<input type="file" name="slider_3">
<label for="img_set_4"><span class="required">*</span> Image Set 4:</label>
<input type="file" name="slider_4">
<label for="img_set_5"><span class="required">*</span> Image Set 5:</label>
<input type="file" name="slider_5">
<label for="img_set_6"><span class="required">*</span> Image Set 6:</label>
<input type="file" name="slider_6">
<label for="img_set_7"><span class="required">*</span> Image Set 7:</label>
<input type="file" name="slider_7">
<label for="img_set_8"><span class="required">*</span> Image Set 8:</label>
<input type="file" name="slider_8">
<label for="img_set_9"><span class="required">*</span> Image Set 9:</label>
<input type="file" name="slider_9">
<label for="img_set_10"><span class="required">*</span> Image Set 10:</label>
<input type="file" name="slider_10">
<button type="submit">SUBMIT</button>
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</form>
提前致谢