-1
我有一個表單,允許我編輯數據庫中的數據。一旦表單提交,我需要 將圖像上傳到文件夾。這是一個編輯頁面,我設置的方式是從數據庫調用圖像名稱,並將圖像存儲在文件夾中。誰能幫我?PHP上傳圖像信息編輯
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<label> Job Name:</label><br><br>
<input type="text" name="job_name" value="<?php echo $name; ?>"/><br/>
<label></label><br><br>
<label>Job Thumbnail:</label><br><br>
<input id="upload" name="file" type="file" size="10"><br><br>
<input id="filename" type="text" name="job_timg" placeholder="This Fills Automatcially" value="<?php echo $timg; ?>"/><br/>
<label> Job Name:</label><br><br>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$name = mysql_real_escape_string(htmlspecialchars($_POST['job_name']));
$timg = mysql_real_escape_string(htmlspecialchars($_POST['job_timg']));
// check that name/image fields are both filled in
if ($name == '' || $timg == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($id, $name, $timg, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE tbl_job SET job_name='$name', job_timg='$timg' WHERE job_id='$id'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: gallery_edit.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM tbl_job WHERE job_id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$name = $row['job_name'];
$timg = $row['job_timg'];
// show form
renderForm($id, $name, $timg, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>
處理文件需要使用有效的加密類型表單標籤內 –
謝謝工作! Connor, –
不客氣。爲了解決這個問題,我決定在下面發表一個可以接受的答案。這樣,它也會告訴其他人找到了解決方案。 –