if(isset($ _ POST ['submitted']))由于未知原因而失去价值或虚假

时间:2013-05-23 00:53:12

标签: php html

我从昨天起就在研究这个问题,仍无法找到任何解决方案。 在此代码中,我采用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>

2 个答案:

答案 0 :(得分:0)

该行:

<label> Image (300 KB max.) <input type="file" name="image" </label>

错过了>的结束<input。它应该是:

<label> Image (300 KB max.) <input type="file" name="image" /> </label>

这可能会搞乱解析表格的其余部分,虽然我不知道为什么它有时有效并且有时会失败。

答案 1 :(得分:0)

不确定您的代码,但有些建议

  • 启用错误报告,以便您可以调试

在脚本开头添加以下代码

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
  • 删除所有错误抑制

从表达式的开头删除@

  • 删除隐藏字段<input type="hidden" name="submitted" value="1">

您只能使用以下内容(已在表单中提供)

<input type="submit" value="Submit" name="submit" />

并检查表单提交

if( isset( $_POST['submit'] ) )
{
    //...
}

而不是if(isset($_POST['submitted']))因为每次提交表单时,提交按钮都会在$_POST数组中提供,最后是my_sql is deprecated