上传图像并将文件名插入MySQL

时间:2014-06-05 21:44:14

标签: php mysql

xxx如何修复此脚本,使其实际发布3个单独的图像而不是相同的图像3次。任何帮助将非常感激。我将提供代码html代码以包含相关内容。

HTML:

<form method="post" action="insert.php" enctype="multipart/form-data">
<input type="text" name="caseName"><br>
<input type="file" name="upload[]"/>
<input type="file" name="upload[]"/>
<input type="file" name="upload[]"/>
<input type="submit" value="Submit" >
</form>

PHP:

if ( isset( $_FILES['upload'] ) ) {
    $name_array = $_FILES['upload']['name'];
    $tmp_name_array = $_FILES['upload']['tmp_name'];
    for ( $i = 0; $i < count( $name_array ); $i++ ) {


            if ( move_uploaded_file( $tmp_name_array[$i], "uploaded/" . $name_array[$i]   ) ) {
                    echo $name_array[$i];
            } else {
                    echo "failed";
            }
    }
    $dsn = 'mysql:host=localhost;dbname=GLO12408958DB';
    $username = 'root';
    $password = 'yt987210d';
    //
    // DB connection was made
    //
    $pdo = new PDO($dsn, $username, $password);



    //loop over array to get names. Make sure we have actual content.
    if ( count( $name_array ) > 0 && $name_array !== false ) {

            //Prepare query
            $statement = $pdo->prepare( 'INSERT INTO caseStudies(caseImage,caseImage2,caseImage3) VALUES (?,?,?)' );

            //use a different index in the event that the numeric keys in the name array are not ordered correctly
            $index = 1;


            foreach ( $name_array as $key => $filename ) {

                    $statement->bindParam( $index, $filename, PDO::PARAM_STR );
                    $index++;
            }

            $statement->execute();

            //etc....
    }
}

1 个答案:

答案 0 :(得分:0)

根据manualbindParam()“将PHP变量绑定到用于准备语句的SQL语句中的相应命名或问号占位符。与PDOStatement :: bindValue()不同, 该变量被绑定为引用,并且仅在PDOStatement :: execute()被调用时进行评估。“