0
xxx如何修復此腳本,以便它實際上發佈3個獨立的圖片而不是3次相同的圖片。任何幫助將非常感激。我將提供代碼html代碼來包裝相關的代碼。上傳圖片並將文件名插入到MySQL
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....
}
}
現在您已經將它發佈到互聯網上,最好更改您的數據庫密碼。 – tadman