每当我刷新页面或离开并返回时,图像都会从站点和数据库中消失。我不希望表单重定向到另一个页面,我只是希望它保持在同一页面上,但只在按下提交按钮时运行查询。
<?php
$display = mysql_query("SELECT image FROM `blog_users` WHERE username = '$session->username'");
$res = mysql_fetch_array($display); echo "<image style='height: 50px; width:50px;' src='".$res['image']."'>";
$close = 0;
?>
//the above code displays the image
<?php
define ("MAX_SIZE","1000");
//This function reads the extension of the file. It is used to determine if the
// file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//This variable is used as a flag. The value is initialized with 0 (meaning no
// error found)
//and it will be changed to 1 if an errro occures.
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['Submit']))
{
//reads the name of the file the user submitted for uploading
$image=$_FILES['image']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and
// will not upload the file,
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension !=
"png") && ($extension != "gif"))
{
//print error message
echo '<h3>Unknown extension!</h3>';
$errors=1;
}
else
{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);
//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images
//folder)
$newname="upload/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h3>Copy unsuccessfull!</h3>';
$errors=1;
}}}}
//this code sends the image location to the database
mysql_query("UPDATE blog_users SET image = '$newname' WHERE username = '$session->username'") ;
if( strcasecmp($_SERVER['REQUEST_METHOD'],"POST") === 0) {
$_SESSION['postdata'] = $_POST;
header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
exit;
}
?>
<form name="newad" method="post" enctype="multipart/form-data"
action=""><img src="" alt="" />
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.0.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
$(document).ready(
function(){
$('input:submit').attr('disabled',true);
$('input:file').change(
function(){
if ($(this).val()){
$('input:submit').removeAttr('disabled');
}
else {
$('input:submit').attr('disabled',true);
}
});
});
</script>
</head>
<table>
<tr><td><input type="file" name="image" id="file"></td></tr>
<tr><td><input name="Submit" type="submit" value="Upload image" id="image_upload" disabled>
</td></tr>
</table>
</form>
答案 0 :(得分:2)
回答问题的评论。
if(isset($_POST['Submit'])){...}
- ...
=要执行的代码。
现在的方式是mysql_query("UPDATE...
无论如何都会运行。
换句话说,为它重新定位支撑。
您有}}}}
删除其中一个大括号并将其重新定位在?>
标记之前,您应该好好去。
if(isset($_POST['Submit'])){
...
$errors=1;
}}}
//this code sends the image location to the database
mysql_query("UPDATE blog_users SET image = '$newname' WHERE username = '$session->username'") ;
if( strcasecmp($_SERVER['REQUEST_METHOD'],"POST") === 0) {
$_SESSION['postdata'] = $_POST;
header("Location: ".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
exit;
}
} // brace for if(isset($_POST['Submit']))
?>