下面的代码是更新数据库中的表单详细信息。只有在图像被更改/上传其他图像时才会更新数据库。如果我们不想上传新图片但需要更新其他表格细节,该怎么办?谢谢你的解决方案,提前!!!
<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 :(得分:0)
在您的php脚本中包含以下代码
if ($_FILES["userfile"]["error"] == 0)
{
//update database code goes here
}
答案 1 :(得分:0)
为旧图像创建一个隐藏字段,并使用表单中数据库中的值进行填充。
$图像= $ _ FILES [ “新图像-FIELD”] [ “名称”];
if($ image!=“”){
SCRIPT TO UPLOAD NEW IMAGE
SCRIPT TO REMOVE OLD IMAGE
}
}
else
{
$image = $_REQUEST["HIDDEN-OLD-IMAGE-FIELD"];
}
$query = UPDATE DATABASE DETAIL WITH image='$image'";
答案 2 :(得分:0)
您好我已经为此完成了解决方案。之前添加以下代码 if($ _ FILES ['userfile'] ['error'] == 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";
}