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 数据库原理及应用上机练习题
  • ¥30 征集Python提取PDF文字属性的代码
  • ¥15 如何联系真正的开发者而非公司
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 (求远程解决)深信服vpn-2050这台设备如何配置才能成功联网?