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 lingo代码报错无法运行,如何解决,如果能解决请提供能够运行的lingo代码
  • ¥100 读取 IMU BNO080 数据
  • ¥15 基于RTKLIB框架写的精密单点定位-AR
  • ¥15 PFENet的预训练权重
  • ¥15 程序哪有错误怎么改?
  • ¥15 交换机和交换机之间的链路带宽以及主机带宽的理解
  • ¥15 ai创想家对战模式代码
  • ¥15 集合A由3个2行4列二维数组构成,从集合A中任意取一个二维数组元素、如果该二维数组元素的对应列位置的上、下两数都是奇数,而且仅有2个列是奇数/奇数,则该数组有意义,并放入集合B中打印输出。
  • ¥15 电信IPV6 无法外网访问吗
  • ¥15 有偿求效果比较好的遥感影像匹配的c++代码