2013-04-06 104 views
0

下面的代碼是更新數據庫中的表單詳細信息。它只會在圖像更改/上傳圖像時更新數據庫。如果我們不想上傳新圖片,但需要更新其他表格詳細信息,應該做些什麼?感謝解決方案,提前!如何使用php,mysql更新數據庫中的表單數據以及不更新圖像

<html> 

    <head> 
     <title>Update the Contact Record</title> 
     <link rel="stylesheet" type="text/css" href="cms_style.css">  
<script> 

window.onunload = function(){ window.opener.location.reload(); }; 

</script> 


    </head> 

    <body> 
     <h2 align="center">Update the Record</h2> 

    <?php 

     // error_reporting(~E_NOTICE); 


      //echo "Test 1 <br>"; 
      $cid = $_GET['id']; 
      $uid = $_GET['uid']; 

     /* echo "<br> value of Con ID is : "; echo $cid; 
      echo "<br> value of UID is : "; echo $uid; */ 



     if($uid==1) { 
      //echo "<br> Test 2 "; 
      updateRecord($cid); 



     } else if (isset($_GET['id'])) { 
      //echo "<br>Test 3 "; 

      $ResumeID = $_GET['id']; 
      $sql="SELECT * from data WHERE ResumeID=$ResumeID"; 
      $result = mysql_query($sql); 
      $Row=mysql_fetch_row($result); 


    ?> 
      <form align="center" action="updateRecord.php?id=<? echo "$Row[0]"?>&uid=1" method="post" enctype="multipart/form-data"> 
       <table align="center"> 
        <input type="hidden" name="resumeid" value="<? echo "$Row[0]"?>"> 

       <!-- <? echo "<tr> <td> Resume ID </td> <td>$Row[0]</td> </tr>" ?> -->      

        <div align="center"> 
         <tr>  
          <td> Name of the Candidate</td> 
          <td><input type="text" name="NameoftheCandidate" size="25" value="<? echo "$Row[1]" ?>"></td> 
         </tr> 

         <tr> 
          <td>TelephoneNo</td> 
          <td><input type="text" name="TelephoneNo" size="25" value="<? echo "$Row[2]"?>"></td> 
         </tr> 

         <tr>  
          <td>Email</td> 
          <td><input type="text" name="Email" size="25" value="<? echo "$Row[3]"?>"></td> 
         </tr> 

         <tr> 
          <td>WEYears</td> 
          <td><input type="text" name="WEYears" size="25" value="<? echo "$Row[4]"?>"></td> 
         </tr> 

         <tr> 
          <td>CurrentLocation</td> 
          <td><input type="text" name="CurrentLocation" size="25" value="<? echo "$Row[5]"?>"></td> 
         </tr> 

         <tr> 
          <td>PreferredLocation</td> 
          <td><input type="text" name="PreferredLocation" size="25" value="<? echo "$Row[6]"?>"></td> 
         </tr> 

         <tr> 
          <td>CurrentEmployer</td> 
          <td><input type="text" name="CurrentEmployer" size="25" value="<? echo "$Row[7]"?>"></td> 
         </tr> 

         <tr> 
          <td>CurrentDesignation</td> 
          <td><input type="text" name="CurrentDesignation" size="25" value="<? echo "$Row[8]"?>"></td> 
         </tr> 

         <tr> 
          <td>AnnualSalary</td> 
          <td><input type="text" name="AnnualSalary" size="25" value="<? echo "$Row[9]"?>"></td> 
         </tr> 

         <tr> 
          <td>UGCourse</td> 
          <td><input type="text" name="UGCourse" size="25" value="<? echo "$Row[10]"?>"></td> 
         </tr> 

         <tr> 
          <td> Image: 
          <? echo $Row[12]; ?> </td> 

         </tr> 

         <tr> 

          <? echo '<td><img src="http://localhost/cmsapp_latest/processimage.php?id=' . $Row[0] . '"></td>'; ?> 
         </tr> 

         <tr> 
         <td><input type="hidden" name="MAX_FILE_SIZE" value="10000000" />Change Image:</td> 
         <td><input name="userfile" type="file" /></td> 
         </tr> 

         <tr> 
          <td align="center"><input type="submit" name="submitvalue" value="UPDATE" ></td> 
          <td align="center"><input type="button" name="cancelvalue" value="CANCEL" onClick="self.close(); return false;"></td> 
         </tr> 



        </div>        
       </table> 
      </form> 




    <?php 
     } // end of else if 

    function updateRecord($cid) { 

     $msg = "Intial Value"; 
     $maxsize = 10000000; //set to approx 10 MB 





         if($_FILES['userfile']['error']== UPLOAD_ERR_OK) { 
          echo "Print uplod error - "; 
          echo UPLOAD_ERR_OK; 

          //check whether file is uploaded with HTTP POST 
          if(is_uploaded_file($_FILES['userfile']['tmp_name'])) { 

          echo "tEST 02 - "; 
           //checks size of uploaded image on server side 
           if($_FILES['userfile']['size'] < $maxsize) { 

            $finfo = finfo_open(FILEINFO_MIME); 




            if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0)  { 

            echo "tEST 03 - "; 

             // prepare the image for insertion 
             $imgData =addslashes(file_get_contents($_FILES['userfile']['tmp_name'])); 

            $sql= "UPDATE data SET NameoftheCandidate=\"$_POST[NameoftheCandidate]\", TelephoneNo='$_POST[TelephoneNo]', Email='$_POST[Email]', WEYears='$_POST[WEYears]',CurrentLocation='$_POST[CurrentLocation]', PreferredLocation='$_POST[PreferredLocation]', CurrentEmployer=\"$_POST[CurrentEmployer]\", CurrentDesignation='$_POST[CurrentDesignation]', AnnualSalary='$_POST[AnnualSalary]', UGCourse=\"$_POST[UGCourse]\", image=\"{$imgData}\", name=\"{$_FILES['userfile']['name']}\" WHERE ResumeID=$_GET[id]"; 

            //echo $sql; 

            //$result = mysql_query($sql); 

            if(mysql_query($sql)) 

            echo "Record updated"; 

            else 
             echo "Record update failed"; 

            } 
            else 
             $msg="<p>Uploaded file is not an image.</p>"; 


           } 








           else { 
            // if the file is not less than the maximum allowed, print an error 
            $msg='<div>File exceeds the Maximum File limit</div> 
            <div>Maximum File limit is '.$maxsize.' bytes</div> 
            <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size']. 
            ' bytes</div><hr />'; 
            } 
          } 
          else 
           $msg="File not uploaded successfully."; 

         } 
         else { 

          $msg= file_upload_error_message($_FILES['userfile']['error']); 

         } 
         return $msg; 









     } // end of update function 


    function file_upload_error_message($error_code) { 
     switch ($error_code) { 
      case UPLOAD_ERR_INI_SIZE: 
       return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; 
      case UPLOAD_ERR_FORM_SIZE: 
       return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; 
      case UPLOAD_ERR_PARTIAL: 
       return 'The uploaded file was only partially uploaded'; 
      case UPLOAD_ERR_NO_FILE: 
       return 'No file was uploaded'; 
      case UPLOAD_ERR_NO_TMP_DIR: 
       return 'Missing a temporary folder'; 
      case UPLOAD_ERR_CANT_WRITE: 
       return 'Failed to write file to disk'; 
      case UPLOAD_ERR_EXTENSION: 
       return 'File upload stopped by extension'; 
      default: 
       return 'Unknown upload error'; 
     } 




    } 


    ?> 



    </body> 
