Realest89 Posted October 29, 2023 Share Posted October 29, 2023 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! Quote Link to comment https://forums.phpfreaks.com/topic/317399-error-could-not-create-team-column-manager_id-cannot-be-null/ Share on other sites More sharing options...
Barand Posted October 29, 2023 Share Posted October 29, 2023 Looks like $_SESSION['user_id'] is NULL. Have you checked its value? Quote Link to comment https://forums.phpfreaks.com/topic/317399-error-could-not-create-team-column-manager_id-cannot-be-null/#findComment-1612592 Share on other sites More sharing options...
Realest89 Posted October 29, 2023 Author Share Posted October 29, 2023 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 Quote Link to comment https://forums.phpfreaks.com/topic/317399-error-could-not-create-team-column-manager_id-cannot-be-null/#findComment-1612593 Share on other sites More sharing options...
Barand Posted October 29, 2023 Share Posted October 29, 2023 What does this output? ... var_dump($_SESSION['user_id']); Quote Link to comment https://forums.phpfreaks.com/topic/317399-error-could-not-create-team-column-manager_id-cannot-be-null/#findComment-1612594 Share on other sites More sharing options...
Realest89 Posted October 29, 2023 Author Share Posted October 29, 2023 8 minutes ago, Barand said: What does this output? ... var_dump($_SESSION['user_id']); NULL Quote Link to comment https://forums.phpfreaks.com/topic/317399-error-could-not-create-team-column-manager_id-cannot-be-null/#findComment-1612595 Share on other sites More sharing options...
Barand Posted October 29, 2023 Share Posted October 29, 2023 Q.E.D. Youe need to ensure you only attempt to update the database when $_SESSION['user_id'] has a value. Somethng like this at the start of your script ... if (!isset($_SESSION['user_id'])) { header("Location: login.php"); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/317399-error-could-not-create-team-column-manager_id-cannot-be-null/#findComment-1612596 Share on other sites More sharing options...
mac_gyver Posted October 29, 2023 Share Posted October 29, 2023 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.) Quote Link to comment https://forums.phpfreaks.com/topic/317399-error-could-not-create-team-column-manager_id-cannot-be-null/#findComment-1612599 Share on other sites More sharing options...
Realest89 Posted October 29, 2023 Author Share Posted October 29, 2023 Thank you for the information ill take that information into my writing and see where it takes me! does this forum have like a discord where there is support with window sharing at all? Quote Link to comment https://forums.phpfreaks.com/topic/317399-error-could-not-create-team-column-manager_id-cannot-be-null/#findComment-1612604 Share on other sites More sharing options...
Barand Posted October 29, 2023 Share Posted October 29, 2023 7 minutes ago, Realest89 said: does this forum have like a discord See big green button at top of the page. Quote Link to comment https://forums.phpfreaks.com/topic/317399-error-could-not-create-team-column-manager_id-cannot-be-null/#findComment-1612605 Share on other sites More sharing options...
Realest89 Posted October 29, 2023 Author Share Posted October 29, 2023 2 minutes ago, Barand said: See big green button at top of the page. Awesome thanks! Quote Link to comment https://forums.phpfreaks.com/topic/317399-error-could-not-create-team-column-manager_id-cannot-be-null/#findComment-1612606 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.