我从昨天开始就在研究这个问题,但仍然找不到任何解决方案。在此代码中,我采用 3 个文本输入和一个图像/图片文件上传。如果我在没有任何无效条目的情况下尝试此操作(文本字段中没有任何空白或使用小图像文件),它就可以正常工作。但是,如果我在 message/msg 字段中没有输入任何值并按两次提交,或者如果我尝试上传大图像文件,则代码不会给出任何错误并且不会在数据库中插入记录。
我认为它if(isset($_POST['submitted']))
在上述情况下发现错误,但为什么在这些情况下它失去了“提交”的价值,我无法理解。一旦它失去 的值$_POST['submitted']
,它将继续这样做,直到我删除历史记录,包括所有 cookie 等。
我将不胜感激。
<?php
session_start();
require_once('modules/dbcon.php'); // Connect to Database
echo ' post '.$_POST['submitted'];
if(isset($_POST['submitted'])) {
$var1=$_POST['wisher'];
$var2=$_POST['wishingto'];
$msg=$_POST['msg'];
$timestmp=time();
$date_rec=date('y/m/d');
$temp=$_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$fileup="";
echo 'Image size = '.$image_size;
if (move_uploaded_file($_FILES['image']['tmp_name'],'images/'.$_FILES['image']['name']))
{
print '<p> The file has been successfully uploaded </p>';
$fileup="true";
}
else
{
switch ($_FILES['image'] ['error'])
{ case 1:
print '<p> The file is bigger than this PHP installation allows</p>';
break;
case 2:
print '<p> The file is bigger than this form allows</p>';
break;
case 3:
print '<p> Only part of the file was uploaded</p>';
break;
case 4:
print '<p> No file was uploaded</p>';
break;
}
}
if($fileup){
$submit=mysql_query("INSERT INTO records VALUES ('','$var1','$var2','$msg','$temp','$timestmp','N','$date_rec')");
echo '<script type="text/javascript">alert ("Your submission has been successfully recorded and awaiting review");</script>';
session_destroy();
} // File is up
} else {
echo 'Nothing submitted at this run';
} //End Submit records (if any)
if(array_key_exists('page',$_GET)){
$page=$_GET['page'];
} else{
$page=0 ;
}
$skip=$page*25;
$time=time()-(604800); // Timestamp 7 days old
//$query=mysql_query("SELECT * FROM records WHERE approved='Y' AND stamp>='$time' ORDER BY stamp DESC LIMIT $skip,25"); // Fetch records of current week
$query=mysql_query("SELECT * FROM records ORDER BY stamp DESC LIMIT $skip,25"); // Fetch records of current week
@ $count=mysql_num_rows($query); //no. of rows fetched
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body bgcolor="#FFFFFF">
<center>
<div class="main">
<?php
while(@ $rec=mysql_fetch_assoc($query)){ //Loop over records to print each
?>
<div class="record">
<div class="image" align="left">
<a href="<?php echo 'images/'.$rec['image']; ?>"><img src="<?php echo 'images/'.$rec ['image']; ?>" width="150" border="0"/></a>
</div><!-- image -->
<div class="name1" align="left">
<?php echo 'From: '.$rec['wisher']; ?>
</div><!-- name1 -->
<div class="name2" align="left">
<?php echo 'To: '.$rec['wishingto']; ?>
</div><!-- name2 -->
<div class="message" align="left">
<?php echo $rec['message']; ?>
</div><!-- message -->
</div><!-- Record -->
<?php } ?>
<?php if($count!=0) { ?> <!-- Navigation Buttons -->
<center> <div class="navbuttons">
<?php if($page!=0){ ?> <a style="padding-right:10px;" href="?page=<?php echo $page-1;?>" >Prev</a> <?php } ?>
<?php if($count==25){ ?><a href="?page=<?php echo $page+1;?>">Next</a> <?php } ?>
</div></center>
<?php } ?>
<div class="submitform" align="left">
<h2>Submit Your Information :</h2><br />
<form action="index.php" method="post" enctype="multipart/form-data" onsubmit="return validateForm(this)" >
<script>
function validateForm(form)
{
var wisher_name = form.wisher.value;
var wishingto_name = form.wishingto.value;
var msg_contents = form.msg.value;
var image_name = form.image.value;
if (wisher_name == null || wisher_name =="")
{
alert('Enter value in Wisher name');
return false;
}
if (wishingto_name == null || wishingto_name =="")
{
alert('Enter value in Wishing to name, prev field = '+form.wisher.value);
return false;
}
if (msg_contents == null || msg_contents =="")
{
alert('Enter value in the message');
return false;
}
if (image_name == null || image_name =="")
{
alert('Select an image');
return false;
}
return true;
}
</script>
<label> Wisher: <input type="text" name="wisher" /> </label>
<label> Wishing to: <input type="text" name="wishingto" /> </label>
<label> Message: <input type="text" name="msg" /> </label>
<input type="hidden" name="MAX_FILE_SIZE" value="307200" />
<label> Image (300 KB max.) <input type="file" name="image" /> </label>
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="submitted" value="1">
</form>
</div><!-- Form -->
</div><!-- main -->
</center>
</body>
</html>