用标题插入多个图像



我需要在每行插入带有广告名称的多个图像。我无法插入广告名称。我无法修改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');
            }
        }
    }
?>

步骤1:将 name="ad_name[]"更改为 name="ad_name"(无括号)
步骤2:将$ad_names = $_POST["ad_name"];更改为$ad_name = $_POST["ad_name"](单数)
步骤3:垃圾$ad_name = $ad_names[$i];(不再需要)

最新更新