表格不承认岗位变量

时间:2011-12-26 03:09:29

标签: php html post html-form

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

时不会输入数据
 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>

1 个答案:

答案 0 :(得分:2)

您有两个PHP页面 - ask.phpquestion.php。我(猜测)认为ask.php用于存储问题和其他详细信息,您想要打开带有问题ID的question.php

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!!!";
  }
?>