猿问
下载APP

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

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说明是否有错误?


蝴蝶不菲
浏览 62回答 2
2回答

胡说叔叔

在else块中,您可以看到如何捕获MySQL错误并将其显示在警报中。if(isset($_POST['btnSubmit'])) {&nbsp; &nbsp; $name = mysqli_real_escape_string($conn,$_POST["name"]);&nbsp; &nbsp; $sql = "INSERT INTO isodetail(title) VALUES ('{$name}')";&nbsp; &nbsp; $run = mysqli_query($conn, $sql);&nbsp; &nbsp; if($run) {&nbsp; &nbsp; &nbsp; &nbsp; echo "<script>alert('Certi Added Successfully');window.open('isocerti.php','_self');</script>";&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $error = addslashes(mysqli_error($conn));&nbsp; &nbsp; &nbsp; &nbsp; echo "<script>alert('An Error occur: {$error}');</script>";&nbsp; &nbsp; }}参考:MySQLi-Error:http ://php.net/manual/en/mysqli.error.php转义特殊字符:http : //php.net/manual/en/function.addslashes.php (它的作用是这个例子)

慕婉清6462132

所以您有正确的错误,现在您可以开始调试它。我的第一个想法是:INSERT isodetail(title, name) VALUES ('{$name}', '')但这只是对数据库结构知识的猜测。
打开App,查看更多内容
随时随地看视频慕课网APP
我要回答