javascriptphpmysqlialert

PHP MySQLi display error message in JavaScript alert


Is it possible to save the PHP/MySQLi "or die" error message in a variable and display it in a JavaScript alert window?

I am not using "or die" at all in the query below; is there a way to display the error in a JavaScript alert?

Here is the code that I'm using:

 <?php
   
   $update = "UPDATE table SET column1 = '$variable1', column2 = $variable2' WHERE UID = '$idVariable'";

   $query = mysqli_query($dbc, $update); // or die usually goes here

  if($query == false)
  {
    echo ("<script language='javascript'>
             window.alert('Error saving record.')
             window.location.href='javascript:history.back()'
           </script>");
  }
  else
  {
    echo ("<script language='javascript'>
             window.alert('Updated')
             window.location.href='main.php'
           </script>");
  } 

As you see above, I run a query, and if it fails, it displays a JavaScript alert stating 'Error saving record' but not what the error is. Could be a duplicate or some other error.

I want to be able to show the exact error. Is there another way to achieve this?

I was thinking something like this might work:

 $query = mysqli_query($dbc, $update) or die($error = mysqli_error());

I try to display the variable $error in the JavaScript alert window, but it does not work.


Solution

  • There is no such case where you would need it.

    Again,

    System errors are not for users!

    So, users have nothing to do with system errors. Just say apologies.