-1

I am trying to develop a registration system (I am developing this site-> bottlesbeach.eu just to train my practical knowledge on the backend part) that inserts the username, email, password, session string and taken string randomly from the avatar array which will mean the name of user avatar image:

        $avatars = array("avatar0.png", "avatar1.png", "avatar2.png", "avatar3.png", "avatar4.png");
        $k = array_rand($avatars);
        $v = $array[$k];
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        $query = "INSERT INTO users SET username = ?, email = ?, avatar = ?, password = ?, sessionid = ?";
        $stmt = $conn->prepare($query);
        $stmt->bind_param('sssss', $username, $email, $v, $password, $randomString);
        $stmt->execute();
        $sub = true;
        

This is the error that is thrown when the form is submitted:

Column 'avatar' cannot be null

I don't know why I'm getting this error, maybe the '$ v' variable is null although I don't know why it should be.

db table structure

Anyone know what caused this exception in my case? how can i solve?

  • 1
    What is `$conn`? What is `$conn->query`? Maybe you want `prepare` instead? – deceze Mar 29 '21 at 15:36
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Mar 29 '21 at 15:38
  • @inganna it keeps giving me that error there even though I entered prepare. – bottles beach Mar 29 '21 at 15:40
  • @Dharman I put die just for debugging purposes – bottles beach Mar 29 '21 at 15:41
  • 1
    Yeah, but please stop. You don't need to debug this way. If you enable automatic error reporting you will not miss any errors – Dharman Mar 29 '21 at 15:41
  • @deceze I have updated my application with the exception that I get – bottles beach Mar 29 '21 at 15:47
  • 1
    I suggest you use the alternative syntax: `INSERT INTO users SET username = NULL, email = ?, avatar = ?, password = ?, sessionid = ?`. This is a MySQL enhancement to standard SQL, but it makes it a lot easier to match the columns to the respective placeholders. – Bill Karwin Mar 29 '21 at 15:51
  • @Bill Karwin I don't know why I'm getting these cascading errors, now I get this: The "avatar" column cannot be anything – bottles beach Mar 29 '21 at 16:05
  • Please edit your question above and post the actual error. I don't know how to guess what "cannot be anything" means. – Bill Karwin Mar 29 '21 at 16:11
  • @Bill Karwin Sorry, I want to write null, not nothing. Now I change the title of the question – bottles beach Mar 29 '21 at 16:14
  • @BillKarwin I just updated the question – bottles beach Mar 29 '21 at 16:17
  • *Please, please, please* always code with error_reporting(E_ALL). It will save a damned ton of time to a lot of people – Your Common Sense Mar 29 '21 at 16:31

1 Answers1

0

Based on your error I can infer a couple of things:

  1. The avatar column in your table is declared with the NOT NULL option. It will not accept NULL as input.

  2. Your $v PHP variable is not set, or has been set to the PHP null value. I suggest you use error_log(print_r($v, true)); to check this.

To solve this, you must either set $v to a value that is not null, or else alter table to change your avatar column to permit NULLs.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828