Need help with a script I am coding

🚫
Exiled
Joined
May 10, 2013
Messages
67
Reaction score
8
Hey guys, I am coding a voucher redeeming script, for a Habbo retro CMS, WabboCMS..

Now, I am not the best with PHP, but I know how to get myself around.. And this script was, yes, created by myself.. Which means there will obviously be problems in the code!

I would like someone to look over the code and possibly revise it and fix problems I may have did while I was coding this. Just for information, the code doesn't actually work, which is why I would like some help with it!

It is much appreciated, thank you:
PHP:
 Voucher redeemer - Pastebin.com[/url]
 
Changed it up a tad
PHP:
<?php
if(isset($_GET['voucher'])) {
	// Defines this script that it is in a index file
	// Requires the core files which links all other needed files!
	define('IN_INDEX', 1);
	require('core.php');
	
	//I presume "filter" is some sort of securing/cleaning function
	$voucher_id = filter($_GET['voucher']);
	//Get all the info of vouchers that belong to the user
	$voucher_query = mysql_query("SELECT * FROM `credit_vouchers` WHERE `user_id` = '" . $user['id'] . "'");

	//Loop through the gathered vouchers
	while($voucher = mysql_fetch_array($voucher_query)) {
		//Does this user have a voucher with an id that corresponds to the $_GET value?
		if($voucher["code"] == $voucher_id) {
			if(mysql_query("UPDATE `users` SET `credits` = `credits` + '" . $voucher["amount"] . "' WHERE `user_id` = '" . $user_id . "'")) {
				mysql_query("DELETE FROM `credit_vouchers` WHERE `code` = '". $voucher_id ."'");
				echo('Voucher accepted! You redeemed the voucher code <b>' . $voucher_id . '</b> for <b>' . $voucher["amount"] .'</b>!');
			}
			break;
		} else {
			echo('Invalid voucher code entered!');
		}
	}
}
?>
 
Last edited:
Changed it up a tad
PHP:
<?php
if(isset($_GET['voucher'])) {
    // Defines this script that it is in a index file
    // Requires the core files which links all other needed files!
    define('IN_INDEX', 1);
    require('core.php');
    
    //I presume "filter" is some sort of securing/cleaning function
    $voucher_id = filter($_GET['voucher']);
    //Get all the info of vouchers that belong to the user
    $voucher_query = mysql_query("SELECT * FROM `credit_vouchers` WHERE `user_id` = '" . $user['id'] . "'");

    //Loop through the gathered vouchers
    while($voucher = mysql_fetch_array($voucher_query)) {
        //Does this user have a voucher with an id that corresponds to the $_GET value?
        if($voucher["code"] == $voucher_id) {
            if(mysql_query("UPDATE `users` SET `credits` = `credits` + '" . $voucher["amount"] . "' WHERE `user_id` = '" . $user_id . "'")) {
                mysql_query("DELETE FROM `credit_vouchers` WHERE `code` = '". $voucher_id ."'");
                echo('Voucher accepted! You redeemed the voucher code <b>' . $voucher_id . '</b> for <b>' . $voucher["amount"] .'</b>!');
            }
            break;
        } else {
            echo('Invalid voucher code entered!');
        }
    }
}
?>
Thanks for the help, but that doesn't seem to work either.. I will try to pin point the actual problem now!
 
"It doesn't work" doesn't really help. What error does it give you?

EDIT: Felt nice and guesses what you were trying to do.... Hope it is useful.

PHP:
<?php
define('IN_INDEX', 1);
require('core.php');

if(isset($_GET['voucher']))
{
        $voucherID = filter($_GET['voucher']);
        $userID = $_SESSION['user']['id']; //Seems like you're missing this or it's defined at core.php - in which case.. that's really nooby.    
        
        $getVoucher = mysql_query("SELECT value FROM credit_vouchers WHERE user_id = '{$userID}' AND code = '{$voucherID}'") or die(mysql_error());

        if(mysql_num_rows($getVoucher) > 0)
        {
            $VoucherAmount = mysql_fetch_row($getVoucher);
                       
            mysql_query("UPDATE users SET credits = credits + '{$voucherAmount[0]}' WHERE user_id = '{$userID}'") or die(mysql_error());
            mysql_query("DELETE FROM credit_vouchers WHERE code = '{$voucherID}'") or die(mysql_error());
                                
            echo("Voucher accepted! You redeemed the voucher code <b>{$voucherID}</b> for <b>{$voucherAmount['0']}</b>!");    
        }

        echo('Voucher does not exist');
} 
else 
{ 
    echo('No voucher was entered!'); 
}
?>
 
Last edited:
"It doesn't work" doesn't really help. What error does it give you?

EDIT: Felt nice and guesses what you were trying to do.... Hope it is useful.

PHP:
<?php
define('IN_INDEX', 1);
require('core.php');

if(isset($_GET['voucher']))
{
        $voucherID = filter($_GET['voucher']);
        $userID = $_SESSION['user']['id']; //Seems like you're missing this or it's defined at core.php - in which case.. that's really nooby.    
        
        $getVoucher = mysql_query("SELECT value FROM credit_vouchers WHERE user_id = '{$userID}' AND code = '{$voucherID}'") or die(mysql_error());

        if(mysql_num_rows($getVoucher) > 0)
        {
            $VoucherAmount = mysql_fetch_row($getVoucher);
                       
            mysql_query("UPDATE users SET credits = credits + '{$voucherAmount[0]}' WHERE user_id = '{$userID}'") or die(mysql_error());
            mysql_query("DELETE FROM credit_vouchers WHERE code = '{$voucherID}'") or die(mysql_error());
                                
            echo("Voucher accepted! You redeemed the voucher code <b>{$voucherID}</b> for <b>{$voucherAmount['0']}</b>!");    
        }

        echo('Voucher does not exist');
} 
else 
{ 
    echo('No voucher was entered!'); 
}
?>
It's telling me : No voucher was entered!
 
127.0.0.1/voucher.php?redeem=abc12345643 (which is the voucher in the database)
 
Back