我创建PHP多个图像更新,如果我选择的所有图像文件只更新第一和第二图像,我想创建一个由一个图像更新我试图做到这一点是我的剧本是只更新一个图像的第二图像更新页面,但它不工作
这里是我的代码
if($_SERVER['REQUEST_METHOD'] == "POST"){
//$id=$_POST['id'];
$title=$_POST['title'];
$desc1=$_POST['desc1'];
$content=$_POST['content'];
$category=$_POST['category'];
$random = substr(number_format(time() * rand(),0,'',''),0,10);
$img= $random .$_FILES['image']['name'][0];
$image_tmp= $_FILES['image']['tmp_name'][0];
$img1= $random .$_FILES['image']['name'][1];
$image_tmp1= $_FILES['image']['tmp_name'][1];
if(move_uploaded_file($image_tmp,"images/$img")){
move_uploaded_file($image_tmp1,"images/$img1");
$stmt = $con->prepare("UPDATE products SET title=?, desc1=?, content=?, category=?,img=?,img1=? WHERE id=?");
$stmt->bind_param("sssssss", $title, $desc1, $content, $category, $img,$img1, $id);
}else{
$stmt = $con->prepare("UPDATE products SET title=?, desc1=?, content=?, category=? WHERE id=?");
$stmt->bind_param("sssss", $title, $desc1, $content, $category, $id);
}
if($stmt->execute()){
header('location:edit-source.php');
}else{
echo "Failed to update product<br/>";
}
}
?>
我认为你在的地方有一个不正确的逻辑。逻辑应是
if($_SERVER['REQUEST_METHOD'] == "POST"){
$id=$_POST['id'];
$title=$_POST['title'];
$desc1=$_POST['desc1'];
$content=$_POST['content'];
$category=$_POST['category'];
$random = substr(number_format(time() * rand(),0,'',''),0,10);
$img= $random .$_FILES['image']['name'][0];
$image_tmp= $_FILES['image']['tmp_name'][0];
$img1= $random .$_FILES['image']['name'][1];
$image_tmp1= $_FILES['image']['tmp_name'][1];
if(move_uploaded_file($image_tmp,"images/$img")){
move_uploaded_file($image_tmp1,"images/$img1");
$stmt = $con->prepare("UPDATE products SET title=?, desc1=?, content=?, category=?,img=?,img1=? WHERE id=?");
$stmt->bind_param("sssssss", $title, $desc1, $content, $category, $img,$img1, $id);
} else if (move_uploaded_file($image_tmp1,"images/$img1")) {
move_uploaded_file($image_tmp1,"images/$img");
$stmt = $con->prepare("UPDATE products SET title=?, desc1=?, content=?, category=? WHERE id=?");
$stmt->bind_param("sssss", $title, $desc1, $content, $category, $id);
}
if($stmt->execute()){
header('location:edit-source.php');
}else{
echo "Failed to update product<br/>";
}
}
?>
另外,我建议它包裹在一个功能,使其可重复使用。