html - How to update form data in database with and without updating image using php, mysql -
below code update form details in database. update database if image changed/upload amother image. should done if don't want upload new image need update other form details? solution, in advance!!!
<html> <head> <title>update 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 record</h2> <?php // error_reporting(~e_notice); //echo "test 1 <br>"; $cid = $_get['id']; $uid = $_get['uid']; /* echo "<br> value of con id : "; echo $cid; echo "<br> value of uid : "; 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 * data 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 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 approx 10 mb if($_files['userfile']['error']== upload_err_ok) { echo "print uplod error - "; echo upload_err_ok; //check whether file uploaded 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 image 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']}\" 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 not image.</p>"; } else { // if file not less maximum allowed, print error $msg='<div>file exceeds maximum file limit</div> <div>maximum file limit '.$maxsize.' bytes</div> <div>file '.$_files['userfile']['name'].' '.$_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 upload_max_filesize directive in php.ini'; case upload_err_form_size: return 'the uploaded file exceeds max_file_size directive specified in html form'; case upload_err_partial: return 'the uploaded file partially uploaded'; case upload_err_no_file: return 'no file uploaded'; case upload_err_no_tmp_dir: return 'missing temporary folder'; case upload_err_cant_write: return 'failed write file disk'; case upload_err_extension: return 'file upload stopped extension'; default: return 'unknown upload error'; } } ?> </body> </html>
create hidden field old image , populate value database in form.
$image=$_files["new-image-field"]["name"];
if($image!="") {
script upload new image script remove old image } } else { $image = $_request["hidden-old-image-field"]; } $query = update database detail image='$image'";
Comments
Post a Comment