簡體   English   中英

如何將 MySQL 表記錄變成 PHP 參數

[英]How to make MySQL table records into a PHP parameter

我想從 while 循環中獲取的單選按鈕中檢索值,其中 name 屬性為$row['id_question'] ,然后我想計算該值,所以我需要array_count_value() ,我不知道如何將此$row['id_question']更改$row['id_question'] array_count_value()參數。

我收到錯誤:

未定義索引: & array_count_values() 中的答案期望參數 1 為數組,給定為空

我試圖把它變成一個變量$answer = $_POST['answer'] ;

但我不認為它會起作用(有效)

這是代碼:

<?php
    while ($row = mysqli_fetch_array($result)) {
        echo "<br/>" . $row['question'];
        echo "<br/>";
        echo "
        <label class='radio-inline'><span>YES</span>
            <input type='radio' class='form-check-label' name= 'answer[".$row['id_question']."]' checked value='1'/>
        </label>
        <label class='radio-inline'><span>NO</span>
            <input type='radio' class='form-check-label' name= 'answer[".$row['id_question']."]' checked value='2'/>
        </label>
        ";
    }
    $answer = $_POST['answer'];
    if (isset($_POST['submit']) ){
        print_r($answer); 
    }
    print_r(array_count_values($answer));

?>

嗯……在這行得通之前,您似乎還有很長的路要走。 :-/

我想我會先打印出$row s - 確保你真的在那個while循環中得到了一些東西。

請記住,在 while 循環之前實例化一個變量,然后將其填充到 while 循環中。 像這樣的東西:

<?php
$answer = '';
while ($row = mysqli_fetch_array($result)) {

    // What happens here? :-)
    echo "<pre";
    print_r( $row );
    echo "<pre";

    echo "<br/>" . $row['question'];
    echo "<br/>";
    echo "
    <label class='radio-inline'><span>YES</span>
        <input type='radio' class='form-check-label' name= 'answer[".$row['id_question']."]' checked value='1'/>
    </label>
    <label class='radio-inline'><span>NO</span>
        <input type='radio' class='form-check-label' name= 'answer[".$row['id_question']."]' checked value='2'/>
    </label>
    ";
}

$answer = $_POST['answer'];

// FALLBACK SO IT DOESN'T CRASH AND BURN
if( ! $answer ){
  echo '$answer was null again! I'll save you, by assigning it an empty array. :-) ';
  $answer = []; 
}
// END OF FALLBACK

if (isset($_POST['submit']) ){
    print_r($answer); 
}
print_r(array_count_values($answer));
?>

你得到的錯誤( 'Undefined index: answer in'... ) - 那是因為$_POST['answer']沒有定義, - 因此$answer將被設置為 null。 array_count_values需要一個數組作為輸入,它不能為空。

我希望這有幫助。 :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM