gpt4 book ai didi

php - 如何在 php 中的 mysql 查询中获取有用的错误消息?

转载 作者:行者123 更新时间:2023-11-29 16:08:47 25 4
gpt4 key购买 nike

HTML 代码

<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有错误吗?

最佳答案

在 else block 中,您可以看到如何捕获 MySQL 错误并将其显示在警报中。

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 (它完成了本例的工作)

关于php - 如何在 php 中的 mysql 查询中获取有用的错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55479941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com