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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?