使用2个文本字段将多个图像上载到数据库

时间:2012-04-14 15:49:08

标签: php mysql html image file-upload

好吧,我有一个两难的境地。我希望能够将最多六个图像和两个文本字段(标题和desc)上传到数据库...实际上让我自己纠正,我想在数据库中存储图像的名称,这样我就可以得到照片后来。我知道如何将多个图片上传到文件夹,我知道如何将行插入数据库,但我无法弄清楚如何将两者结合起来。如何将其与图像工作结合起来,如果其中一个文件出现问题,则取消整个过程?

我的数据库设置只是id |标题| desc | img1 | img2 | img3 | img4 | img5 | img6

我到目前为止写的代码是:

if (isset($_POST['formsubmitted'])) { //if form was submitted
$error = array();//Declare An Array to store any error message  

if (empty($_POST['title'])) {//if no name has been supplied 
    $error[] = 'Please enter a title for your post.';//add to array "error"
    $show_errors = 'show';
} else {
    $title = $_POST['title'];//else assign it a variable
}



if (empty($_POST['desc'])) {
    $error[] = 'Please enter a short desc of your post.';//add to array "error"
    $show_errors = 'show';
} else {
    $desc = $_POST['desc'];//else assign it a variable
}



    if (empty($error)){ //if no error, insert into db

$new_post = "INSERT INTO `posts` ( `title`, `desc`) VALUES ( '$title', '$desc')";
    $result = mysql_query($new_post ) or die(mysql_error('error inserting post'));

}


}

然后html是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php if (isset($show_errors)) { 
    //show user the errors if form cant be submitted
    echo '<div> <ol>';
        foreach ($error as $key => $values) { echo '    <li>'.$values.'</li>'; }
        echo '</ol></div><br />'; }?>

<br />
<form method="post" id="newpost" action="" enctype="multipart/form-data">
<div><input name="title" type="text" value="" class="title_input"></div>
<div><textarea id="area4" cols="40" rows="5" name="desc" class="desc_texbox"></textarea></div>
<div><input type="file" name="images1"></div>
<div><input type="file" name="images2"></div>
<div><input type="file" name="images3"></div>
<div><input type="file" name="images4"></div>
<div><input type="file" name="images5"></div>
<div><input type="file" name="images6"></div>
<input type="hidden" name="formsubmitted" value="TRUE" />
<input type="submit" id="upload" value="Upload">
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

这绝不是完成但它应该让你朝着正确的方向前进。请注意,我已根据更改的文件字段重命名为<input type="file" name="images[1]" />来完成此操作,以便将它们作为数组进行处理。

<?php

error_reporting(E_ALL);
ini_set('display_errors', true);

$db = new PDO('mysql:dbname=test;host=127.0.0.1', 'user', 'pass');


if (isset($_POST['formsubmitted'])) { //if form was submitted

    $error_array = array();//Declare An Array to store any error message

    // check all images for upload errors
    // you should probably add extra file validation here - check image type etc
    $upload_error = false;
    foreach($_FILES['images']['error'] as $error) {
        if ($error != 0 && $error != 4) {
            $upload_error = true;
        }
    }

    if ($upload_error) {
        $error_array[] = 'One of the image uploads failed!';
    }


    if (empty($_POST['title'])) {//if no name has been supplied
        $error_array[] = 'Please enter a title for your post.';//add to array "error"
    } else {
        $title = $_POST['title'];//else assign it a variable
    }

    if (empty($_POST['desc'])) {
        $error_array[] = 'Please enter a short desc of your post.';//add to array "error"
    } else {
        $desc = $_POST['desc'];//else assign it a variable
    }

    if (empty($error_array)){ //if no error, insert into db

        $new_post = "INSERT INTO `posts` ( `title`, `desc`) VALUES (?, ?)";
        $stmt = $db->prepare($new_post);
        $stmt->execute(array($title, $desc));

        $new_post_id = $db->lastInsertId();

        // now start processing the images
        $image_sql = "INSERT INTO `post_images` (`post_id`, `img_name`) VALUES (?, ?)";
        $stmt = $db->prepare($image_sql);

        for ($i = 1; $i <= 6; $i++) {

            // you need to add some code to vlaidate, move and rename the files

            // add the files to the db
            $file_name = $_FILES['images']['name'][$i];
            $stmt->execute(array($new_post_id, $file_name));

        }

    } else {
        print_r($error_array);
    }

}
?>

答案 1 :(得分:0)

我认为更好,首先在服务器上传图像,如果move_uploaded_file()函数返回错误,在$ error数组中添加一个值。  这样,只有在上传成功完成后,数据才会插入数据库中。