-2

HTML code

<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 code

<?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>";
    }
}

 ?>

This shows an alert message saying Something Error!..please try Again.. so is it mysqli_query that has a mistake?

Jeff
  • 12,555
  • 5
  • 33
  • 60
Archi Patel
  • 65
  • 2
  • 14
  • Use the `mysqli_error` function: http://php.net/manual/en/mysqli.error.php Example: `echo mysqli_error($conn);` – Steven Jan 16 '18 at 06:36
  • it doesnt show any errors – Archi Patel Jan 16 '18 at 06:50
  • @Sandra read about [variable parsing in strings](http://php.net/manual/en/language.types.string.php#language.types.string.parsing). That code is correct as it is now. – axiac Jan 16 '18 at 07:05
  • Related: [how-to-get-mysqli-error-information-in-different-environments](https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments) – Paul Spiegel Apr 02 '19 at 17:17

1 Answers1

-1

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>";
    }
}

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)

Steven
  • 381
  • 2
  • 10
  • @ArchiPatel So you have your correct error, now you can start debug it. My first idea will be: `INSERT isodetail(title, name) VALUES ('{$name}', '')` but that's just a guess without knowledge about your database structure. – Steven Jan 16 '18 at 07:29
  • yes i got this ...Thank you so much. – Archi Patel Jan 16 '18 at 07:36