无法在PHP中访问变量值

时间:2018-03-12 04:28:37

标签: php html mysql

这是我打印问题的代码,然后是他们各自的答案。

<?php 
  $reg = $_SESSION['reg'];
  $sql1="select users.reg,questions.question,questions.id from users,questions where users.reg=questions.reg and questions.company='infosys' and questions.reg!='$reg' limit 0,5";
  $query1=mysqli_query($conn,$sql1);
  $sql2="select questions.id,answers.reg,answers.answer from questions,answers where questions.id=answers.id";
  $query2=mysqli_query($conn,$sql2);
  while($row1 = mysqli_fetch_assoc($query1)){
    echo "<p class='fill'><bold>".$row1["reg"]."</bold> asked:-" ;
    echo '<br>QID:-'.$row1['id'].'&emsp;&emsp;"'.$row1["question"].'"<br></p>';
        while($row2 = mysqli_fetch_assoc($query2)){
        echo "<pre align='right'><bold>".$row2["reg"]."</bold> answered:-" ;
        echo '<br>&emsp;&emsp;"'.$row2["answer"].'"</pre>'; 
        }
echo '<form method="post" style="display:none;">';
echo '<textarea rows="2" cols="80" name="ans" class="form-control" required></textarea>';
echo '<button class="w3-button w3-blue w3-padding-medium w3-medium w3-margin-top w3-right" type="submit" name="answer">submit</button></form>';
echo '<button class="w3-button w3-blue w3-padding-small w3-small w3-right w3-margin-top show_button2">Answer</button><br><br>';
}
?>

但是我无法获得正确的输出,而且我无法从文本区域获得答案并插入带有正确问题ID的表格。我的INSERT代码是:

if(isset($_POST['answer'])){
  $id = $_SESSION['id'];
  $reg = $_SESSION['reg'];
  $answer = $_POST['ans'];
  $sql="insert into answers VALUES('$id','$reg','$answer')";
  $query=mysqli_query($conn,$sql);
  if($query==TRUE){
      header("location: infosys.php");
  }
}

请告诉我如何从该范围访问此插入范围

的$ row变量

2 个答案:

答案 0 :(得分:1)

我得到了解决方案。我将$ row1 [&#39; id&#39;]存储在$ id中并存储在&#34;提交&#34;按钮的值

$id=$row31['id'];
echo '<form method="post" style="display:none;">';
echo '<textarea rows="2" cols="80" name="ans" class="form-control" required></textarea>';
echo '<button type="submit" name="answer_BTN" value='.$id.'">submit</button></form>';

然后我通过$ _POST [&#39; answer_BTN&#39;]访问了该值。

if(isset($_POST['answer_BTN'])){
  $id = $_POST['answer_BTN'];
  $reg = $_SESSION['reg'];
  $answer = $_POST['ans'];
  $sql2="insert into answers VALUES('$id','$reg','$answer')";
  $query2=mysqli_query($conn,$sql2);
  if($query2==TRUE){
      header("location: infosys.php");
  echo "<script>alert('Added your Answer!')</script>";
  }
}

答案 1 :(得分:0)

你是在同一个PHP文件或另一个PHP文件上检查提交的吗?

如果另一个文件提到这里的位置:

echo '<form method="post" action="../location/to/yourfile.php" style="display:none;">';

否则请确保您正在检查页面顶部的isset()