如何在php的mysql查询中获取有用的错误消息? [英] How to get useful error messages in mysql query in php?
本文介绍了如何在php的mysql查询中获取有用的错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<form class="col s6 " method="post" enctype="multipart/form-data">
<div class="input-field col s12">
<input id="last_name" type="text" name="name" class="validate">
<label for="last_name">Certificate Name</label>
</div>
<button type="submit" id="btnSubmit" name="btnSubmit" class="btn btn-default" style="margin-top:20px;">ADD</button>
</form>
PHP代码
<?php
include('footer.php');
include('conn.php');
if(isset($_POST['btnSubmit']))
{
$name = mysqli_real_escape_string($conn,$_POST["name"]);
$sql = "INSERT INTO `isodetail`(`title`) VALUES ('$name')";
$run = mysqli_query($conn, $sql);
if($run)
{
echo "<script>alert('Certi Added Successfully')</script>";
echo "<script>window.open('isocerti.php','_self')</script>";
}
else
{
echo "<script>alert('Something Error!..please try Again..')</script>";
}
}
?>
这将显示一条警告消息,提示Something Error!..please try Again..
那么mysqli_query
是否有错误?
This shows an alert message saying Something Error!..please try Again..
so is it mysqli_query
that has a mistake?
推荐答案
在else块中,您可以看到如何捕获MySQL错误并将其显示在警报中.
In the else-block you can see, how you can catch up MySQL-errors and display them in an alert.
if(isset($_POST['btnSubmit'])) {
$name = mysqli_real_escape_string($conn,$_POST["name"]);
$sql = "INSERT INTO isodetail(title) VALUES ('{$name}')";
$run = mysqli_query($conn, $sql);
if($run) {
echo "<script>alert('Certi Added Successfully');window.open('isocerti.php','_self');</script>";
} else {
$error = addslashes(mysqli_error($conn));
echo "<script>alert('An Error occur: {$error}');</script>";
}
}
参考:
MySQLi错误: http://php.net/manual/en/mysqli.error. php
转义特殊字符: http://php.net/manual/en/function.addslashes. php (在此示例中完成工作)
References:
MySQLi-Error: http://php.net/manual/en/mysqli.error.php
Escape special characters: http://php.net/manual/en/function.addslashes.php (it does the job for this example)
这篇关于如何在php的mysql查询中获取有用的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文