Last active
April 23, 2016 11:08
-
-
Save thinsoldier/b4ce1408ec71e96a1bab68c2549b02bf to your computer and use it in GitHub Desktop.
Esau date diff problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//----------------------------- | |
/* | |
It is not safe to rely on the system's timezone settings. | |
You are *required* to use the date.timezone setting | |
or the date_default_timezone_set() function. | |
*/ | |
// Force time zone to Nassau, Bahamas | |
date_default_timezone_set('America/Nassau'); | |
//----------------------------- | |
require 'databaseconnect.php'; | |
$conn = new mysqli($servername, $username, $password, $dbname); | |
// check connection | |
if ($conn->connect_error) { | |
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);} | |
// ^^ I would put all of the above within the databaseconnect.php file unless you have a reason not to. | |
//----------------------------- | |
// Create an array list of names of allowed inputs: | |
$_allowed_inputs = explode('room_id,playerstat,gameid,name,user_id,current_time,playeramount'); | |
// An array to hold the filtered values: | |
$_filtered_inputs = array; | |
// Loop through the list and for each item run it through filter_input and save the result | |
foreach( $_allowed_inputs as $key => $GETfield ) | |
{ | |
$_filtered_inputs[ $GETfield ] = filter_input(INPUT_GET, $GETfield); | |
} | |
// var_dump( $_filtered_inputs ); | |
// Extract filtered input entries as $normal $variable $names: | |
extract($_filtered_inputs); | |
//----------------------------- | |
//----------------------------- | |
//SELECT ACCOUNT BALANCE FROM USER | |
$sql='SELECT accountbal FROM as_users WHERE user_id = ?'; | |
/* Prepare statement */ | |
$stmt = $conn->prepare($sql); | |
if($stmt === false) { | |
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR); | |
} | |
/* Bind parameters. TYpes: s = string, i = integer, d = double, b = blob */ | |
$stmt->bind_param('i',$user_id); | |
/* Execute statement */ | |
$stmt->execute(); | |
// ^^^ You could wrap all of the above into a simpl ereusable function called: | |
// db_select_data( $fields, $fromTable, $whereField, $whereValue ) | |
// | |
// Example: $stmt = db_select_data( 'accountbal', 'as_users', 'user_id', $user_id ); | |
//----------------------------- | |
$stmt->bind_result($accountbal); | |
while ($stmt->fetch()) { | |
// ^^^ YOU SEEM DO BE DOING ABSOLUTELY NOTHING IN HERE AFTER FETCHING THE DATA! | |
} | |
$sql = 'SELECT player1,privacylevel,playerlimit,sets,gamesplayed,roomtotal,gametype,betamount,playeramount,player2,player3,player4,roomuse,playerexit,guests,status,acceptguest, | |
chat1,chat2,chat3,chat4,player1time,player2time,player3time,player4time,player1score,player2score,player3score,player4score,player1update, | |
player2update,player3update,player4update,gameid,referee,datetime FROM roomvalues INNER JOIN chat ON roomvalues.room_id = chat.room_id WHERE roomvalues.room_id=?'; | |
// ^^^ IF YOU ARE SELECTING EVERY FIELD FROM BOTH TABLES YOU (maybe) CAN USE | |
// SELECT roomvalues.*, chat.* | |
// FROM roomvalues | |
// INNER JOIN chat ON roomvalues.room_id = chat.room_id | |
// WHERE roomvalue.room_id=? | |
// ... I might be wrong though. Have a field named room_id in both tables might | |
// prevent using SELECT roomvalues.*, chat.* ... | |
/* Prepare statement */ | |
$stmt = $conn->prepare($sql); | |
if($stmt === false) { | |
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR); | |
} | |
/* Bind parameters. TYpes: s = string, i = integer, d = double, b = blob */ | |
$stmt->bind_param('i',$room_id); | |
/* Execute statement */ | |
$stmt->execute(); | |
$stmt->bind_result($player1,$privacylevel,$playerlimit,$sets,$gamesplayed,$roomtotal,$gametype,$betamount,$playeramount,$player2,$player3,$player4, | |
$roomuse,$playerexit,$guests,$status,$acceptguest,$chat1,$chat2,$chat3,$chat4,$player1time,$player2time,$player3time,$player4time,$player1score,$player2score,$player3score,$player4score,$player1update,$player2update,$player3update,$player4update,$gameid,$referee,$datetime); | |
// ^^^ But for thispart above I'm pretty sure you can just | |
// fetch the data into to an array and then use extract(); | |
// instead of having to type all this. | |
// http://php.net/extract | |
// Lets pretend you had a single variabled called $dbData that contain all those variables as $key > $value pairs. | |
while ($stmt->fetch()) | |
// ^^^ I don't understand why this needs to be within a while() statement. | |
$datetime1 = new DateTime( date("Y-m-d" )); // today | |
$datetime2 = new DateTime( $subject ); | |
$elasped = date_diff($datetime1, $datetime2); | |
//^^^ "elapsed" is spelled wrong here! | |
echo $player1.';'.$privacylevel.';'.$playerlimit.';'.$sets.';' | |
.$gamesplayed.';'.$roomtotal.';'.$gametype.';'.$betamount.';' | |
.$playeramount.';'.$player2.';'.$player3.';'.$player4.';' | |
.$roomuse.';'.$playerexit.';'.$guests.';'.$status.';'.$acceptguest.';' | |
.$chat1.'|'.$time1.';'.$chat2.'|'.$time2.';'.$chat3.'|'.$time3.';' | |
.$chat4.'|'.$time4.';'.$player1score.';'.$player2score.';'.$player3score.';' | |
.$player4score.';'.$player1update.';'.$player2update.';'.$player3update.';'.$player4update.';' | |
.$elapsed.';'.$gameid.';'.$referee.';'.$accountbal; | |
//^^^ BUT "elapsed" is spelled correctly here! | |
// ^^^ You could replace the above with: | |
// echo implode(';', $dbData); | |
// But prior to that you'd have to do some array manipulation | |
// to inject "chat1|$time1" and $elapsed into the right spots | |
// within the array. | |
// Personally I would replace this bespke textual data format | |
// with JSON if possible. | |
$nowDaateTime = date('Y-m-d H:i:s'); | |
if ( $playerstat == 1 AND $getall == 0 ) | |
{ | |
$sql = 'UPDATE chat SET chat1= ? ,player1time=? WHERE room_id= ?'; | |
$player1time=date('Y-m-d H:i:s'); | |
$chat1 = filter_input(INPUT_GET, 'chat1'); // <<- Why wasn't this filtered ealier near the top? | |
$stmt = $conn->stmt_init(); | |
if ($stmt->prepare($sql)) | |
{ | |
$stmt->bind_param('ssi', $chat1,$player1time,$room_id); | |
$done = $stmt->execute(); | |
// ^^^ Again you can replace repetitive code like this with a simple reusable function | |
// db_update_one( $dbTable, $arrayOfFieldsAndValues, $whereField, $whereValue ); | |
// db_update_one( "chat", ['player1time'=>date('Y-m-d H:i:s'), 'chat1'=>$chat1], 'room_id', $room_id); | |
} | |
else { $getall = 1; } | |
} | |
elseif ( $playerstat == 2 ) | |
{ | |
$chat2 = filter_input(INPUT_GET, 'chat2'); | |
$outcome = db_update_one( "chat", array('player2time'=>$nowDaateTime,'chat2'=>$chat2), 'room_id', $room_id); | |
if( $outcome !== true ) | |
{ $getall = 1; } | |
} | |
elseif ( $playerstat == 3 ) | |
{ | |
$chat3 = filter_input(INPUT_GET, 'chat3'); | |
$outcome = db_update_one( "chat", array('player3time'=>$nowDaateTime,'chat3'=>$chat3), 'room_id', $room_id); | |
if( $outcome !== true ) | |
{ $getall = 1; } | |
} | |
elseif ( $playerstat == 4 ) | |
{ | |
$chat4 = filter_input(INPUT_GET, 'chat4'); | |
$outcome = db_update_one( "chat", array('player4time'=>$nowDaateTime,'chat4'=>$chat4), 'room_id', $room_id); | |
if( $outcome !== true ) | |
{ $getall = 1; } | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment