doulu4316 2015-04-04 20:16
浏览 36
已采纳

POST和GET同时

hello everyone sorry but am just beginner in php , mysql ... i was developing question and answer website which i have page for all Question (Q.php) and page for displaying a specific question (QR.php) and get information according to data sent from Q.php via url ($_GET['start'] i also have page to confirm that the answer is already submitted .... but i got error when entering the id from get method and the message from post method ... any answer will be appreciated

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

if i removed passing hi from QR to QRR answer stored //$answer

if i removed storing answer from the text area the id from url stroed //$FURL

  • 写回答

1条回答 默认 最新

  • douyuben9434 2015-04-04 20:36
    关注

    This isnt the most beautiful code, as i just copied yours and made a few modifications.. but this form will submit back to the same page, and the php will run and insert ONLY if the form is submitted.

    if(isset($_POST['answer'])) says "if the variable _POST answer is set, run the php code and insert.. if it is not set, do nothing.

    You will notice the form action is left blank, you can set it to the page name yo are on.. as the form will send the variables to the same page. This is a good way of doing it because if there are errors, you can prepopulate the input or textareas with the code they just typed in.

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

    ` you will see i removed your answer_id. if you use this code, make sure that field is set to primary auto increment in your database.

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

    NOTE: both of these snippets will go in the same page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值