嗨大家我有一个带有各种字段的mysql dbase,其中一个将文件/ URL存储到图像位置。用户可以在需要时通过预填充特定记录数据的php更新表单来更新/编辑dbase,并重新提交更改。
一切正常,除非用户将“上传图片”表单字段留空,然后他们会收到错误“查询为空”。
上传图片字段留空的原因是因为它已在dbase上为该记录创建了一个图片,并希望保留该图片,但仍会更新任何其他所需数据。
此外,在预先填充更新表单时,它无法预填充图像上载字段,我认为这是出于安全目的,因此如果用户不浏览选择图像并将该字段留空,则会擦除dbase上的现有图像最终处理表格时。
目前,我使用验证来查找空表单字段,并指出用户返回并上传图像。
我不知道如何在不使用验证强制用户重新上传他们可能不知道该怎么做甚至不想知道的图像的情况下克服这个问题。
任何想法?
<?php
// get variables from form post for which record to change, and what to do with it
$action = $_GET['action'];
$id = $_GET['id'];
// THE DELETE OPTION
if($action == 'delete')
{
// set up delete query
$query = "DELETE FROM tablename WHERE id ='$id' ";
// do the query
mysql_query($query) or die('Error, query failed');
echo "<center><h3><font color=#000000>Deleted Successfully!</font></h3>";
}
// THE ADD OR EDIT OPTION
elseif (isset($_POST['add']) || isset($_POST['edit']))
{
//ADD
// File upload handling
if (isset($_POST['add'])&& $_FILES['field_3']['name']!='')
{
$field_3_filename = "file_3_".date("sihdmY").substr($_FILES['field_3']['name'],strlen($_FILES['field_3']['name'])-4);
if(!move_uploaded_file($_FILES['field_3']['tmp_name'], "./files/".$field_3_filename))
{
die("File " . $_FILES['field_3']['name'] . " was not uploaded.");
}
// set up insert query
$query = "INSERT into tablename(,field_1,field_2,field_3) VALUES ('" . $_POST['field_1'] . "','" . $_POST['field_2'] . "','" . "./files/".$field_3_filename . "')";
}
//EDIT
if (isset($_POST['edit'])&& $_FILES['field_3']['name']!='')
{
$field_3_filename = "file_3_".date("sihdmY").substr($_FILES['field_3']['name'],strlen($_FILES['field_3']['name'])-4);
if(!move_uploaded_file($_FILES['field_3']['tmp_name'], "./files/".$field_3_filename))
{
die("File " . $_FILES['field_3']['name'] . " was not uploaded.");
}
//Post..Variables..for..Update
$name = $_POST['field_1'];
$email = $_POST['field_2'];
$image = $_POST['field_3'];
//UPDATE..QUERY
$query = "UPDATE tablename SET field_1 = '$name',field_2 = '$email',field_3 = '" . "./files/".$field_3_filename . "' WHERE id='$id'";
}
//DO...QUERY
mysql_query($query) or die(mysql_error());
echo "<center><h3><font color=#000000>Updated Successfully!</font></h3>
<br />";
//EDIT FORM
}
else
{
// load stored values for editing event but not image field as will not prepopulate field due to security
if($action == 'edit')
{
$query = "SELECT * FROM tablename WHERE id='$id'";
$result = mysql_query($query);
$name = mysql_result($result,0,"field_1");
$email = mysql_result($result,0,"field_2");
}
?>
答案 0 :(得分:0)
只有在用户发送图像时才上传/更新db-table-field。如果没有,请不要触摸此字段:
<?php
// Check if form was submitted
if(count($_POST) > 0)
{
// Check if new image was uploaded
if(isset($_FILE) && !empty($_FILE['image']['tmp_name']))
{
// Here update the whole db-table, also the image/url-column...
}
else
{
// Here update the db-table, but not the image/url-column...
}
}