vendredi 19 juin 2015

Insert multiple checkbox into 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.

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://ift.tt/kkyg93">
<html xmlns="http://ift.tt/lH0Osb">
<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.




Aucun commentaire:

Enregistrer un commentaire