表单不能识别post变量


Form not recognizing post variables

我有一个简单的html表单和一些php输入POST变量到mysql数据库。但是,我注意到当

时,表单不会输入数据。
 if (isset($_POST['submit'])){
 insert stuff in here
 }

是包括在内。然后我删除上面的if语句并运行代码。除了使用POST (ex $_POST['var1'])的表单中的变量外,所有变量都被输入。似乎POST变量没有被识别,我不知道出了什么问题。

所有代码:

<?php session_start(); ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src='http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js'></script>
<link rel="stylesheet" href="../css/bootstrap.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" />
</head>
<body>
<?php include '../css/bar.php'; ?>
<div id='content'>
    <?php include '../nav.php'; 
    ?>

    <?php

    if (isset($_POST['submit'])){
    include '../connect.php';
    $question=mysql_real_escape_string($_POST['question']);
    $detail=mysql_real_escape_string($_POST['detail']);
    $date=date("d M Y");
    $time=time();
    $user=$_SESSION['id'];
    $put=mysql_query("INSERT INTO questions VALUES ('','$question','$detail','$date','$time','$user','subject','0')");
    $result=mysql_query("SELECT * FROM questions WHERE user='$user' AND time='$time'");
        while ($row = mysql_fetch_assoc($result)){
             $q=$row['id'];
            }
    }
    ?>
    <form method='POST' action='question.php?q=<?php echo $q ?>'>   
        <p>Question:</p>
        <p><input type='text' name='question' id='question'  maxlength='200'></p>
        <p>Add some detail (optional):</p>
        <p><textarea id='detail' name='detail' ></textarea></p>
        <p>Tags:</p>
        <p><input type='submit' value='submit' name='submit'></p>
    </form>

</div>
<?php include '../footer.php'; ?>
</body>
</html>

TESTPAGE:

<?php 
 include 'connect.php'; 
if (isset($_POST['submit'])){
$hhh=mysql_real_escape_string($_POST['hhh']);
$put=mysql_query("INSERT INTO questions VALUES ('','$hhh','','','','','','')");
}

?>
<form action='test.php' method='post'>
<input type='text' name='hhh'>
<input type='submit' name='submit' value='submit'>
</form>

您有两个PHP页面- ask.phpquestion.php。我(猜)认为ask.php是用来存储问题和其他细节,你想打开一个question.php与问题id。

ask.php

<?php session_start(); ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
<script type='text/javascript' src='http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js'></script>
<link rel="stylesheet" href="../css/bootstrap.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/basic.css" type="text/css" media="screen" />
</head>
<body>
<?php include '../css/bar.php'; ?>
<div id='content'>
    <?php 
     include '../nav.php'; 
     /*--- If submit button is pressed  ---- */
     if (isset($_POST['submit']))
       {
        include '../connect.php';
        $question=mysql_real_escape_string($_POST['question']);
        $detail=mysql_real_escape_string($_POST['detail']);
        $date=date("d M Y");
        $time=time();
        $user=$_SESSION['id'];
        /* SELECT column names you want to use with INSERT statement */
        $put=mysql_query("INSERT INTO questions 
              (`question`,`detail`,`date`,`time`,`user`,`subject`,`name_of_last_col` ) 
                VALUES 
                   ('$question','$detail','$date','$time','$user','subject','0')");
        //for debug purpose 
        if($put)
          {
            echo "Record added";
           }
         else
         {
           echo "Can't add record " . mysql_error();
          }
         }
        /*----- End submit block -----------*/
       /*----List the questions and select it----------*/
        $time=time(); 
        $user=$_SESSION['id'];
        //I think this wont work. Try to remove time comparison from the SELECT statement. 
        $result=mysql_query("SELECT * FROM questions WHERE `user`='$user' AND `time`='$time'");
        //$result=mysql_query("SELECT * FROM questions WHERE `user`='$user'");
        if($result)
         {
            echo "<table>";
            while($row = mysql_fetch_assoc($result))
            {
               echo "<tr>";
               echo "<td>$row[question]</td>";
               echo "<td><a href='question.php?qid=$row[id]'>Show a question</a></td>";
               echo "</tr>";
            }
            echo "</table>";
         }
        else
          {
            echo "No questions!!!";
          }
        ?>
        <form method='POST' action="ask.php">
            <p>Question:</p>
            <p><input type='text' name='question' id='question'  maxlength='200'></p>
            <p>Add some detail (optional):</p>
            <p><textarea id='detail' name='detail' ></textarea></p>
            <p>Tags:</p>
            <p><input type='submit' value='submit' name='submit'></p>
        </form>

question.php应该是:

<?php
 $qid=$_GET["qid"];
 echo "$qid is selected...";
?>

你可以把代码分成两个PHP页面——一个保存记录,另一个列出和选择行。

addquestion.php

<?php 
 session_start(); 
 if(isset($_POST['submit']))
  {
   mysql_connect("localhost","user","password") or die(mysql_error());
   mysql_select_db("your_db_name") or die(mysql_error());
   $question=mysql_real_escape_string($_POST['question']);
   $detail=mysql_real_escape_string($_POST['detail']);
   $date=date("d M Y");
   $time=time();
   $user=$_SESSION['id'];
   $put=mysql_query("INSERT INTO questions 
         (`question`,`detail`,`date`,`time`,`user`,`subject`,`name_of_last_col` ) 
           VALUES 
         ('$question','$detail','$date','$time','$user','subject','0')");
   //for debug purpose 
   if($put)
    {
     echo "Record added";
     }
   else
    {
     echo "Can't add record " . mysql_error();
     }
  }
?>
<form method='post' action="addquestion.php">
 <p>Question:</p>
 <p><input type='text' name='question' id='question'  maxlength='200'></p>
 <p>Add some detail (optional):</p>
 <p><textarea id='detail' name='detail' ></textarea></p>
 <p>Tags:</p>
 <p><input type='submit' value='submit' name='submit'></p>
</form>

questionlist.php

<?php
  session_start(); 
  $user=$_SESSION['id'];
  mysql_connect("localhost","user","password") or die(mysql_error());
  mysql_select_db("your_db_name") or die(mysql_error());  
  $result=mysql_query("SELECT * FROM questions WHERE `user`='$user'");
  if($result)
  {
   echo "<table>";
   while($row = mysql_fetch_assoc($result))
   {
     echo "<tr>";
     echo "<td>$row[question]</td>";
     echo "<td><a href='question.php?qid=$row[id]'>Show a question</a></td>";
     echo "</tr>";
     }
  echo "</table>";
  }
  else
  {
   echo "No questions!!!";
  }
?>