-1

When my registration form tries to insert with user1 (which has been granted ALL PRIVILEGES) it works. But when I try to insert with user2 (which has also been granted ALL PRIVILEGES) it won't insert. I don't get any errors and have been troubleshooting for a couple of hours now and can't find a problem, better yet a solution.

User2 didn't have all privileges (like any sane person should do of course). When I tried granting it all privileges it still couldn't insert while user1 which has the same privileges can.

As I don't know where the problem is, I can only include the database connection code which seems to be right. If anything else is needed I'd gladly put it here.

$dbservername = "localhost";
$dbusername = "user1";
$dbpassword = "password1";
//$dbpassword = "password2"; (I keep this here to change users fast when troubleshooting and yes I change the username everytime)
$dbname = "database1";

$db = new mysqli($dbservername, $dbusername, $dbpassword, $dbname);

I expected user2 to insert into the database but it won't. I don't have any error messages.

Privileges:

enter image description here

EDIT:

I have tried running the code Martin gave and both users connect...

Community
  • 1
  • 1
Shmotten
  • 11
  • 4

1 Answers1

0

Please use the below code to give yourself error feedback:

$dbServerName = "localhost";
$dbUsername = "user1";
$dbPassword = "password1";
$dbName = "database1";

    $mysqliDriver = new \mysqli_driver();
    $mysqliDriver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;

    try {
        $db = new \mysqli($dbServerName, $dbUsername, $dbPassword, $dbName);
    }
    catch(\mysqli_sql_exception | \Exception $em) { // format requires PHP 7.2
        error_log(print_r($em,true)); // READ THIS!!! 
        die("failed. Check the error log!");
    }
    die("We are connected!");

This sets up a MySQLi Driver reporting mode which can give i) Exception reporting and ii) more informative/easier to access reasons for failure.

Please let us know (Update/Edit your question) what the error reporting catches when connecting with User2.

NOTE: If you do not have PHP7.2 running replce that line with:

catch(\Exception $em)

NOTE:

Your pastebin code; you are creating the $db as a PHP object but are using it with static commands (mysqli_query($db, $sql) etc.) as if it was not an object (procedural). This is inconsistent and will give you issues.

Plese read here how to correctly structure your PHP / SQL interface

Martin
  • 22,212
  • 11
  • 70
  • 132
  • I have edited the question and added the results. Nothing special so far, as for your note; I'm pretty new to SQL and more advanced PHP so I don't really understand how to fix the consistency. – Shmotten Aug 28 '19 at 16:10
  • @Shmotten if my code runs ok and solves your problem please +1 and tick the tick. Thank you. – Martin Aug 28 '19 at 16:36
  • Isn't it weird that this fixed the problem? It was just a test... however it did work so thank you anyways! – Shmotten Aug 28 '19 at 17:11
  • I've just tested it again and it doesn't insert, while I thought it did. – Shmotten Aug 28 '19 at 18:26
  • @Shmotten you seem to be missing the point -- my answer is not a full answer, it is a request to check that the basic connection works. If you DO NOT get an exception error when loading the above code; then it works and your Username/Password on MySQL is NOT the issue. – Martin Aug 28 '19 at 20:28
  • I stll maintain your issue may well be actually using OO and Procedural MySQLi on the same object. *This* needs to be fixed. – Martin Aug 28 '19 at 20:29
  • Yeah I get it now, it doesn't seem to be a username/password problem. But what could be the problem? Is it the other issue you mentioned? Because I don't really understand how to fix it. – Shmotten Aug 29 '19 at 09:17