0

I'm trying to insert into my database and have been frustratingly not been able to get my statement(s) to work. I'm using PHP's MySQL Improved (mysqli) procedural interface. It might be worth noting that I'm using the c9.io IDE (pre-AWS) and everything including the server that my application is running on is through c9.

What I've noticed is that the statements have been working randomly. Initially, I was making very subtle changes to my INSERT statements until it worked, but after the working trial, it would fail again. So, eventually I started hitting the refresh button (same inputs, no modifications to my code) repeatedly until I hit a success.

In terms of code:

$sql = "INSERT INTO `users` (`email`,`password`) VALUES ('example@mail.com','1234')";

$result = mysqli_query($connection,$sql);

gives

$result = false 

very consistently, but every random nth trial

$result = true 

(same inputs, no change to my code).

I do not believe it is an error with my SQL syntax considering the random successes, nor do I believe it is an error with my connection. All of my SELECT statements have been working fine.

Thus, I have a hunch that for some reason it may be an issue with c9? If you have ever had a similar issue with any of MySQL, SQL, PHP, or c9, please help!

koolahman
  • 405
  • 3
  • 12
  • When `$result` is `false`, what does [`mysqli_error()`](http://docs.php.net/manual/en/mysqli.error.php) give? – ChrisGPT was on strike Apr 27 '18 at 01:31
  • do more than jsut return the result of `mysqli_query` check for errors –  Apr 27 '18 at 01:31
  • You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Use [`mysqli_error()`](http://php.net/manual/en/mysqli.error.php) to get a detailed error message from the database. – John Conde Apr 27 '18 at 01:32

2 Answers2

0

Use myqli_error() which will give you a error message which should help clarify the issue with your code

user39239
  • 17
  • 4
0

You Should try this
<?php
  if (!mysqli_query($connection,"INSERT INTO Persons (FirstName) VALUES ('Glenn')"))
  {
    echo("Error description: " . mysqli_error($connection));
  }
?>
Mohini
  • 268
  • 3
  • 15