I have 1 file whichs uses PhP, JavaScript, HTML, AJAX, CSS, MySQLi and SQL that is basically a page with some fields and then it sends the information to a database using AJAX to do the connection without refreshing the page.
Everything is working except that I can't make it show errors with the or die()
. I tested it out by changing one of the values in the my_table(...)
which makes it not send information to the database but the user has no ideia because there's no error message. How can I make it throw errors?
I have no ideia what I'm doing wrong but if I had to bet it would be the AJAX not refreshing the page or something. I have seen some questions out there which I'm sure they explain exactly this but the jQuery confuses me all the time.
Here's my code with some modifications to reduce the non-important stuff:
<?php
if(isset($_POST["name"]) && !empty($_POST["name"])) {
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
mysqli_set_charset($link, "utf8");
// code
mysqli_query($link, "INSERT INTO my_table(...)values(...)") or die(mysqli_error($link));
mysqli_close($link);
exit;
}
?>
<html>
<head>
<meta charset="UTF-8">
<script>
function AJAX() {
// code
var xhttp = new XMLHttpRequest();
xhttp.open('POST', 'my_file.php', true);
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.send(...);
}
</script>
</head>
</body>
<!-- code -->
<button onclick="AJAX()">Send</button>
</body>
</html>
Edit to explain better the situation:
If I use this simple code it works fine:
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_query($link,"INSERT INTO my_table(Name) VALUES ('Bob')") or die(mysqli_error($link));
mysqli_close($link);
?>
But, for example, when I change my_table
to a wrong name which doesn't exist it writes in the document Table 'my_db.wrong_table_name' doesn't exist
. While in my file it doesn't write/show/throw nothing at all and it makes the user think it worked when it didn't.