2014-04-23 146 views
1

這個問題可能會被標記爲重複,但我沒有找到其他答案有幫助。如何將變量從一個php文件轉移到另一個文件?

我正在創建一個網站,用戶可以回答多種類型的問題。因此,我編寫代碼,以便根據問題的類型顯示相應的html模板。現在我的PHP代碼如下:

<?php 
include_once 'init/init.funcs.php'; 
$_SESSION['pollid']=(int) $_GET['pollid']; 
$questions = array(); 
$answer = $_POST['answer']; 
if (!isset($_SESSION['answering'])) { 
    $result = mysql_query('SELECT * from katse_kysimused where kysimustik_id="' . $_SESSION['pollid'] . '"'); 
    while($row = mysql_fetch_assoc($result)) { 
     $questions[] = $row['kysimus']; 
     } 
    $_SESSION['answering']['questions'] = $questions; 
    $_SESSION['answering']['index'] = 0; 
} 
    $x = $_SESSION['answering']['index']; 
    $result3 = mysql_query('SELECT tyyp_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x] . '"'); 
    $type = mysql_result($result3, 0); 
    if ($type=='3'){ 
     echo $_SESSION['answering']['questions'][$x]; 
     $result4 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x] . '"'); 
     $question_id = mysql_result($result4, 0); 
     $result5 = mysql_query('SELECT * from katse_valik_vastused where kysimus_id="' . $question_id . '"'); 
     if($result5 === FALSE) { 
      die(mysql_error()); 
     } 
     while($row = mysql_fetch_assoc($result5)) { 
      $options[] = $row['vasuts']; 
     } 
     foreach($options as $option=>$option_value) { 
      echo $option_value; 
} 
     } 

    if ($type=='1'){ 
      echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/tekstkysimusele_vastamine2.php'>"; 
     } 
if(isset($_POST['submit'])){ 
    $result2 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x -1] . '"'); 
    $q_id = mysql_result($result2, 0); 
    mysql_query('INSERT INTO katse_vastused2 (id, vastus,kysimus_id, vastustik_id) VALUES (NULL,"' . $answer . '","' . $q_id . '","1")'); 
    } 
$_SESSION['answering']['index']++; 
?> 

而我想要做的就是讓下面的HTML模板出現在$類型==「1」。

<?php 
echo $_SESSION['answering']['questions'][$x]; 
?> 
<html> 
<br> 
<form method="post" action="answering.php"> 
<input type="text" name="answer" style="width:300px; height:150px;"><br> 
<input name= "submit" type="submit" value="Vasta"> 
</form> 
</html> 

我的問題是,我怎麼能讓它出現問題。現在$ _SESSION ['回答'] ['questions'] [$ x];是不確定的。我怎樣才能將它從第一個文件轉移到第二個文件?

+0

注意SQL注入。 –

+0

你怎麼稱呼第二個文件? –

+0

我稱之爲'tekstkysimusele_vastamine2.php'。 – user244902

回答

0

$_SESSION['answering']['questions'];
包含一個項目列表,你必須指定哪一個應該被顯示。

如果你需要的最後一個索引,這是簡單的(醜陋的)解決方案:

<?php 
    $last_index = count($_SESSION['answering']['questions']) -1; 
    echo $_SESSION['answering']['questions'][$last_index]; 
?> 
<html> 
<br> 
<form method="post" action="answering.php"> 
<input type="text" name="answer" style="width:300px; height:150px;"><br> 
<input name= "submit" type="submit" value="Vasta"> 
</form> 
</html> 
0

取出$x,然後嘗試:

echo $_SESSION['answering']['questions']; 
0

我解決了這個問題。現在我的第二個文件如下:

<?php 
include_once 'init/init.funcs.php'; 
$x = $_SESSION['answering']['index']; 
echo $_SESSION['answering']['questions'][$x]; 
?> 
<html> 
<br> 
<form method="post" action="answering.php"> 
<input type="text" name="answer" style="width:300px; height:150px;"><br> 
<input name= "submit" type="submit" value="Vasta"> 
</form> 
</html> 
相關問題