0

I have this query here:

function movement_performed_today($class_id, $client_id){
$class_id = (int)$class_id;
$client_id = (int)$client_id;

$query = mysql_query("SELECT COUNT(`movement`) FROM `completed_movements` WHERE `class_id` = '$class_id' AND `client_id` = '$client_id' AND `date` = CURDATE()");

$movement_performed = mysql_fetch_assoc($query);

print_r($movement_performed);   
}

That returns this:

Array ( 
[COUNT(`movement`)] => 0 
) 
Array ( 
[COUNT(`movement`)] => 0 
)

Which is correct. Now I want to do something like this:

function movement_performed_today($class_id, $client_id){
$class_id = (int)$class_id;
$client_id = (int)$client_id;

$query = mysql_query("SELECT COUNT(`movement`) FROM `completed_movements` WHERE `class_id` = '$class_id' AND `client_id` = '$client_id' AND `date` = CURDATE()");

$movement_performed = mysql_fetch_assoc($query);

return (mysql_result($movement_performed['count'], 0) == 1) ? true : false; 
}

So that I can call this function inside of a while loop and if it returns 0/false it will then post the form data to the db. Can someone help? Here is the while loop where Im trying to perform this function:

if (empty($_POST)=== false){
$i = 0;
while (isset($_POST["first_name"][$i])) {
    $movement_data = array(   
    'user_id'       => $session_user_id,   
    'class_id'      => $class_id,   
    'class_name'    => $class_name,
    'client_id'     => $_POST['client_id'][$i],    
    'first_name'    => $_POST['first_name'][$i],   
    'last_name'     => $_POST['last_name'][$i],   
    'nickname'      => $_POST['nickname'][$i],   
    'order'         => $_POST['order'][$i],   
    'movement'      => $_POST['movement'][$i],   
    'rep_set_sec'   => $_POST['rep_set_sec'][$i],   
    'rest'          => $_POST['rest'][$i],   
    'date'          => $today   
    );  

   $movement_performed = movement_performed_today($class_id, $_POST['client_id']);
   foreach ($movement_performed as $performed){
   if($performed == false){
    completed_movement($movement_data);
    } // if performed
  } // foreach
   $i++; 
   } // while

} // if empty   
12
  • Insert bowel joke here. Seriously though, Should probably look at enforcing uniqueness with database constraints. Can still do the SELECT COUNT check but ensure that any race conditions are handled properly once trying to insert to DB. Can you explain what you mean by "for a single form but not an array"? Commented Aug 27, 2012 at 20:44
  • The parsing scripts loops through rows in a form and inserts each row of data in db with each row of data in the form representing a new row of data in the db. So when I do my check I need to see if the $_POST['movement'] in each row has already been posted in the db and if so go to the next row in the array. Does that make sense? Commented Aug 27, 2012 at 20:51
  • 1
    So do the same thing but in a loop? Just need to make your call to movement_performed_today in the loop before each insert no? Commented Aug 27, 2012 at 21:00
  • Please DO NOT USE mysql_query in new applications. It's being phased out of PHP. You should be using mysqli or PDO for all new code. Clever tricks like casting to int are no substitute for proper SQL escaping. If you develop bad habits like that, one day you will make a mistake, and those can cost you dearly. Commented Aug 27, 2012 at 21:20
  • I edited my parsing script to call my function but its not working. It seems to be just getting stuck. I've tried to echo back variables in the loop just to see what I was getting, but no such luck. Evidently I'm not doing something right here. Commented Aug 27, 2012 at 22:02

1 Answer 1

1

Your method movement_performed_today returns a Boolean. Not something you can loop on. Do the check in your main loop. Also you are aware that trusting user input from a _POST is not secure? The use of mysql_query is depreciated you need to look at using PDO and bound parameters.

if (! empty($_POST)) {
    $i = 0;
    while (isset($_POST["first_name"][$i])) {
        $movement_data = array(   
        'user_id'       => $session_user_id,   
        'class_id'      => $class_id,   
        'class_name'    => $class_name,
        'client_id'     => $_POST['client_id'][$i],    
        'first_name'    => $_POST['first_name'][$i],   
        'last_name'     => $_POST['last_name'][$i],   
        'nickname'      => $_POST['nickname'][$i],   
        'order'         => $_POST['order'][$i],   
        'movement'      => $_POST['movement'][$i],   
        'rep_set_sec'   => $_POST['rep_set_sec'][$i],   
        'rest'          => $_POST['rest'][$i],   
        'date'          => $today);  

        //check not already performed today
        $isPerformed = movement_performed_today($class_id, $_POST['client_id']);

        //if not performed then do insert
        if(! $isPerformed) completed_movement($movement_data);    

        $i++; 
    }    
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. So how does my query need to change so that if it returns 1/true it will skip the completed_movement function?
thanks for your help. Does my query need to change to make this operate properly?

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.