dsjuimtq920056 2015-06-19 21:40
浏览 91

在mysql中插入多个复选框

So i would like to insert multiple checkbox into mysql but cant figured it ou how.

Selecting from database and inserting:

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO tournamenteams (id, id_tournament, id_team) VALUES (%s, %s, %s)",
   GetSQLValueString($_POST['id'], "int"),
   GetSQLValueString($_POST['id_tournament'], "int"),
   GetSQLValueString($_POST['id_team'], "int"));


mysql_select_db($database_searchon, $searchon);
$Result1 = mysql_query($insertSQL, $searchon) or die(mysql_error());
}

And now i created a form and inside the form theres the checkbox. i select database records and show it on multiple checkbox and then what i want to do is selecting the checkboxs i want it and insert it all when i submit the form, but its only submit the first checkbox i mark.

Form:

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<select name="id_tournament">
<?php 
do {  
?>
<option value="<?php echo $row_tournaments['id_tournament']?>" ><?php echo $row_tournaments['id_tournament']?></option>
<?php
} while ($row_tournaments = mysql_fetch_assoc($tournaments));
?>
</select>
<?php do { ?>
<?php echo $row_teams['id_team']?>:<input type="checkbox" name="id_team" value="<?php echo $row_teams['id_team']?>" />
<?php } while ($row_teams = mysql_fetch_assoc($teams)); ?>
<input type="submit" value="Insert record" />
<input type="hidden" name="id" value="" />
<input type="hidden" name="MM_insert" value="form1" />
</form>

Checkbox:

<?php do { ?>
<?php echo $row_teams['id_team']?>:<input type="checkbox" name="id_team" value="<?php echo $row_teams['id_team']?>" />
<?php } while ($row_teams = mysql_fetch_assoc($teams)); ?>

Entire Code for a better view:

<?php require_once('Connections/searchon.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;    
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}



if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO tournamenteams (id, id_tournament, id_team) VALUES (%s, %s, %s)",
   GetSQLValueString($_POST['id'], "int"),
   GetSQLValueString($_POST['id_tournament'], "int"),
   GetSQLValueString($_POST['id_team'], "int"));


mysql_select_db($database_searchon, $searchon);
$Result1 = mysql_query($insertSQL, $searchon) or die(mysql_error());
}

mysql_select_db($database_searchon, $searchon);
$query_tournamenteams = "SELECT * FROM tournamenteams";
$tournamenteams = mysql_query($query_tournamenteams, $searchon) or die(mysql_error());
$row_tournamenteams = mysql_fetch_assoc($tournamenteams);
$totalRows_tournamenteams = mysql_num_rows($tournamenteams);

mysql_select_db($database_searchon, $searchon);
$query_tournaments = "SELECT * FROM tournaments";
$tournaments = mysql_query($query_tournaments, $searchon) or die(mysql_error());
$row_tournaments = mysql_fetch_assoc($tournaments);
$totalRows_tournaments = mysql_num_rows($tournaments);

mysql_select_db($database_searchon, $searchon);
$query_teams = "SELECT * FROM teams";
$teams = mysql_query($query_teams, $searchon) or die(mysql_error());
$row_teams = mysql_fetch_assoc($teams);
$totalRows_teams = mysql_num_rows($teams);
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">


<select name="id_tournament">
<?php 
do {  
?>
<option value="<?php echo $row_tournaments['id_tournament']?>" ><?php echo $row_tournaments['id_tournament']?></option>
<?php
} while ($row_tournaments = mysql_fetch_assoc($tournaments));
?>
</select>


<?php do { ?>
<?php echo $row_teams['id_team']?>:<input type="checkbox" name="id_team" value="<?php echo $row_teams['id_team']?>" />
<?php } while ($row_teams = mysql_fetch_assoc($teams)); ?>


<input type="submit" value="Insert record" />

<input type="hidden" name="id" value="" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($tournamenteams);

mysql_free_result($tournaments);

mysql_free_result($teams);
?>

PS: Im using dreamweaver since im still starting coding :)

Any help will be appreciated,

Cumps.

  • 写回答

1条回答 默认 最新

  • 普通网友 2015-06-19 22:23
    关注

    In order to have PHP to receive every checkbox value (in an array, naturally) is to put [] at the end of the group's name. So, the part where you create the checkboxes should be changed to:

    <?php do { ?>
    <?php echo $row_teams['id_team']?>:<input type="checkbox" name="id_team[]" value="<?php echo $row_teams['id_team']?>" />
    <?php } while ($row_teams = mysql_fetch_assoc($teams)); ?>
    

    Notice that the only change was in name="id_team[]".

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100