2016-09-01 5 views
2

私は一連の質問を表示しました。phlを使ってURLから値を取得

私は答えを同じページに掲載する必要があったが、問題は私がselectgの質問に渡しているURLからidパラメータを取得していないことです。

私が選択されている質問に答えるとき、私は、例:id = 1のパラメータとしてurlに既にあるperticular質問の答えを投稿するために質問のidが必要になります。ここで

は、HTMLページの本体部分である:

 <?php 
include("menu/menu.php"); 
$sqli = "SELECT * FROM forum_question where id='$id'"; 
$result=mysqli_query($conn,$sqli); 
    ?> 

    <form action="submit_answer.php" method="post" name="answers"> 
    <br> <br> <br> 


     <?php 
    while($row = mysqli_fetch_array($result)) 
    echo "Q".$row['detail']; 
    ?> 

    <br> 
    answers:<br> 
    <textarea class="tinymce" name="answers"></textarea> 
    <input type="hidden" name="id" value="<?php echo $id;?>"> 
    <br> <br> 
    <input type="submit" value="submit" name="submit"> 

後にページを提出し、 "submit_answer.php"、コードは次のとおりです。

<?php 
include'config.php'; 

if($conn){ 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 

    $answers = $_REQUEST['answers']; 

    $id= $_GET ['id']; 
    } 

    $sqli= "INSERT INTO answers (answers) 
VALUES ('$answers')"; 
if (mysqli_query($conn,$sqli)) 
{ 
    echo "New record created successfully"; 
    header("location:answer.php?id='$id'"); 

} else { 
    echo "Error: " . $sqli . "<br>" . $conn->error; 
    } 
}else{    

} 

mysqli_close($conn); 


?> 

基本的に私は、私はちょうどPHPで非常に新鮮です質問のIDをどのように取得し、それを「submit_answer.php」に回答コンテンツとともに提出するかを知りたい。

+3

あなたのhtmlとjsコードを –

+1

に提供します。あなたのSQLクエリは、残念なことに注入攻撃に対して広く開かれています。 PDOに切り替えてください.PDOには、セキュリティ上の欠陥を緩和するのに役立つ用意されたステートメントとバインドされたパラメータが用意されています。あなたはPHPに慣れていないので、古いチュートリアルで見つけることができる一般的な落とし穴になるhttp://www.phptherightway.com/を読むことを本当にお勧めします。 – jedifans

+0

@jedifans - mysqli(OPがすでに使用している)はプリペアドステートメントとバインドされたパラメータもサポートしています。 – Quentin

答えて

0

あなたはそれが

<?php 
include'config.php'; 

if($conn){ 
    if (isset($_POST['answers']) && isset($_POST['id'])) { 
    $answers = $_POST['answers']; 
    $id= $_POST['id']; 

    $sqli= "INSERT INTO answers (answers) VALUES ('$answers')"; 
    if (mysqli_query($conn,$sqli)) 
    { 
     echo "New record created successfully"; 
     header("location:answer.php?id='$id'"); 

    } else { 
     echo "Error: " . $sqli . "<br>" . $conn->error; 
    } 
    } 
    mysqli_close($conn); 
} 
?> 
1

だけの回答フィールドの下に隠されたフィールドを取り、にURLパラメータを取得する(マークアップHTMLでIDの隠し入力を追加)

<?php 
include'config.php'; 
//session_start(); 
$id= $_GET ['id']; 

?> 

<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="css/style.css"> 
    <script type="text/javascript" src="tinymce/js/jquery.min.js"></script> 
     <script type="text/javascript" src="tinymce/plugin/tinymce/tinymce.min.js"></script> 
     <script type="text/javascript" src="tinymce/plugin/tinymce/init-tinymce.js"></script> 
</head> 
    <body> 
    <div id="container"> 
     <div id="main"> 


     <?php 
     include("menu/menu.php"); 
     $sqli = "SELECT * FROM forum_question where id='$id'"; 
     $result=mysqli_query($conn,$sqli); 
     ?> 

    <form action="submit_answer.php" method="post" name="answers"> 
     <br> <br> <br> 
     <?php 
     while($row = mysqli_fetch_array($result)) 
     echo "Q".$row['detail']; 
     ?> 

     <br>answers:<br> 
     <textarea class="tinymce" name="answers"></textarea> 
     <input type="hidden" name="id" value="<?php echo $id;?>"> 
     <br> <br> 
     <input type="submit" value="submit" name="submit"> 
    </form> 
    </body> 
</html> 

submit_answer.php変更する必要がありますユーザーがbuivankim2020と言ったように、そのページの非表示フィールドを送信した後、submit_answer.php、

送信した後に、答え。

関連する問題