Je tente depuis plusieurs jours de trouver une solution pour l'enregistrement de checkbox multiples sans succès.
Je récupère bien un echo avec l'ensemble choix mais lorsque je tente de l’enregistrer dans une base de donnée je ne récupère que la dernière valeur
Voici mon code
<?php
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $opt) {
echo $opt;
}
}
La je récupère bien les valeurs sous forme d'une ligne$_POST['option'] = $opt;
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO vehicule (type, version, annee, km, porte, puissance, bv, energie, mec, couleurinter, couleurext, `option`, prix, contact1, contact2) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['type'], "text"),
GetSQLValueString($_POST['version'], "text"),
GetSQLValueString($_POST['annee'], "text"),
GetSQLValueString($_POST['km'], "text"),
GetSQLValueString($_POST['porte'], "text"),
GetSQLValueString($_POST['puissance'], "text"),
GetSQLValueString($_POST['bv'], "text"),
GetSQLValueString($_POST['energie'], "text"),
GetSQLValueString($_POST['mec'], "text"),
GetSQLValueString($_POST['couleurinter'], "text"),
GetSQLValueString($_POST['couleurext'], "text"),
GetSQLValueString($_POST['option'], "text"),
GetSQLValueString($_POST['prix'], "text"),
GetSQLValueString($_POST['contact1'], "text"),
GetSQLValueString($_POST['contact2'], "text"));
Mais quand j'enregistre le tous je ne récupère que la dernière valeur cochéeVoici ma page complete
<?php require_once('../Connections/auto.php'); ?>
<?php
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $opt) {
echo $opt;
}
}
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $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']);
}
$_POST['option'] = $opt;
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO vehicule (type, version, annee, km, porte, puissance, bv, energie, mec, couleurinter, couleurext, `option`, prix, contact1, contact2) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['type'], "text"),
GetSQLValueString($_POST['version'], "text"),
GetSQLValueString($_POST['annee'], "text"),
GetSQLValueString($_POST['km'], "text"),
GetSQLValueString($_POST['porte'], "text"),
GetSQLValueString($_POST['puissance'], "text"),
GetSQLValueString($_POST['bv'], "text"),
GetSQLValueString($_POST['energie'], "text"),
GetSQLValueString($_POST['mec'], "text"),
GetSQLValueString($_POST['couleurinter'], "text"),
GetSQLValueString($_POST['couleurext'], "text"),
GetSQLValueString($_POST['option'], "text"),
GetSQLValueString($_POST['prix'], "text"),
GetSQLValueString($_POST['contact1'], "text"),
GetSQLValueString($_POST['contact2'], "text"));
mysql_select_db($database_auto, $auto);
$Result1 = mysql_query($insertSQL, $auto) or die(mysql_error());
$insertGoTo = "espace.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
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;
}
}
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;
}
}
mysql_select_db($database_auto, $auto);
$query_contact = "SELECT * FROM contact ORDER BY prenom ASC";
$contact = mysql_query($query_contact, $auto) or die(mysql_error());
$row_contact = mysql_fetch_assoc($contact);
$totalRows_contact = mysql_num_rows($contact);
mysql_select_db($database_auto, $auto);
$query_Recordset1 = "SELECT * FROM options ORDER BY nom ASC";
$Recordset1 = mysql_query($query_Recordset1, $auto) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<!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>Document sans titre</title>
<style type="text/css">
<!--
body {
background-image: url(img/fond.jpg);
background-repeat: no-repeat;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-position: center top;
background-color: #263135;
}
.Style1 {
color: #E0E7ED;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 16px;
}
body,td,th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #333333;
}
.Style3 {font-size: 18px}
.liste {
font-size: 11px;
float: left;
overflow: visible;
position: relative;
z-index: auto;
font-family: Verdana, Geneva, sans-serif;
list-style-type: disc;
}
-->
</style>
<script type="text/javascript">
<!--
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<link href="../css/structure.css" rel="stylesheet" type="text/css" />
</head>
<body onload="MM_preloadImages('img/bouton/vehicule2.png','img/bouton/option2.png','img/bouton/CONTACT2.png','img/bouton/USER2.png','img/bouton/accueil2.png')">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="139" colspan="2" align="center" valign="top" background="img/header.jpg"><table width="1044" height="140" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="35" colspan="3" align="left" valign="middle"><span class="Style1">ESPACE ADMINISTRATION - </span></td>
<td align="center" valign="top"> </td>
<td colspan="3" align="right" valign="middle"><img src="img/bouton/DECONNECTION.png" width="14" height="16" /></td>
</tr>
<tr>
<td width="160" height="105" align="center" valign="middle"><a href="espace.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image6','','img/bouton/accueil2.png',1)"><img src="img/bouton/accueil1.png" name="Image6" width="127" height="87" border="0" id="Image6" /></a></td>
<td width="160" align="center" valign="middle"><a href="ajout_vehicule.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image1','','img/bouton/vehicule2.png',1)"><img src="img/bouton/vehicule1.png" name="Image1" width="127" height="87" border="0" id="Image1" /></a></td>
<td width="160" align="center" valign="middle"><a href="ajout_option.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image2','','img/bouton/option2.png',1)"><img src="img/bouton/option1.png" name="Image2" width="127" height="87" border="0" id="Image2" /></a></td>
<td width="160" align="center" valign="middle"><a href="ajout_contact.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image4','','img/bouton/CONTACT2.png',1)"><img src="img/bouton/CONTACT1.png" name="Image4" width="127" height="87" border="0" id="Image4" /></a></td>
<td width="160" align="center" valign="middle"><a href="ajout_user.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image5','','img/bouton/USER2.png',1)"><img src="img/bouton/USER1.png" name="Image5" width="127" height="87" border="0" id="Image5" /></a></td>
<td width="160" align="center" valign="middle"> </td>
<td width="84" align="center" valign="middle"> </td>
</tr>
</table></td>
</tr>
<tr>
<td height="35"> </td>
<td> </td>
</tr>
<tr>
<td height="393" colspan="2" align="center" valign="middle"><table width="1044" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="1049" height="54" background="img/header contenu.png"><table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="57" height="22" align="left" valign="bottom"> </td>
<td width="916" align="left" valign="bottom"><span class="Style3">Ajouter un nouveau véhicule à la vente.</span></td>
<td width="27"> </td>
</tr>
</table></td>
</tr>
<tr>
<td height="317" align="center" valign="top" background="img/contenu.png"><table width="965" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
</tr>
<tr>
<td align="center"><form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="373" height="25" align="left" valign="bottom">Type:</td>
<td width="375" align="left" valign="bottom">Version:</td>
</tr>
<tr>
<td align="left"><input name="type" type="text" class="box label" id="type" size="45" /></td>
<td align="left"><input name="version" type="text" class="box label" id="version" size="45" /></td>
</tr>
<tr>
<td height="25" align="left" valign="bottom">Année:</td>
<td align="left" valign="bottom">Kilométrage:</td>
</tr>
<tr>
<td align="left"><input name="annee" type="text" class="box label" id="annee" /></td>
<td align="left"><input name="km" type="text" class="box label" id="km" /></td>
</tr>
<tr>
<td height="25" align="left" valign="bottom">Nombre de porte:</td>
<td align="left" valign="bottom">Puissance fiscal:</td>
</tr>
<tr>
<td align="left"><select name="porte" class="box label" id="porte">
<option value="2 portes">2 portes</option>
<option value="3 portes">3 portes</option>
<option value="4 portes">4 portes</option>
<option value="5 portes">5 portes</option>
</select> </td>
<td align="left"><input name="puissance" type="text" class="box label" id="puissance" /></td>
</tr>
<tr>
<td height="25" align="left" valign="bottom">Boite de vitesse:</td>
<td align="left" valign="bottom">Energie:</td>
</tr>
<tr>
<td align="left"><select name="bv" class="box label" id="bv">
<option value="Mécanique" selected="selected">Mécanique</option>
<option value="Automatique">Automatique</option>
</select> </td>
<td align="left"><select name="energie" class="box label" id="energie">
<option value="Essence" selected="selected">Essence</option>
<option value="Diesel">Diesel</option>
<option value="GPL">GPL</option>
<option value="Electrique">Electrique</option>
</select> </td>
</tr>
<tr>
<td height="25" align="left" valign="bottom">Mise en circulation:</td>
<td align="left" valign="bottom">Couleur interieur:</td>
</tr>
<tr>
<td align="left"><input name="mec" type="text" class="box label" id="mec" /></td>
<td align="left"><input name="couleurinter" type="text" class="box label" id="couleurinter" /></td>
</tr>
<tr>
<td height="25" align="left" valign="bottom">Couleur exterieur:</td>
<td align="left" valign="bottom">Prix:</td>
</tr>
<tr>
<td align="left"><input name="couleurext" type="text" class="box label" id="couleurext" /></td>
<td align="left"><input name="prix" type="text" class="box label" id="prix" /></td>
</tr>
<tr>
<td height="25" align="left" valign="bottom">Contact pour la vente:</td>
<td> </td>
</tr>
<tr>
<td colspan="2" align="left"><table width="432" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="left">Contact 1</td>
<td align="left">Contact 2</td>
</tr>
<tr>
<td align="left"><select name="contact1" class="box label" id="contact1">
<?php
do {
?>
<option value="<?php echo $row_contact['prenom']?>"><?php echo $row_contact['prenom']?></option>
<?php
} while ($row_contact = mysql_fetch_assoc($contact));
$rows = mysql_num_rows($contact);
if($rows > 0) {
mysql_data_seek($contact, 0);
$row_contact = mysql_fetch_assoc($contact);
}
?>
</select> </td>
<td align="left"><select name="contact2" class="box label" id="contact2">
<?php
do {
?>
<option value="<?php echo $row_contact['prenom']?>"><?php echo $row_contact['prenom']?></option>
<?php
} while ($row_contact = mysql_fetch_assoc($contact));
$rows = mysql_num_rows($contact);
if($rows > 0) {
mysql_data_seek($contact, 0);
$row_contact = mysql_fetch_assoc($contact);
}
?>
</select></td>
</tr>
</table></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="2" align="center"><table width="685" border="1" align="center">
<tr>
<td align="center" valign="top">
<?php do { ?>
<p>
<span class="liste">
<input name="checkbox[]" type="checkbox" value="<?php echo $row_Recordset1['nom']; ?>">
<?php echo $row_Recordset1['nom']; ?>
-
</span>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?></td>
</tr>
</table></td>
</tr>
<tr>
<td align="center"> </td>
<td> </td>
</tr>
<tr>
<td align="center"> </td>
<td> </td>
</tr>
<tr>
<td align="center"> </td>
<td> </td>
</tr>
<tr>
<td colspan="2" align="center" valign="middle"> <input name="option" type="hidden" id="option" value="<?php echo $opt; ?>" /> <?php ?><input name="valider" type="submit" class="btnLogin" id="valider" value="Enregister le nouveau véhicule" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
</table></td>
</tr>
<tr>
<td height="28" background="img/footer_contenu.png"> </td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>
<?php
mysql_free_result($contact);
mysql_free_result($Recordset1);
?>