How to update form data in database with and without updating image using php, mysql

6mastermeb

New Member
Below code is to update form details in database. It would update database only if image is changed/upload amother image. What should be done if we don't want to upload new image but need to update other form details? Thanks for solution, in advance!!! \[code\]<html> <head> <title>Update the Contact Record</title> <link rel="stylesheet" type="text/css" href="http://stackoverflow.com/questions/15849965/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="http://stackoverflow.com/questions/15849965/<? 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="http://stackoverflow.com/questions/15849965/<? echo"$Row[1]" ?>"></td> </tr> <tr> <td>TelephoneNo</td> <td><input type="text" name="TelephoneNo" size="25" value="http://stackoverflow.com/questions/15849965/<? echo"$Row[2]"?>"></td> </tr> <tr> <td>Email</td> <td><input type="text" name="Email" size="25" value="http://stackoverflow.com/questions/15849965/<? echo"$Row[3]"?>"></td> </tr> <tr> <td>WEYears</td> <td><input type="text" name="WEYears" size="25" value="http://stackoverflow.com/questions/15849965/<? echo"$Row[4]"?>"></td> </tr> <tr> <td>CurrentLocation</td> <td><input type="text" name="CurrentLocation" size="25" value="http://stackoverflow.com/questions/15849965/<? echo"$Row[5]"?>"></td> </tr> <tr> <td>PreferredLocation</td> <td><input type="text" name="PreferredLocation" size="25" value="http://stackoverflow.com/questions/15849965/<? echo"$Row[6]"?>"></td> </tr> <tr> <td>CurrentEmployer</td> <td><input type="text" name="CurrentEmployer" size="25" value="http://stackoverflow.com/questions/15849965/<? echo"$Row[7]"?>"></td> </tr> <tr> <td>CurrentDesignation</td> <td><input type="text" name="CurrentDesignation" size="25" value="http://stackoverflow.com/questions/15849965/<? echo"$Row[8]"?>"></td> </tr> <tr> <td>AnnualSalary</td> <td><input type="text" name="AnnualSalary" size="25" value="http://stackoverflow.com/questions/15849965/<? echo"$Row[9]"?>"></td> </tr> <tr> <td>UGCourse</td> <td><input type="text" name="UGCourse" size="25" value="http://stackoverflow.com/questions/15849965/<? 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="http://stackoverflow.com/questions/15849965/10000000" />Change Image:</td> <td><input name="userfile" type="file" /></td> </tr> <tr> <td align="center"><input type="submit" name="submitvalue" value="http://stackoverflow.com/questions/15849965/UPDATE" ></td> <td align="center"><input type="button" name="cancelvalue" value="http://stackoverflow.com/questions/15849965/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 =http://stackoverflow.com/questions/15849965/addslashes(file_get_contents($_FILES['userfile']['tmp_name'])); $sql= "UPDATE data SET NameoftheCandidate=\"$_POST[NameoftheCandidate]\", TelephoneNo='$_POST[TelephoneNo]', Email='$_POST', 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>\[/code\]
 
Back
Top