将多个上载文件的文件名存储到MySQL数据库中

时间:2011-05-31 15:10:22

标签: php mysql forms

好的,我接触到了这一刻,我只打印了第一张提交的图像!我该如何做到不受限制,以便我的foreach可以 INSERT 所有这些?

 <table width="400">
<form action="" method="post" enctype="multipart/form-data" >
<p>Pictures:
<input type="file" name="pictures[]"  />
<input type="file" name="pictures[]"  />

<input type="submit" value="Send" />
<input type="hidden" value="<?php echo $row_Rs_maxID    ['MAX(projectos_ID)']; ?>" name="idz" id="idz" />
</p>
</form>

<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "uploads/$name");
    }
}
foreach($_FILES as $file)
    {

            #upload deu certo
            $query = mysql_query("INSERT INTO projectoimagens (projecto_fk, image)
                                  VALUES (, '".$_FILES["pictures"]["name"][$key]."')");
        }


?>

1 个答案:

答案 0 :(得分:4)

你的问题措辞非常尴尬。不确定这是不是你想要的:

mysql_connect('localhost', 'user', 'pass') or trigger_error('Unable to connect to MySQL: ' . mysql_error());
mysql_select_db('database_name') or trigger_error('Unable to select DB: ' . mysql_error());

foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "uploads/$name");
        mysql_query("INSERT INTO table_name (col1, col2, col3) VALUES('something', 'somethingelse', 'uploads/" . mysql_real_escape_string($name) . "');") or trigger_error('Unable to INsert: ' . mysql_error()); 
    }
}
?>

这可能就是你要找的东西。您将不得不对其进行广泛修改以匹配您的数据库信息,但希望能够将您带到您想去的地方。