我有一个表单,允许我编辑数据库中的数据。我需要 提交表单后将图像上载到文件夹。这是一个编辑页面,我设置的方式是从数据库调用图像名称,图像存储在文件夹中。任何人都可以帮助我吗?
<?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!';
}
}
?>
答案 0 :(得分:0)
处理文件时,表单必须包含有效的enctype。
<form action="" method="post" enctype="multipart/form-data">
有关处理文件的更多信息,请访问PHP.net上的以下页面: