我正在尝试通过多个值发送到INSERT字符串,但该值返回NULL。
我正在尝试将两个值发送到单个表,成员Id和团队ID,团队ID是复选框值,其中此示例中的成员ID保持不变,因此我的表收集整数,如此:
teamMember (table name)
id, memberId, teamId
1, 1, 2
2, 1, 1
3, 2, 3
我希望你明白这一点。
这是我的插入内容:
$memberId = $_POST['memberId'];
$teamId = $_POST['teamId'];
foreach($teamId as $val){
$sql = "INSERT INTO teamMember (teamId, memberId) VALUES ('$val','$memberId')";
}
if ($conn->query($sql) === TRUE) {
}
我的表单如下:
<form action="cmt.php" method="POST">
<input type="hidden" name="memberId" value="<?php echo $memberId; ?>">
<?php
$sql = "SELECT * FROM team WHERE dashboardId = $dashboardId";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo '<input name="teamId[]" type="checkbox" value="' . $row["id"] . '">' . $row["name"] . '<br><br>';
}
}
?>
<form>
我只需要将复选框值作为一个数组来处理,这样我就可以添加多个团队ID以及我选择团队的用户ID。
问题是我如何插入多行,因为这似乎每次只插入一行。
答案 0 :(得分:1)
您传递了错误的变量
$memberId = $_POST['memberId'];
$teamId = implode(',', $_POST['teamId']);
$sql = "INSERT INTO teamMember (teamId, memberId) VALUES ('$teamId', '$memberId')"; //<----teamID instead of dashboardId
if ($conn->query($sql) === TRUE) {
}
修改强>
输入单独的行:
$memberId = $_POST['memberId'];
//$teamId = implode(',', $_POST['teamId']);
$teamId = $_POST['teamId'];
$sql = "INSERT INTO teamMember (teamId, memberId) ";
$sql_concat="";
foreach ($teamId as $team) {
if($sql_concat == "")
$sql_concat .= " VALUES ('$team', '$memberId')";
else
$sql_concat .= " , ('$team', '$memberId')";
}
$sql .= $sql_concat;
if ($conn->query($sql) === TRUE) {
}
答案 1 :(得分:0)
这样的例子:
$teamId = $_POST['teamId'];
$memberId = $_POST['memberId'];
foreach($teamId as $val){
//I am confused with the $dashboardId, i supposed its $teamId?
$sql = "INSERT INTO teamMember (teamId, memberId) VALUES ('".$val."','".$memberId."')";
//execute the query here
}
或强>
$teamId = $_POST['teamId'];
$memberId = $_POST['memberId'];
$sql = "INSERT INTO teamMember (teamId, memberId)"
$c = 0;
foreach($teamId as $val){
if($c == 0){
$sql.=" VALUES ('".$val."','".$memberId."')";
}else{
$sql.=", ('".$val."', '".$memberId."')";
}
$c++;
}
//execute query here
if ($conn->query($sql) === TRUE) {
}
$val
是每个复选框的值