我正在制作Update表单,一切都很好,但是现在我遇到了一个问题,那就是如果我更新标题但在提交图像时仍然不更新图像,则在更新表单中消失了,但是当我更新图像时它就是更新了像当我不更新图像时,它变成空白如何解决此问题?
这是我的代码:
<?php
include('includes/db.php');
if (isset($_GET['edit'])) {
$edit_id=$_GET['edit'];
$get_edit="select * from mobile_phone where mobile_id = '$edit_id'";
$run_edit=mysqli_query($con,$get_edit);
$row_edit=mysqli_fetch_array($run_edit);
$update_id=$row_edit['mobile_id'];
$m_title=$row_edit['mobile_name'];
$brand_id=$row_edit['brand_id'];
$m_image1=$row_edit['img1'];
$m_image2=$row_edit['img2'];
$m_image3=$row_edit['img3'];
$m_price=$row_edit['price'];
$m_desc=$row_edit['mobile_desc'];
$m_key=$row_edit['product_keywords'];
}
// exact brand
$get_brand="select * from brands where brand_id='$brand_id'";
$run_brand=mysqli_query($con,$get_brand);
$brand_row=mysqli_fetch_array($run_brand);
$brand_edit=$brand_row['brand_title'];
?>
这是html表单的一部分
<tr>
<td style="color:#fff"><b>Mobile Image 1</b></td>
<td style="color:#fff"> <input type="file" name="Mimg1"><br><img src="mobile_images/<?php echo $m_image1; ?>" width="80" height="100"> </td>
</tr>
<tr>
<td style="color:#fff"><b>Mobile Image 2</b></td>
<td style="color:#fff"> <input type="file" name="Mimg2"><br><img src="mobile_images/<?php echo $m_image2; ?>" width="80" height="100"> </td>
</tr>
<tr>
<td style="color:#fff"><b>Mobile Image 3</b></td>
<td style="color:#fff"><input type="file" name="Mimg3" ><br><img src="mobile_images/<?php echo $m_image3; ?>" width="80" height="100"> </td>
</tr>
<tr align="center">
<td colspan="2"> <input type="submit" class="btn" name="update_mobile" value= "Update"></td>
</tr>
// php部分
if(isset($_POST['update_mobile'])){
//text data variables
$mobile_title=$_POST['Mname'];
$mobile_price=$_POST['Mprice'];
$mobile_brand=$_POST['Mbrand'];
$mobile_desc=$_POST['mdesc'];
$mobile_keywords = $_POST['key'];
//image names
$mobile_img1=$_FILES['Mimg1']['name'];
$mobile_img2=$_FILES['Mimg2']['name'];
$mobile_img3=$_FILES['Mimg3']['name'];
//Image temporary names
$temp_name1=$_FILES['Mimg1']['tmp_name'];
$temp_name2=$_FILES['Mimg2']['tmp_name'];
$temp_name3=$_FILES['Mimg3']['tmp_name'];
if($mobile_title=='' OR $mobile_brand=='' OR $mobile_price=='' OR $mobile_desc=='' ){
echo"<script>alert('please fill all the fields!')</script>";
exit();
}
else{
//uploading image to its folder
move_uploaded_file($temp_name1, "mobile_images/$mobile_img1");
move_uploaded_file($temp_name2, "mobile_images/$mobile_img2");
move_uploaded_file($temp_name3, "mobile_images/$mobile_img3");
$update_mobile= "update mobile_phone set mobile_name='$mobile_title',brand_id='$mobile_brand',date=NOW(),price='$mobile_price',img1='$mobile_img1',img2='$mobile_img2',img3='$mobile_img3',mobile_desc='$mobile_desc',product_keywords='$mobile_keywords' where mobile_id='$update_id'";
$run_update= mysqli_query($con,$update_mobile);
if($run_update){
echo "<script>alert('mobile updated successfully')</script>";
echo "<script>window.open('index.php?view','_self')</script>";
}
}
}
?>
答案 0 :(得分:0)
您需要检查是否从每个输入中选择了某些图像。即使用户未选择某些图片,您也正在更新:
这是您可以尝试的代码:
if(isset($_POST['update_mobile'])){
//text data variables
$mobile_title=$_POST['Mname'];
$mobile_price=$_POST['Mprice'];
$mobile_brand=$_POST['Mbrand'];
$mobile_desc=$_POST['mdesc'];
$mobile_keywords = $_POST['key'];
if($mobile_title=='' OR $mobile_brand=='' OR $mobile_price=='' OR $mobile_desc=='' ){
echo"<script>alert('please fill all the fields!')</script>";
exit();
}
else{
// Declare the columns that always will be updated
$queryUpdateFields = "mobile_name='$mobile_title',brand_id='$mobile_brand',date=NOW(),price='$mobile_price',mobile_desc='$mobile_desc',product_keywords='$mobile_keywords'"
// check if the images exist
if ($_FILES['Mimg1']['size'] != 0) {
$temp_name1=$_FILES['Mimg1']['tmp_name'];
$mobile_img1=$_FILES['Mimg1']['name'];
move_uploaded_file($temp_name1, "mobile_images/$mobile_img1");
$queryUpdateFields .= ", img1='$mobile_img1'"; // image exists, so, concatenate to update
}
if ($_FILES['Mimg2']['size'] != 0) {
$temp_name2=$_FILES['Mimg2']['tmp_name'];
$mobile_img2=$_FILES['Mimg2']['name'];
move_uploaded_file($temp_name2, "mobile_images/$mobile_img2");
$queryUpdateFields .= ", img2='$mobile_img2'";
}
if ($_FILES['Mimg3']['size'] != 0) {
$temp_name3=$_FILES['Mimg3']['tmp_name'];
$mobile_img3=$_FILES['Mimg3']['name'];
move_uploaded_file($temp_name3, "mobile_images/$mobile_img3");
$queryUpdateFields .= ", img3='$mobile_img3'";
}
$update_mobile= "update mobile_phone set " . $queryUpdateFields . " where mobile_id='$update_id'";
$run_update= mysqli_query($con,$update_mobile);
if($run_update){
echo "<script>alert('mobile updated successfully')</script>";
echo "<script>window.open('index.php?view','_self')</script>";
}
}
}
我没有测试。您可以获取逻辑并在不起作用时应用。让我知道发生了什么事。