我想使用復(fù)選框?qū)ξ业木W(wǎng)站上的個(gè)人資料進(jìn)行設(shè)置。這些復(fù)選框也有效,但我不斷收到此錯(cuò)誤:Fatal error : Uncaught mysqli_sql_exception: Column 'match_role_id' cannot be null 這可能是因?yàn)閼?yīng)在其中輸入值的表是一個(gè)臨時(shí)表,其中包含來自其他兩個(gè)表的兩個(gè) ID。如果我將這兩個(gè)值的默認(rèn)值設(shè)置為NULL,則會(huì)出現(xiàn)無限循環(huán)。我怎樣才能解決這個(gè)問題?功能: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貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊
當(dāng)未設(shè)置復(fù)選框時(shí),則為$_POST['add_match_roles'][$i]
NULL。但不允許插入 NULL 或空字符串,因?yàn)樵撟侄问钦麛?shù)。
通過驗(yàn)證“set and notempty”來檢查角色是否被檢查(因?yàn)樵撝禐椤皁n”)。如果為空,則在檢查時(shí)給出零或一。
$isChecked = (int) !empty($_POST['add_match_roles'][$i]);
還將類型從字符串更正為整數(shù)
$stmt->bind_param("is", $isChecked, $team_id);
注意:
如果您只需要一個(gè)布爾值(true=1,false=0),請(qǐng)使用 TinyInt(1) UNSIGNED。
- 1 回答
- 0 關(guān)注
- 106 瀏覽
添加回答
舉報(bào)
0/150
提交
取消