我在这里有这个问题:
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);
}
返回:
Array (
[COUNT(`movement`)] => 0
)
Array (
[COUNT(`movement`)] => 0
)
哪个是对的。现在我想做这样的事情:
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;
}
这样我就可以在while循环中调用这个函数,如果它返回0 / false,它会将表单数据发布到db。有人可以帮忙吗?这是我尝试执行此功能的while循环:
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
答案 0 :(得分:1)
您的方法movement_performed_today
返回一个布尔值。不是你可以循环的东西。检查主循环。您也知道从_POST信任用户输入不安全吗?使用PDO和绑定参数需要考虑使用mysql_query。
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++;
}
}