【问题标题】:POST and GET at the same time同时发布和获取
【发布时间】:2015-04-04 20:16:27
【问题描述】:

大家好,对不起,但我只是 php、mysql 的初学者……我正在开发问答网站,我有所有问题的页面(Q.php)和显示特定问题的页面(QR.php)并得到根据通过 url 从 Q.php 发送的数据的信息($_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 ";
}
?>

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

如果我从文本区域中删除了存储答案,则来自 url 的 id 将被删除 //$FURL

【问题讨论】:

  • 当人们投票否决您的问题但不说明原因时,您不讨厌它吗?我会投票赞成。他们可能投了反对票,因为您不能同时使用 POST 和 GET。
  • 完全不清楚。尝试指出代码中出现问题的位置?
  • 你绝对可以同时使用get和post。我一直都这样做。例如..通过post提交表单时,您可以使用参数设置url,使其成为get。但是,如果您要提交表单。您可能没有设置要获取的输入和要发布的输入。
  • 我在 $answer=$_REQUEST['answer']; 行中的 QRR.php 中有错误@anantkumarsingh 泰
  • @AdamJosephLooze 或者可能会:D 我做到了兄弟

标签: php mysql post get


【解决方案1】:

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

if(isset($_POST['answer'])) 表示“如果设置了变量 _POST 答案,则运行 php 代码并插入。如果未设置,则什么也不做。

您会注意到表单操作留空,您可以将其设置为您所在的页面名称.. 因为表单会将变量发送到同一页面。这是一个很好的方法,因为如果出现错误,您可以使用他们刚刚输入的代码预先填充输入或文本区域。

<?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>

注意:这两个 sn-ps 将进入同一页面。

【讨论】:

    猜你喜欢
    • 2014-11-05
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2011-05-05
    • 2014-02-06
    相关资源
    最近更新 更多