0

Basically, this code lists the "Available" challenges where complete = 0 and has an accept submit button with every listing. So far I can only list one item at a time, because more than one button being listed cannot identify the match ID $echo Can anyone look through my code and see if I can make any changes so I can change desc limit 10 from 1 Thanks.

$query = mysql_query("SELECT `create_id` ,`value`, `player1`, `match_id` FROM `multiplayer` WHERE `complete` = 0 ORDER BY `value` DESC LIMIT 1");
echo '<table>';
while($rowtwo = mysql_fetch_array($query)){
    $format_coins = number_format($rowtwo['value']);
    $value = $rowtwo['value'];
    $create_player = $rowtwo['player1'];
    $echo = $rowtwo['match_id'];
    $create_id = $rowtwo['create_id'];
    $button =   '<form action="multiplayer.php" method="POST"><input type="submit" name="'.$echo.'" value="Accept"></form>';
    ///$button = '<a href="match/'.$echo.'.php">Accept</a>';
    echo   '<tr>
            <td><font size="2" face="Lucida Sans Unicode"><strong>'.$rowtwo['player1'].'</strong> has wagered '.$format_coins.'M/Gp   '.$button.'</td>
            </tr>';
}
echo '</table>';
if (isset($_POST[''.$echo.''])) {
    if ($user_data['coins'] >= $value) {
        if ($user_data['user_id'] == $create_id) {
            echo 'You can\'t play yourself.';
        } else {    
                    $betadd = $user_data['bets'] + 1;
                    mysql_query("UPDATE `users` SET `bets` = $betadd WHERE `user_id` = $user_id");
                    $rand1 = rand(1, 5);$rand2 = rand(1, 5);$rand3 = rand(1, 5);$rand4 = rand(1, 5);$rand5 = rand(1, 5);$rand6 = rand(1, 5);$rand7 = rand(1, 5);$rand8 = rand(1, 5);$rand9 = rand(1, 5);$rand10 = rand(1, 5);
                    echo '<font size="2"><strong>You\'ve</strong> planted : <br></font>';
                    if ($rand1 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand1 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand1 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand1 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand1 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                    if ($rand2 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand2 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand2 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand2 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand2 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                    if ($rand3 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand3 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand3 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand3 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand3 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                    if ($rand4 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand4 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand4 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand4 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand4 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                    if ($rand5 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand5 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand5 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand5 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand5 === 5) {echo '<img src="images/Flowers_(pastel).png">';}                     
                    $userscore = counting(array($rand1, $rand2, $rand3, $rand4, $rand5));
                    echo '<br>';
                    echo '<font size="2"><strong>'.$create_player.'</strong> planted : <br></font>';
                    if ($rand6 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand6 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand6 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand6 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand6 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                    if ($rand7 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand7 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand7 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand7 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand7 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                    if ($rand8 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand8 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand8 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand8 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand8 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                    if ($rand9 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand9 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand9 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand9 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand9 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                    if ($rand10 === 1){     echo '<img src="images/Red_flowers.png">';} else if ($rand10 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand10 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand10 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand10 === 5) {echo '<img src="images/Flowers_(pastel).png">';}                     
                    //echo '<br>';
                    $hostscore = counting(array($rand6, $rand7, $rand8, $rand9, $rand10));
                    mysql_query("UPDATE `multiplayer` SET `rand1` = $rand1 WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `rand2` = $rand2 WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `rand3` = $rand3 WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `rand4` = $rand4 WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `rand5` = $rand5 WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `rand6` = $rand6 WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `rand7` = $rand7 WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `rand8` = $rand8 WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `rand9` = $rand9 WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `rand10` = $rand10 WHERE `match_id` = $echo");
                    echo '<br>';
                    echo '<br>';
                    if ($userscore > $hostscore) {
                        echo 'You win';
                        $username = $user_data['username'];
                        $userid = $user_data['user_id'];
                        mysql_query("UPDATE `users` SET `multi` = 0 WHERE `user_id` = $create_id");
                        mysql_query("UPDATE `multiplayer` SET `complete` = 1 WHERE `match_id` = $echo");
                        mysql_query("UPDATE `multiplayer` SET `score1` = $hostscore WHERE `match_id` = $echo");
                        mysql_query("UPDATE `multiplayer` SET `score2` = $userscore WHERE `match_id` = $echo");
                        mysql_query("UPDATE `multiplayer` SET `player2` = '$username' WHERE `match_id` = $echo");
                        $coins = $user_data['coins'] + $value;
                        mysql_query("UPDATE `users` SET `coins` = $coins WHERE `user_id` = $userid");
                    } else if ($userscore == $hostscore) {
                        echo 'You\'ve Tied';
                        $username = $user_data['username'];
                        mysql_query("UPDATE `users` SET `multi` = 0 WHERE `user_id` = $create_id");
                        mysql_query("UPDATE `multiplayer` SET `complete` = 1 WHERE `match_id` = $echo");
                        mysql_query("UPDATE `multiplayer` SET `score1` = $hostscore WHERE `match_id` = $echo");
                        mysql_query("UPDATE `multiplayer` SET `score2` = $userscore WHERE `match_id` = $echo");
                        mysql_query("UPDATE `multiplayer` SET `player2` = '$username' WHERE `match_id` = $echo");
                        $hostcoins = mysql_query("SELECT `coins` FROM `users` WHERE `user_id` = $create_id");
                        $hostpayback = $hostcoins + $value;
                        mysql_query("UPDATE `users` SET `coins` = $hostpayback WHERE `user_id` = $create_id");
                    } else if ($userscore < $hostscore) {
                        echo 'You\'ve Lost.';
                        $username = $user_data['username'];
                        $userid = $user_data['user_id'];
                        mysql_query("UPDATE `users` SET `multi` = 0 WHERE `user_id` = $create_id");
                        mysql_query("UPDATE `multiplayer` SET `complete` = 1 WHERE `match_id` = $echo");
                        mysql_query("UPDATE `multiplayer` SET `score1` = $hostscore WHERE `match_id` = $echo");
                        mysql_query("UPDATE `multiplayer` SET `score2` = $userscore WHERE `match_id` = $echo");
                        mysql_query("UPDATE `multiplayer` SET `player2` = '$username' WHERE `match_id` = $echo");
                        $hostcoins = mysql_query("SELECT `coins` FROM `users` WHERE `user_id` = $create_id");
                        $hostpayback = $hostcoins + ($value * 2);
                        mysql_query("UPDATE `users` SET `coins` = $hostpayback WHERE `user_id` = $create_id");
                        $coins = $user_data['coins'] - $value;
                        mysql_query("UPDATE `users` SET `coins` = $coins WHERE `user_id` = $userid");
                    }
        }
    } else { echo 'You can\'t afford to enter this match.'; }
}

Here

    ///$button = '<a href="match/'.$echo.'.php">Accept</a>';
    echo   '<tr>
            <td><font size="2" face="Lucida Sans Unicode"><strong>'.$rowtwo['player1'].'</strong> has wagered '.$format_coins.'M/Gp'.       $button =   '
                <form action="multiplayer.php" method="POST">
                <input type="radio" name="$echo" value="$echo">                     
                ';.' '.$button.'</td>
            </tr>';
}
echo '</table>';
echo '<input type="submit" name="submit" value="Accept"></form>';

if (isset($_POST['submit'])) {
    if (isset($_POST['$echo'])){
    echo $echo;
    }
2
  • Omg your code is total mess why do you update table in such strange way, why don't you update table by 1 query instead of spliting it to so many? Commented Apr 30, 2013 at 9:22
  • Because I'm still learning #beginner :) Commented Apr 30, 2013 at 9:52

1 Answer 1

2

Change your code it's total mess for example your ifs

 if ($rand1 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand1 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand1 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand1 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand1 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                if ($rand2 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand2 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand2 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand2 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand2 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                if ($rand3 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand3 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand3 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand3 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand3 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                if ($rand4 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand4 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand4 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand4 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand4 === 5) {echo '<img src="images/Flowers_(pastel).png">';}
                if ($rand5 === 1) {     echo '<img src="images/Red_flowers.png">';} else if ($rand5 === 2) {echo '<img src="images/Blue_flowers.png">';} else if ($rand5 === 3) {echo '<img src="images/Yellow_flowers.png">';} else if ($rand5 === 4) {echo '<img src="images/Orange_flowers.png">';} else if ($rand5 === 5) {echo '<img src="images/Flowers_(pastel).png">';}      

can be written

$color="";
if($rand1 === 1||$rand2=== 1||$rand3=== 1||$rand4=== 1||||$rand5===1) $color="Red";   
else if($rand1 === 2||$rand2=== 2||$rand3=== 2||$rand4=== 2||||$rand5===2) $color="Blue";

echo "<img src=\"images/$color_flowers.png\">";

but better would be to use array $rand = array(); and access them $rand[0], $rand[1]; etc.

same with mysql_updates

mysql_query("UPDATE `users` SET `multi` = 0 WHERE `user_id` = $create_id");
                    mysql_query("UPDATE `multiplayer` SET `complete` = 1 WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `score1` = $hostscore WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `score2` = $userscore WHERE `match_id` = $echo");
                    mysql_query("UPDATE `multiplayer` SET `player2` = '$username' WHERE `match_id` = $echo");

can be changed to

 mysql_query("UPDATE `multiplayer` SET `complete` = 1, score1=$hostscore, score2=$userscore, player2=$username WHERE `match_id` = $echo");

but mysql_* functions are old I'd advice to change them to PDO because they will be removed in future

Use escape functions and casting because your script is insecure!!

Answering your question you can use other form of input like radio, checkbox or hidden and put the values in loop for such fields, then use submit to send them.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the advice on my code as well as the security issues, but on the main topic, are you saying it's not possible to list these buttons with the available challenges?
of course you can, is match_id unique? If so you can use for example <input type="hidden" name="match_id" value="$echo" />
The match ID's are different for every available game, when someone creates a game it auto-increments a new match ID. When I tried doing it before I couldn't get the match I'Ds to echo when pressing the buttons. Only one button would work.
so don't use submit but use radio button in loop <input type="radio" name="match_id" value="$echo" /> and submit button to submit form.
This still doesn't work, I cannot echo the different match_ids it only echos one of them no matter which submit button I choose
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.