doutany76678 2012-03-19 04:12 采纳率: 0%
浏览 307
已采纳

MySQL更新某些数据库字段而不覆盖未更改的字段

Ok, this one is driving me nuts. I have a backend file uploader that uploads .jpg files to the server. Then I want to upload the filename(s) of the .jpgs to my database. So when the page loads I can add the filename from the database and the pictures will display on the page. This works fine, but I also need to be able to update the files and the filenames in the database. If the user changes all the files and file names everything is fine. But if the user wishes to change only one or two file(s) and filename(s) the MySql update statement ends up having some of the variables empty thereby effectively deleting the existing filenames in the record instead of leaving them alone. As usual I have searched stackoverflow and google before asking for help and I have not found anything that is really pertinent. Here is the applicable code.

<?php 
 session_start();
 $id = $_SESSION['id'];

 //This is the directory where images will be saved 
 $target = "imgs/"; 
// "http://www.surfcup.com/travel_site/images/ ";

 $targetlogo = $target . basename( $_FILES['imageLogo']['name']);
 $targetpic1 = $target . basename( $_FILES['image1']['name']);
 $targetpic2 = $target . basename( $_FILES['image2']['name']);
 $targetpic3 = $target . basename( $_FILES['image3']['name']);
 $targetpic4 = $target . basename( $_FILES['image4']['name']);
 $targetpic5 = $target . basename( $_FILES['image5']['name']);

 //This gets all the other information from the form 

 $logo=($_FILES['imageLogo']['name']); 
 $pic1=($_FILES['image1']['name']); 
 $pic2=($_FILES['image2']['name']); 
 $pic3=($_FILES['image3']['name']); 
 $pic4=($_FILES['image4']['name']); 
 $pic5=($_FILES['image5']['name']); 


 // Connects to Database 
 mysql_connect("localhost", "surfcup_HotAdmin","password") or die ('I cannot connect to        the database because: ' .mysql_error());
 mysql_select_db("surfcup_hotels") or die('I cannot connect to the database because: .mysql_error());

 $query="UPDATE Hotels
 SET 
 hotel.imageLogo = '".$logo."',
 hotel.image1 = '".$pic1."',
 hotel.image2 = '".$pic1."',
 hotel.image3 ='".$pic1."',
 hotel.image4 = '".$pic1."',
 hotel.image5 = '".$pic1."'
 WHERE Hotels.id='".$id."'";


 mysql_query($query) or die ('Error Updating Hotel '.mysql_error());

//stuff to upload the files below



?>

I think I either need to check if the variables are null and somehow not up load them or stop the database from accepting null entries. The later though would make the user have to add 6 files when he/she creates a record. What if they only had 5 or 3? I can't seem to get my head around how I would check if the variables are null and only upload the ones with filenames in them in the UPLOAD statement. Thanks again, in advance, for all your help. Dave

  • 写回答

2条回答 默认 最新

  • duanhuang2150 2012-03-19 04:33
    关注

    You can try this. Basically it checks if the value is empty. If it is not, then it adds the value to the array. At the end we implode the array into a string that we will add to the your query. In the example I only did a few of the images, but you should get the point. It should work barring syntax errors in my code.

    Although I will say that this is not the best way to do it. You can definitely improve on this and make it more secure and efficient.

    $uploaded_images = array();
    
    if(!empty($logo)){
       $uploaded_images[] = "hotel.imageLogo = '".$logo."'";
    }
    
    if(!empty($pic1)){
       $uploaded_images[] = "hotel.image1 = '".$pic1."'";
    }
    
    if(!empty($pic2)){
       $uploaded_images[] = "hotel.image2 = '".$pic2."'";
    }
    
    $values_to_set = implode(', ', $uploaded_images);
    $query= "UPDATE Hotels SET " . $values_to_set . " WHERE Hotels.id='" . $id . "'";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况