【问题标题】:Insert multiple checkbox into mysql将多个复选框插入mysql
【发布时间】:2015-06-20 06:23:57
【问题描述】:

所以我想在 mysql 中插入多个复选框,但不知道该怎么做。

从数据库中选择并插入:

$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());
}

现在我创建了一个表单,在表单内有复选框。我选择数据库记录并将其显示在多个复选框上,然后我想要做的是选择我想要的复选框并在我提交表单时将其全部插入,但它只提交我标记的第一个复选框。

表格:

<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>

复选框:

<?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)); ?>

完整代码以获得更好的视图:

<?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:我正在使用dreamweaver,因为我还在开始编码:)

任何帮助将不胜感激,

肿块。

【问题讨论】:

  • 为什么不将问题简化为最简单的形式
  • 好吧,我减少了一些代码,但维护完整的代码让人们看得更清楚。

标签: php mysql checkbox


【解决方案1】:

为了让 PHP 接收每个复选框值(自然是在一个数组中),将[] 放在组名的末尾。因此,您创建复选框的部分应更改为:

<?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)); ?>

请注意,唯一的变化是 name="id_team[]"

【讨论】:

  • 我这样做了,但知道给了我一个错误:警告:mysql_real_escape_string() 期望参数 1 是字符串,数组在 C:\xampp\htdocs\searchon\test.php 第 10 行列' id_team' 不能为空
  • 也许我也需要更改插入但不知道该怎么做?
猜你喜欢
  • 2020-11-07
  • 2012-08-20
  • 1970-01-01
  • 2015-02-19
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多