我已经创建并验证了for循环的图片上传。我现在正尝试将这些值添加到数据库中,并将其他值先前存储在网站较早部分的会话中。PHP为循环添加数据库值
for循环
for($i=1; $i<=$items; $i++) {
$file_size = $_FILES["photo{$i}"]['size'];
$file_name = $_FILES["photo{$i}"]['name'];
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES["photo{$i}"]['tmp_name'];
$allowed = array('jpg', 'jpeg', 'gif', 'png');
if (in_array($file_extn, $allowed) === false) {
echo '<div class="alert alert-danger">[Photo '.$i.'] Format <strong>NOT</strong> acceptable. ';
echo 'Accepted formats: '. implode(', ', $allowed).'</div>';
} elseif($file_size > 1048576) {
echo '<div class="alert-danger">[Photo '.$i.'] Image is too big. Maximium file size is 1MB.</div>';
} else {
// add values to database
}
}
我这是怎么打算的所有值插入到数据库
$temp_data = array(
'name' => ucwords($_SESSION['name']),
'email' => strtolower($_SESSION['email']),
'delivery' => strtolower($_SESSION['delivery']),
'item' => sanitize($_GET['item']),
'price' => sanitize(($_GET['item'])*4.99),
'time_now' => date('Y-m-d H:i:s'),
'time_end' => date('Y-m-d H:i:s', strtotime("+15 day")),
'photo1' => $file_path
);
insert_temp($temp_data);
这是insert_temp
函数的代码。
function insert_temp($temp_data) {
array_walk($temp_data, 'array_sanitize');
$fields = '`' . implode('`, `', array_keys($temp_data)) . '`';
$data = '\'' . implode('\', \'', $temp_data) . '\'';
mysql_query("INSERT into `temp` ($fields) VALUES ($data)");
}
这就是问题所在。我想插入先前已存储为会话的值,并将图像存储在数据库的for循环中。我怎样才能做到这一点,而不是使用for循环一遍又一遍地将相同的值插入到数据库中?
提前许多感谢,哈利