我想使用復選框對我的網站上的個人資料進行設置。這些復選框也有效,但我不斷收到此錯誤:Fatal error : Uncaught mysqli_sql_exception: Column 'match_role_id' cannot be null 這可能是因為應在其中輸入值的表是一個臨時表,其中包含來自其他兩個表的兩個 ID。如果我將這兩個值的默認值設置為NULL,則會出現無限循環。我怎樣才能解決這個問題?功能:function add_team_match_role() { global $connection; if(isset($_POST['add_match_roles'])) { $match_role_checkbox = $_POST['match_role_check']; $team_id = escape($_POST['team_id']); for($i = 0; $i < $match_role_checkbox; $i++) { $team_id = escape($_POST['team_id']); $stmt = $connection->prepare("INSERT INTO game_role_team (match_role_id, team_id) VALUES (?, ?)"); $stmt->bind_param("ss", $match_role_checkbox[$i], $team_id); $stmt->execute(); $stmt->close(); } }}HTML:<div class="form-group"> <label for="match_role">Match Rollen</label> <?php $stmt = $connection->prepare("SELECT * FROM game_role"); $stmt->execute(); $result = $stmt->get_result(); while($row = $result->fetch_assoc()) { $match_role_id = $row['match_role_id']; $match_role_name = $row['match_role_name']; echo "<div class='form-check'> <input class='form-check-input' type='checkbox' name='match_role_check[]' value='$match_role_id'> <label class='form-check-label'>$match_role_name</label> </div>"; } $stmt->close(); ?></div>
1 回答

白板的微信
TA貢獻1883條經驗 獲得超3個贊
當未設置復選框時,則為$_POST['add_match_roles'][$i]
NULL。但不允許插入 NULL 或空字符串,因為該字段是整數。
通過驗證“set and notempty”來檢查角色是否被檢查(因為該值為“on”)。如果為空,則在檢查時給出零或一。
$isChecked = (int) !empty($_POST['add_match_roles'][$i]);
還將類型從字符串更正為整數
$stmt->bind_param("is", $isChecked, $team_id);
注意:
如果您只需要一個布爾值(true=1,false=0),請使用 TinyInt(1) UNSIGNED。
- 1 回答
- 0 關注
- 108 瀏覽
添加回答
舉報
0/150
提交
取消