Jump to content

Error: Could not create team. Column 'manager_id' cannot be null


Realest89

Recommended Posts

Hello im using GitHub to help build me a site because i have minimal experience with coding, I have my manager_id set to NOT NULL in my database and i keep getting this error 

Error: Could not create team. Column 'manager_id' cannot be null

Here is the code github keeps giving to clear the error but it doesn't seem to work.

// Handle team creation

if (isset($_POST['create_team'])) {

  // Validate form data

  $name = trim($_POST['teamname']);

  $tag = trim($_POST['tag']);

 

  if (empty($name)) {

    $errors[] = 'Team name is required.';

  }

 

  if (empty($tag)) {

    $errors[] = 'Tag is required.';

  }

 

  if (empty($errors)) {

    // Insert new team into the database

    $sql = "INSERT INTO teams (teamname, tag, manager_id) VALUES (?, ?, ?)";

    $stmt = mysqli_prepare($conn, $sql);

    mysqli_stmt_bind_param($stmt, 'ssi', $name, $tag, $_SESSION['user_id']);

    $result = mysqli_stmt_execute($stmt);

 

    if ($result === false) {

      $errors[] = 'Error: Could not create team. ' . mysqli_error($conn);

    } else {

      $team_id = mysqli_insert_id($conn);

 

      // Add the user to the team

      $sql = "INSERT INTO players (userid, playername, team_id) VALUES (?, ?, ?)";

      $stmt = mysqli_prepare($conn, $sql);

      mysqli_stmt_bind_param($stmt, 'isi', $_SESSION['user_id'], $_SESSION['username'], $team_id);

      $result = mysqli_stmt_execute($stmt);

     

      if ($result === false) {

        $errors[] = 'Error: Could not add user to team. ' . mysqli_error($conn);

      } else {

        header('Location: team.php?id=' . $team_id);

        exit;

      }

    }

  }

}

 

All help is appreciated!

Link to comment
Share on other sites

6 minutes ago, Barand said:

Looks like $_SESSION['user_id'] is NULL. Have you checked its value?

Name    Type      Collation         Attributes     Null        Default

user_id   int(11)   UNSIGNED                           No          None 

this is what i see in my database

Link to comment
Share on other sites

the session variable is a 'required' input to your code. you must 'validate' it before using it, i.e. the current user must be logged in, in order to be able to see the team creation form and process the form data. if there is not a logged in user, you must take an appropriate action and not allow the current user to see the form or be able to run the form processing code.

next, the only piece of user data you should store in a session variable upon successful login is the user_id. you should query on each page request to get any other user data, such as the username. this will allow this other user data to be edited and the change will take effect on the very next page request.

likewise, you should not store the username in the players table. this is duplicating data that's defined/stored elsewhere. if you have the player's user_id, you would query when needed to get the player's username.

if you were doing this for real, you would need to run the set of INSERT queries as part of a transaction, so that if either query failed with an error, they can both be rolled back. you also need to define the teams teamname and tag columns as unique indexes, to prevent duplicates, and have error handling that tests the result of the teams insert query for a duplicate index error (number.) the players userid and team_id columns must be defined as a composite unique index, to prevent duplicates (assuming that a player can only be on one team at a time), and have error handling that tests the result of the players insert query for a duplicate index error (number.)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.