POST和GET同时

时间:2015-04-04 20:16:28

标签: php mysql post get

大家好抱歉,但我只是初学者在php,mysql ...我正在开发问答网站,我有所有问题页面(Q.php)和显示特定问题(QR.php)的页面根据从Q.php通过url发送的数据的信息($ _GET ['start']我也有页面确认答案已经提交....但是从get方法输入id时出错并且来自发布方法...任何答案将不胜感激

Q.php

<?php


  include("pagination.php");

    if(isset($res))
    {
        while($result = mysql_fetch_assoc($res))
        {
                    echo '<div class="shop-item">' ;
                    echo '  <div class="price">' ;
                    echo  $result['Inquirer'] ;
                    echo '  </div>' ;

                    echo '  <div class="price">' ;
                    echo  $result['question'] ;
                    echo '  </div>' ;

                    echo ' <div class="actions"> ';
    echo '<input type="button" class="btn btn-large " value="More Info"  onclick="window.location=\'QR.php?start=' . urlencode($result['id']) . ' \';" />';
 echo '</div> ';
 echo ' </div> ';
        }

    }
?>

QR.php

<form action="QRR.php" method="POST"> 
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>

<div  align="Right">

<textarea class="form-control" rows="4" name ="answer" id="Test">       
</textarea>

<br>
<div class="actions">

<?php echo '<input type="button" class="btn btn-large " value="Post Answer" onclick="window.location=\'QRR.php?start=' . urlencode($_GET['start']) . ' \';" />'; ?>
</div>
</div>

 </div>
 </form>

QRR.php

<?php
// variables 
$answer=$_REQUEST['answer'];
require ("coonection.php");
$FURL = $_REQUEST['hi'];


//query 
$query = "INSERT INTO `answers`(`answer_id`, `question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('','$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data)
{
echo "Your Questions Has Been Successfully Added ";
}
?>

如果我从QR中删除了传递hi到QRR的答案存储// $ answer

如果我从文本区域删除了存储来自url stroed // $ FURL

的id的答案

1 个答案:

答案 0 :(得分:0)

这不是最漂亮的代码,因为我只是复制了你的代码并进行了一些修改..但是这个表单将提交回同一页面,并且只有在提交表单时,php才会运行并插入。

if(isset($ _ POST [&#39; answer&#39;]))&#34;如果设置变量_POST answer,运行php代码并插入..如果没有设置,什么都不做

您会注意到表单操作留空,您可以将其设置为页面名称,因为表单会将变量发送到同一页面。这是一种很好的方法,因为如果有错误,您可以使用他们刚输入的代码预填充输入或textareas。

<?php
// variables 
if(isset($_POST['answer'])){
    $answer=$_POST['answer'];
    require ("coonection.php");
    $FURL = $_POST['hi']; // there is no input name 'hi' set in your form.  so this code will fail due to that.


    //query 
    $query = "INSERT INTO `answers`(`question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
    $data=mysql_query($query) or die(mysql_error());
    if($data){
        echo "Your Questions Has Been Successfully Added ";
    }
}
?>

` 你会看到我删除了你的answer_id。如果您使用此代码,请确保该字段在数据库中设置为主要自动增量。

<form action="" method="POST"> 
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>

<div  align="Right">

<textarea class="form-control" rows="4" name="answer" id="Test"></textarea>

<br>
<div class="actions">
<button type="submit" class="btn btn-large " value="Post Answer">Post Answer</button>
</div>
</div>
 </div>
 </form>

注意:这两个片段都将放在同一页面中。