[英]If/else error message to PHP MYSQL query
I have a simple query (AJAX). 我有一个简单的查询(AJAX)。 I noticed that if the $insert_stmt
does not execute I still got a success message. 我注意到,如果$insert_stmt
不执行,我仍然会收到一条成功消息。
if ($insert_stmt = $mysqli->prepare("INSERT INTO `alerte` (`nom_alerte`, `country`, `date_debut_alerte`) VALUES (?, ?, ?)")) {
$insert_stmt->bind_param('sss', $alert_name, $country, $start_alert_date);
$insert_stmt->execute();
$reponse = 'success';
} else {
$reponse = 'Sorry, a database error occurred; please try later';
}
echo json_encode(["reponse" => $reponse]);
I would like to know if there is a way of adding another if/else statement to verify the execute 我想知道是否可以添加另一个if / else语句来验证执行
I tried something like that, but I am not sure it is good coding: 我尝试过类似的方法,但是我不确定它的编码是否正确:
if ($insert_stmt = $mysqli->prepare("INSERT INTO `alerte` (`nom_alerte`, `country`, `date_debut_alerte`) VALUES (?, ?, ?, ?)")) {
$insert_stmt->bind_param('sss', $alert_name, $country, $start_alert_date);
// inserted part if/else check
if (! $insert_stmt->execute()) {
$reponse = 'Sorry, a database error occurred; please try later';
} else {
$reponse = 'success';
}
} else {
$reponse = 'Sorry, a database error occurred; please try later';
}
Is someone able to tell me if it is suitable or if there is a better way? 有人可以告诉我是否合适或是否有更好的方法?
Please debug you code step wise like this. 请像这样逐步调试代码。
It will surely give your err, if you have. 如果有的话,它一定会给您带来错误。
if ($stmt = $mysqli->prepare("INSERT INTO `alerte` (`nom_alerte`, `country`, `date_debut_alerte`) VALUES (?, ?, ?, ?)")) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!$stmt->bind_param('sss', $alert_name, $country, $start_alert_date)){
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}else {
$reponse = 'success';
}
Once you get err. 一旦犯错。 You can change your code accordingly later. 您可以稍后相应地更改代码。
I hope this will help you to find your err easily! 希望这可以帮助您轻松找到错误!
I'm not sure about the types of the columns from the alerte
-Table, but i think the errors are the forgotten quotes at each insert value... 我不确定来自alerte
的列的类型,但是我认为错误是每个插入值处被遗忘的引号...
try this: 尝试这个:
INSERT INTO `alerte` (`nom_alerte`, `country`, `date_debut_alerte`) VALUES ('?', '?', '?')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.