I am trying to find out a way to check of I need to update or insert a new entry in the database table but I am trying for days now and not get it to work in any way. PLEASE HELP! :puzzle:
Here is the code I got so far:
if (isset ( $_POST ['pointsform'] )) {
//post vals
$points_added = $_POST ['points_added'];
if (! $points_added) {
// not sure of this works :S
$points_added = 0;
}
$points_rested = $_POST ['points_rested'];
if (!$points_rested) {
$points_rested = 0;
}
$pointcomments = $_POST ['pointcomments'];
$form_earner_id = $_SESSION ['earner_id'];
$form_goal_id = $_SESSION ['goal_id'];
$form_goal_name = $_SESSION ['goal_name'];
///Get points needed for reward
$query = "SELECT * FROM goals ";
$query .= "WHERE id = {$form_goal_id} ";
$result_set = mysql_query ( $query, $connection );
confirm_query ( $result_set );
$goal_set = mysql_fetch_array ($result_set );
//end points needed for reward
//for some reason this not always works :S
$points_needed = $goal_set['points_required'];
///Get points earned for reward
$query = "SELECT * FROM points ";
$query .= "WHERE id = {$form_goal_id} ";
$result_set = mysql_query ( $query );
confirm_query ( $result_set );
$points_set = mysql_fetch_array ( $result_set );
$num_rows = mysql_num_rows($result);
//end points earned for reward
$points_earned = $points_set ['goal_pointsearned'];
if ($points_earned == NULL) {
$points_earned = 0;
}
$form_date = date ( 'l jS \of F Y h:i A' );
//there much be a better day to get dates in mysql?
$redeemed = NULL;
//count points (also not works grrrr!!)
$points = $points_earned + $points_added - $points_rested;
if ($points >= $points_needed) {
$points_earned = $points_needed;
// add date to redeemed here
$redeemed = $form_date;
}
//history table
$query = "INSERT INTO pointshistory (
pointearners_id, member_id, award_id, comments, pointsadded, pointsrested, dateawared, awaredby
) VALUES (
{$form_earner_id}, {$_SESSION['kt_login_id']}, {$form_goal_id}, '{$pointcomments}', {$points_added}, {$points_rested}, '{$form_date}', '{$_SESSION['kt_login_user']}'
)";
$result = mysql_query ( $query );
if (mysql_affected_rows () == 1) {
$goodmessage = "";
} else {
$message = "Our Database is offline at the moment please try later again";
$message .= "<br />" . mysql_error ();
}
// here is the problem table it does do the insert but the update not working
if ($points_earned != 0) {
$query = "INSERT INTO points (
pointearners_id, member_id, award_id, goal_name, goal_pointsneeded, goal_pointsearned, redeemed
) VALUES (
{$form_earner_id}, {$_SESSION['kt_login_id']}, {$form_goal_id}, '{$form_goal_name}', {$points_needed}, {$points}, '{$redeemed}'
)";
$result = mysql_query ( $query );
if (mysql_affected_rows () == 1) {
redirect_to ( 'status.php' );
} else {
$message = "Something did go wrong please try later again";
}
} else {
//this does not work one bit even when I use update alone it fails and I not sure whats wrong with it.
$query = "UPDATE points SET pointearners_id = {$form_earner_id}, member_id = {$_SESSION['kt_login_id']}, goal_name = '{$form_goal_name}', goal_pointsneeded = {$points_needed}, goal_pointsearned = {$points},
redeemed = '{$redeemed}'
WHERE award_id = {$form_goal_id}";
$result = mysql_query ( $query );
// test to see if the update occurred
if (mysql_affected_rows () == 1) {
// Success!
redirect_to ( 'status.php' );
}
}
}
if (isset ( $_GET ['select'] ) && $_GET ['select'] != 0 && isset ( $_GET ['select1'] ) && $_GET ['select1'] != 0) {
$earnerID = $_GET ['select'];
$_SESSION ['earner_id'] = $earnerID;
$fullname = get_pointearner_name ( $_SESSION ['earner_id'] );
$_SESSION ['earner_name'] = $fullname ['name'];
$goalID = $_GET ['select1'];
$_SESSION ['goal_id'] = $goalID;
$fullgoal = get_goal_name ( $_SESSION ['goal_id'] );
$_SESSION ['goal_name'] = $fullgoal ['award_name'];
$_SESSION ['goalpointsneeded'] = $fullgoal ['goal_pointsneeded'];
}
?>