Undefined Index Question [Resolved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Undefined Index Question [Resolved]

Post by Epiales »

Okay, I have a simple dice game where they can place a bet and if they match 2 or 3 dice, they win gold. Problem is, is I always get the error below. I'm not sure why.

Notice: Undefined index: bet in C:\xampp\htdocs\login3\dicegame.php on line 219

dicegame.php

Code: Select all

<?php

$get_stats = "SELECT * FROM stats WHERE id=$id";
$user_query = mysqli_query($db_conx, $get_stats); 


$rolldice = (int) (rand(100, 999));
$maxbet = $stats_level * 50;
$_GET['rolldice'] = abs((int) $_GET['rolldice']);
if (($_SESSION['rolldice'] == $_GET['rolldice']) || $_GET['rolldice'] < 100)
{
    die(
            "<center><br />
<a href='dicegame.php?rolldice=$rolldice'> Refresh</a>");
}
$_SESSION['rolldice'] = $_GET['rolldice'];

$_GET['bet'] = abs((int) $_GET['bet']);
echo "<center>";
 
if ($_GET['bet'])
{
    if ($_GET['bet'] > $stats_gold)
    {
        die(
                "You cannot bet more than you have!<br />
<a href='dicegame.php?rolldice=$rolldice'>> Back</a>");
    }
    else if ($_GET['bet'] > $maxbet)
    {
        die(
                "You have gone over the max bet.<br />
<a href='dicegame.php?rolldice=$rolldice'>> Back</a>");
    }

    $chance[1] = (int) rand(1, 9);
    $chance[2] = (int) rand(1, 9);
    $chance[3] = (int) rand(1, 9);
        
    $dice1="<img src='images/one.png' width ='65'>";
    $dice2="<img src='images/two.png' width ='65'>";
    $dice3="<img src='images/three.png' width ='65'>";
    $dice4="<img src='images/four.png' width ='65'>";
    $dice5="<img src='images/five.png' width ='65'>";;
    $dice6="<img src='images/six.png' width ='65'>"; 
    $dice7="<img src='images/seven.png' width ='65'>"; 
    $dice8="<img src='images/eight.png' width ='65'>"; 
    $dice9="<img src='images/nine.png' width ='65'>"; 

    $one = $chance[1];
    $two = $chance[1];
    $three = $chance[1];
    $four = $chance[1];
    $five = $chance[1];
    $six = $chance[1];
    $seven = $chance[1];;
    $eight = $chance[1];
    $nine = $chance[1];
    
    $one = $chance[2];
    $two = $chance[2];
    $three = $chance[2];
    $four = $chance[2];
    $five = $chance[2];
    $six = $chance[2];
    $seven = $chance[2];;
    $eight = $chance[2];
    $nine = $chance[2];
    
    $one = $chance[3];
    $two = $chance[3];
    $three = $chance[3];
    $four = $chance[3];
    $five = $chance[3];
    $six = $chance[3];
    $seven = $chance[3];;
    $eight = $chance[3];
    $nine = $chance[3];    
        
    if ($chance[1]==1) echo $dice1;
    elseif ($chance[1]==2) echo $dice2;
    elseif ($chance[1]==3) echo $dice3;
    elseif ($chance[1]==4) echo $dice4;
    elseif ($chance[1]==5) echo $dice5;
    elseif ($chance[1]==6) echo $dice6;
    elseif ($chance[1]==7) echo $dice7;
    elseif ($chance[1]==8) echo $dice8;
    elseif ($chance[1]==9) echo $dice9;

    if ($chance[2]==1) echo $dice1;
    elseif ($chance[2]==2) echo $dice2;
    elseif ($chance[2]==3) echo $dice3;
    elseif ($chance[2]==4) echo $dice4;
    elseif ($chance[2]==5) echo $dice5;
    elseif ($chance[2]==6) echo $dice6;
    elseif ($chance[2]==7) echo $dice7;
    elseif ($chance[2]==8) echo $dice8;
    elseif ($chance[2]==9) echo $dice9;
    
    if ($chance[3]==1) echo $dice1;
    elseif ($chance[3]==2) echo $dice2;
    elseif ($chance[3]==3) echo $dice3;
    elseif ($chance[3]==4) echo $dice4;
    elseif ($chance[3]==5) echo $dice5;
    elseif ($chance[3]==6) echo $dice6;
    elseif ($chance[3]==7) echo $dice7;
    elseif ($chance[3]==8) echo $dice8;
    elseif ($chance[3]==9) echo $dice9;    

    
    echo "<br><br>You bet $_GET[bet] gold ";
    if ($chance[1] == $chance[2] && $chance[2] == $chance[3])
    {
        $won = $_GET['bet'] * 26;
        $gain = $_GET['bet'] * 25;
        echo " and won $won gold by matching 3 dice, giving you an extra $gain gold.  Fantastic!";
    }
    else if ($chance[1] == $chance[2] || $chance[2] == $chance[3]
            || $chance[1] == $chance[3])
    {
        $won = $_GET['bet'] * 3;
        $gain = $_GET['bet'] * 2;
        echo " and won $won gold by matching 2 dice, giving you an extra $gain gold.";
    }
    else
    {
        $won = 0;
        $gain = -$_GET['bet'];
        echo "and lost it.";
    }
    
$updateplayer = "UPDATE stats SET gold=gold+ '$gain' where id='$_SESSION[userid]'";
$user_query = mysqli_query($db_conx, $updateplayer); 
  
     $rolldice = (int) (rand(100, 999));
    echo "<br /><br> 
    
    
<a href='dicegame.php?bet={$_GET['bet']}&rolldice=$rolldice'><font color='red'> Try Your Luck Again</a><br /><br>
<a href='dicegame.php?rolldice=$rolldice'><font color='red'>Let Me Change Bet</a><br /><br>
<a href='index.php'><font color='red'>Go Home</a>";
}
else
{
echo
"Your Current Gold: $stats_gold <br /><br />
The maximum bet is based on your level.  Please place your bet!<br /><br />

Please bet $maxbet gold or less.
<form action='dicegame.php' method='get'>
Bet: <input type='text' name='bet' value='$maxbet' /><br />
<input type='hidden' name='rolldice' value='$rolldice' />
<input type='submit' value='Try Your Luck!!' />
</form>";
}
?>
It's just like with any other If isset GET statment, but this one isn't working properly and I don't know why. I've used forms a million times and the if isset statement and never had this problem.
Last edited by Epiales on Thu Oct 10, 2013 9:52 am, edited 1 time in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Undefined Index Question

Post by Winawer »

Try adding

Code: Select all

if( !isset( $_GET['bet'] ) ) {
    $_GET['bet'] = 0;
}
before you use $_GET['bet'] the first time
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Undefined Index Question

Post by Epiales »

Winawer wrote:Try adding

Code: Select all

if( !isset( $_GET['bet'] ) ) {
    $_GET['bet'] = 0;
} 
before you use $_GET['bet'] the first time
Yeah, that worked. Not sure why I didn't think of it lol! :roll:

Thank you again :)
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”