我需要每行插入多个带广告名称的图片。我无法插入广告名称。我无法修改php代码。插入多个图像没有问题。但是当尝试使用图像插入广告名称时会出现问题。 Plz帮助。
HTML
<form action="posting_ad_imagec.php" method="post" enctype="multipart/form-data">
Ad Title: <input type="text" id="ad_name" name="ad_name[]"/>
<input type="file" id="files" name="files[]" multiple="multiple" />
<input type="submit" name="submit" value="Submit">
</form>
PHP:
<?php
$dir='ad/data/img/';
if( isset( $_POST['submit'], $_FILES['files'] ) ) {
$uploaded=array();
$ad_names = $_POST["ad_name"];
$sql = 'insert into `full texts` set `img_name` = ?, `img_type` = ?, `img_size` = ?, `ad_name` = ?';
$stmt = $connection->prepare( $sql );
if( $stmt ){
$stmt->bind_param( 'ssss', $name, $type, $size, $ad_name);
foreach( $_FILES['files']['name'] as $i => $name ) {
if( !empty( $_FILES['files']['tmp_name'][$i] ) ) {
$name = $_FILES['files']['name'][$i];
$size = $_FILES['files']['size'][$i];
$type = $_FILES['files']['type'][$i];
$tmp = $_FILES['files']['tmp_name'][$i];
$ad_name = $ad_names[$i];
if( is_uploaded_file( $tmp ) ){
$bytes = move_uploaded_file( $tmp, $dir.$name );
if( $bytes ){
$status = $stmt->execute();
$uploaded[]=$status && $bytes ? $name : false;
}
}
}
}
if( !empty( $uploaded ) ){
$_SESSION['s']=sprintf("%d images successfully saved", count( $uploaded ) );
header('Location: posting_ad_image.php');
}
}
}
?>
答案 0 :(得分:0)
第1步:将name="ad_name[]"
更改为name="ad_name"
(不再使用括号)
第2步:将$ad_names = $_POST["ad_name"];
更改为$ad_name = $_POST["ad_name"]
(单数)
第3步:删除$ad_name = $ad_names[$i];
(不再需要)