Help Update Income
Posted: Wed Apr 28, 2010 2:27 pm
Hello again,
I have created a form that will update the winner of a sport event between two teams. This update of the database works fine.
However I now need it to also update my income of the users that have selected the correct winner.
Basically I will use 2 tables:
1)sportEvents - which stores: the 2 teams, team ids, game data/time and so one
2)makeBets - which stores the sport event id (references sportEvents), the bet amount, and the member_id that bet on the game
I am unable to get my php and mysql to actually update my database properly and I'm pretty sure my MySQL commands work fine.
My code is as follows:
I have created a form that will update the winner of a sport event between two teams. This update of the database works fine.
However I now need it to also update my income of the users that have selected the correct winner.
Basically I will use 2 tables:
1)sportEvents - which stores: the 2 teams, team ids, game data/time and so one
2)makeBets - which stores the sport event id (references sportEvents), the bet amount, and the member_id that bet on the game
I am unable to get my php and mysql to actually update my database properly and I'm pretty sure my MySQL commands work fine.
My code is as follows:
Code: Select all
<?php
//Start session
session_start();
//Include database connection details
require_once('config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$winner_id = clean($_POST['school_name']);
$game_id = clean($_POST['game_id']);
$member_id = $_SESSION['SESS_MEMBER_ID'];
$data = mysql_query("SELECT school_name FROM teams WHERE team_id='$winner_id'")
or die(mysql_error());
$gameupdate = mysql_fetch_array( $data );
//determines school name from team_id
$winner = $gameupdate['school_name'];
//Create update winner query
$qry = "UPDATE sportEvents SET game_winner='$winner' WHERE sport_event_id ='$game_id'";
$result = mysql_query($qry);
//if query is successful
/////////////////////////////This Section Seems To Be the Problem////////
if($result) {
//pull information from bet where bet is on right game
$income_qry = "SELECT * from makeBets where sport_event='$game_id'";
$income_results = mysql_query($income_qry)
or die("Query failed");
while($incomeupate = mysql_fetch_array($income_results)){
$bet_amt = $incomeupdate['bet_amt'];
$selection = $incomeupdate['selection'];
$member = $incomeupdate['member_id'];
$inc = $bet_amt + $bet_amt;
if($selection==$winner){
$qry_update_income = "UPDATE users SET income='$inc' WHERE member_id ='$member'";
$update_results = mysql_query($qry_update_income);
header("location: bet-success.php");
exit();
}
else {
continue;
}
}
}
//Check whether the query was successful or not
if($result) {
header("location: update-games-success.php");
exit();
}else {
die("Query failed");
}
?>