我的情况是这样的:

<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input type="file" name="image[]" />


我的数据库字段表是:

ID = 1
image1 = image1.jpg
image2 = image2.jpg
image3 = image3.jpg
image4 = image4.jpg


假设行id = 1之前已经插入了值,并且如果用户要更新image1和image 4,那么他们将浏览并选择所需的图像文件,并且将image2和image3留为空白。

这是执行mysql查询后的概念:

ID = 1
image1 = newimage1.jpg
image2 = image2.jpg
image3 = image3.jpg
image4 = newimage4.jpg


我该如何进行验证查询?

目前我的代码是这样的,由于我还是PHP / mysql的新手,所以我不知道如何从这里继续。

foreach ($_FILES['product_image']['name'] as $key => $value) {
if(!empty($value)) {
                `$upt=mysql_query("update products_list set` `image1='$pic1',image2='$pic2',image3='$pic3',image4='$pic4' where id='$id'");`
}
}


希望这对你们很清楚。提前致谢。 =)

最佳答案

您可以使用计数器来更新正确的字段:

 foreach ($_FILES['product_image']['name'] as $key => $value) {
   if(!empty($value)) {
        // field names starts from 1
        $field = 'image'.($key+1);
        mysql_query("UPDATE product_list ".
                    "SET $field = '$value' ".
                    "WHERE id = $id");
   }
 }


当然,这将意味着您必须将同一条记录最多更新四次,具体取决于表单发送的内容。另外,您应该使用mysql_escape_string()之类的符号来避开用户输入。

10-04 22:52
查看更多