</html> 

回答

0

包括下面的代碼在你的PHP腳本

if ($_FILES["userfile"]["error"] == 0) 
{ 
//update database code goes here 

} 
0

創建舊圖像隱藏字段,並與數據庫中的表單值填充。

$ image = $ _ FILES [「NEW-IMAGE-FIELD」] [「name」];

如果($形象!= 「」){

 SCRIPT TO UPLOAD NEW IMAGE 
       SCRIPT TO REMOVE OLD IMAGE 
    } 
} 
else 
{ 
    $image = $_REQUEST["HIDDEN-OLD-IMAGE-FIELD"]; 
} 
$query = UPDATE DATABASE DETAIL WITH image='$image'"; 
+0

嗨,我已經爲此做了解決方案。 – trendzcreator 2013-04-08 06:42:49

0

您好我已經做了this.Just解決 前添加以下代碼,如果($ _ FILES [ 'userfile的'] [ '錯誤'] = = UPLOAD_ERR_OK)

  if($_FILES['userfile']['error']== UPLOAD_ERR_NO_FILE) 
    { 
     //echo UPLOAD_ERR_NO_FILE; 

     SQL update query  
     //echo $sql; 

     if(mysql_query($sql)) 

         echo "Record updated"; 

        else 
         echo "Record update failed"; 

    }