php - Insert multiple checkbox into mysql -
so insert multiple checkbox mysql cant figured ou how.
selecting database , 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 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 created form , inside form theres checkbox. select database records , show on multiple checkbox , want selecting checkboxs want , insert when submit form, submit first checkbox mark.
form:
<form action="<?php echo $editformaction; ?>" method="post" name="form1" id="form1"> <select name="id_tournament"> <?php { ?> <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 { ?> <?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 { ?> <?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 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 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 * 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 * 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 * 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 { ?> <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 { ?> <?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> </p> <p> </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 appreciated,
cumps.
in order have php receive every checkbox value (in array, naturally) put []
@ end of group's name. so, part create checkboxes should changed to:
<?php { ?> <?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 change in name="id_team[]"
.
Comments
Post a Comment