在“事件”表单中,如果未上传新图像,我将尝试保留(更新)现有图像。表单的其余部分保留诸如位置,时间等信息。 但是,当您尝试使用现有映像(在您单击更新之前会显示)时,在您单击更新后,现有映像将消失,并且不将其自身包含在更新中。我是一个业余爱好者。 ..感谢您的帮助。下面是一些代码:
function eventadd(){
if($_REQUEST['startampm'] == 'PM') $_REQUEST['starthour'] += 12;
$datetimescheduled = $_REQUEST['startyear'].'-'.$_REQUEST['startmonth'].
'-'.$_REQUEST['startdate'].' '.$_REQUEST['starthour'] .
':'.$_REQUEST['startminute'].':00';
$filetmp = $_FILES['file_img']['tmp_name'];
$filename = $_FILES['file_img']['name'];
$filetype = $_FILES['file_img']['type'];
$filepath = "flyers/".$filename;
move_uploaded_file($filetmp,$filepath);
$query = 'insert into eventcalendar '.
'(dateentered,datetimescheduled,title,location,description,img_name,img_path,img_type,soldout,private,free,paydoor,ticketlink,ticketURL) '.
'values '.'(now(),"'.$datetimescheduled.'","'.stripslashes($_REQUEST['title']).'","'.addslashes($_REQUEST['location']).'","'.stripslashes($_REQUEST['description']).'","'.$filename.'","'.$filepath.'","'.$filetype.'","'.addslashes($_REQUEST['soldout']).'","'.addslashes($_REQUEST['private']).'","'.addslashes($_REQUEST['free']).'","'.addslashes($_REQUEST['paydoor']).'","'.addslashes($_REQUEST['ticketlink']).'","'.addslashes($_REQUEST['ticketURL']).'")';
$result = mysql_query($query) or die("Query failed : " . mysql_error());
以下是更新表单,其中没有保留图像:
// update existing event action
if($updateevent) {
if($startampm == 'PM') $starthour += 12;
$datetimescheduled = $startyear.'-'.$startmonth.'-'.$startdate.' '.$starthour .':'.$startminute.':00';
// added to try to fix update image
$filetmp = $_FILES['file_img']['tmp_name'];
$filename = $_FILES['file_img']['name'];
$filetype = $_FILES['file_img']['type'];
$filepath = "flyers/".$filename;
// end fix - also see image name field didnt work
move_uploaded_file($filetmp,$filepath);
$query = 'update eventcalendar set '.
'datetimescheduled = "'.$datetimescheduled.'",'.
'title = "'.stripslashes($title).'",'.
'location = "'.addslashes($location).'",'.
'description = "'.stripslashes($description).'",'.
'img_name = "'.$filename.'",'.
'img_path = "'.$filepath.'",'.
'img_type = "'.$filetype.'",'.
'soldout = "'.stripslashes($soldout).'",'.
'private = "'.stripslashes($private).'",'.
'free = "'.stripslashes($free).'",'.
'paydoor = "'.stripslashes($paydoor).'",'.
'ticketlink = "'.stripslashes($ticketlink).'",'.
'ticketURL = "'.stripslashes($ticketURL).'"'.
'where id = '.$id;
$result = mysql_query($query) or die("Query failed : " . mysql_error());
HTML格式摘录:
<td align="right">IMAGE:</td>
<td><input name="file_img" type="file"><?php echo $row['img_path'] ?><?php echo '<img src='. $row['img_path'] .' class="img-responsive no-img thumb-img">'; ?><br>
同样,当提交没有新图像的HTML表单时,现有图像不会自动更新。
我得到的所有信息:传单/(无图像名称)
尽管显示为:<img src='. $row['img_path'] .
但在点击SUBMIT
之后不包含自身。
答案 0 :(得分:0)
首先,mysql_query已过时,因此,您应该使用mysqli_query。不建议在查询中传递变量,因为它会鼓励SQL注入-建议您使用准备好的语句。
要回答您的问题,您可以在进行更新之前检查图像是否已上传。解决方案是如果尚未上传图像,则忽略图像列,或者从数据库中选择现有图像。这是一个示例,如果未使用准备好的语句(未测试代码)上传图像,则从数据库中选择现有图像:
$filetmp = $_FILES['file_img']['tmp_name'];
$filename = $_FILES['file_img']['name'];
$filetype = $_FILES['file_img']['type'];
$filepath = "flyers/".$filename;
// end fix - also see image name field didnt work
move_uploaded_file($filetmp,$filepath);
// if a file has not been uploaded get the flyer from the DB
if(empty($filename))
{
// CHANGE $connection variable to whatever your db connection is called
$select_image = mysqli_prepare($connection, "SELECT img_name, img_path, img_type FROM eventcalendar WHERE id = ? ");
mysqli_stmt_bind_param($select_image, 'i', $id);
mysqli_stmt_execute($select_image);
mysqli_stmt_bind_result($select_image, $filename, $filepath, $filetype);
mysqli_stmt_fetch($select_image);
mysqli_stmt_close($select_image);
}
// CHANGE $connection variable to whatever your db connection is called
$query = mysqli_prepare($connection, "UPDATE eventcalendar SET datetimescheduled = ?, title = ?, location = ?,
description = ?, img_name = ?, img_path = ?, img_type = ?, soldout = ?, private = ?,
free = ?, paydoor = ?, ticketlink = ?, ticketURL = ? WHERE id = ? ");
mysqli_stmt_bind_param($query, 'sssssssssssssi', $datetimescheduled, stripslashes($title), addslashes($location),
stripslashes($description), $filename, $filepath, $filetype, stripslashes($soldout), stripslashes($private),
stripslashes($free), stripslashes($paydoor), stripslashes($ticketlink), stripslashes($ticketURL), $id);
mysqli_stmt_execute($query);
mysqli_stmt_close($query);