dongzaheng4449 2015-12-01 06:07
浏览 60
已采纳

Php - 注意:未定义的偏移量10? [重复]

This question already has an answer here:

<?php

require "connection.php";

$cek = mysql_query("SELECT * FROM `kelimeler` ORDER BY `numara` DESC LIMIT 10");

$i = 0;
$tr_dizi = array();
$alm_dizi = array();

while ($satir = mysql_fetch_array($cek)) {
    $tr_dizi[$i] = $satir["turkce"];
    $alm_dizi[$i] = $satir["almanca"];
    $i++;
}

$deger = rand(1,10);

setcookie("soru", $tr_dizi[$deger]);
setcookie("cevap", $alm_dizi[$deger]);

?>

<?php

if (isset($_POST["kelime"])) {
    if ($_POST["kelime"] == $_COOKIE['cevap']) {
        echo '<div class="bildirim">Richtig &#10004</div>';
    } else {
        echo '<div class="bildirim">'.$_COOKIE['soru'].' = '.$_COOKIE['cevap'].'</div>';
    }
}

?>

This is a quiz game like a = b what is a ? if user send b it says 'yes!' else correct answer was 'b' php say

Notice: Undefined offset: 10 on line 19 - 20 - 55

Line 19 -> setcookie("soru", $tr_dizi[$deger]);  
Line 20 -> setcookie("cevap", $alm_dizi[$deger]); 
Line 55 -> <divclass="kelime"><?php echo $tr_dizi[$deger] ?></div>

when user send the answer program check it and ask new question and yes or no is showing on new page i'm using cookies for that

how can i fix this error i can make error handling but i'm a new developer i want to learn thank you for everybody :)

</div>
  • 写回答

2条回答 默认 最新

  • dongliang1941 2015-12-01 06:11
    关注

    It is not setting 10 as $i starts from 0. And you are fetching 10 rows from database.

    So the keys would be 0-9. The rand should generate numbers between 0-9. Try with -

    $deger = rand(0, 9);
    

    And try to avoid using mysql. It is deprecated now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