如果代码有错误,请告诉我,从语法上讲一切都很好。 我要用这个脚本做什么?
1)检查app_cleaners_tb
表中的登录名和密码。
2)如果在表中找到条目,请检查activate_flag_cleaner
表中app_cleaners_tb
的值是否为空。
2.1)如果表activate_flag_cleaner
的值app_cleaners_tb
为空,则更新。
3)如果activate_flag_cleaner
的输入值不为空,则在app_cleaner_settings_tb
表中插入具有值的记录。
代码是否有运行的机会?
<?php
require("config.php");
if (!empty($_POST)){
$response = array("error" => FALSE);
//Login and password verification
$query = "SELECT 1 FROM app_cleaners_tb WHERE id_cleaner = :id_cleaner AND password_cleaner = :password_cleaner LIMIT 1";
//Prepare array for verify the login and password
$query_params = array(
':id_cleaner' => $_POST['id_cleaner'],
//The password in the database is not hashed, this is normal in this case! Don't worry about it =)
':password_cleaner' => $_POST['password_cleaner'],
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["error"] = true;
$response["message"] = "Database Error [Error 0001]. Please try again!";
die(json_encode($response));
}
$row = $stmt->fetch();
// If the record of id_cleaner is found (Username and password are accepted)
if($row){
$response["error"] = FALSE;
$response["message"] = " s/n: $id_cleaner and password are entered correctly";
echo json_encode($response);
//Check whether the entry in the column activate_flag_cleaner is empty in the app_cleaners_tb table
//- set the flag activate_flag_cleaner=1 into app_cleaners_tb table
if (empty($row['activate_flag_cleaner'])) {
$query = "UPDATE app_cleaners_tb SET activate_flag_cleaner = :activate_flag_cleaner,
activate_at_cleaner =: activate_at_cleaner
WHERE id_cleaner = :id_cleaner";
$activate_flag_cleaner = '1';// flag
$activate_at_cleaner = new DateTime();// current date
//prepare an array for operations
$update_params = array(
':activate_flag_cleaner' => $_POST['activate_flag_cleaner'],
':activate_at_cleaner' => $_POST['activate_at_cleaner'],
':id_cleaner' => $_POST['id_cleaner']
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($update_params);
}
catch (PDOException $ex) {
$response["error"] = TRUE;
$response["message"] = "Database Error [Error 0002]. Please try again!";
die(json_encode($response));
}
//if everything is fine send json notification
$response["error"] = FALSE;
$response["message"] = "Activating the S / N $ id_cleaner device
produced in $ activate_at_cleaner";
echo json_encode($response);
}
//if the activate_flag_cleaner column is not empty
$query = "INSERT INTO app_cleaner_settings_tb (id_cleaner,unique_id, param_1, param_2,
param_3, param_4, autoclaen_flag_cleaner,
param_5,param_6, updated_settings_cleaner)
VALUES ( :id_cleaner, :unique_id,:param_1,:param_2,
:param_3,:param_4,:autoclaen_flag_cleaner,
:param_5,:param_6,updated_settings_cleaner=NOW()
)";
$incert_params = array(
'id_cleaner' => $_POST['id_cleaner'],
'unique_id' => $_POST['unique_id'],
'param_1'=> $_POST['param_1'],
'param_2'=> $_POST['param_2'],
'param_3' => $_POST['param_3'],
'param_4'=> $_POST['param_4'],
'autoclaen_flag_cleaner'=> $_POST['autoclaen_flag_cleaner'],
'param_5'=> $_POST['param_5'],
'param_6'=> $_POST['param_6'],
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($incert_params);
}
catch (PDOException $ex) {
$response["error"] = TRUE;
$response["message"] = "Database Error [Error 0003]. Please try again!";
die(json_encode($response));
}
/*if everything is fine send json notification*/
$response["error"] = FALSE;
$response["message"] = "Device s / n: $ id_cleaner connected!";
echo json_encode($response);
}
else{
$response["error"] = TRUE;
$response["message"] = " Invalid Serial number or password, [Error 0004]. Please try again!";
die(json_encode($response));
}
}
?>
非常感谢您的帮助。最好的问候。