update de cases à cocher multiples qui ne fonctionne pas
Bonjour à tous,
Pour faire suite à mon post http://www.developpez.net/forums/d12...e/#post6942613 Je n'arrive pas à faire un update des caser à cocher 'ok'. Pour rappel, le code que 'rawsrc' m'avait donné fonctionne parfaitement:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| <?php
$mission_id = $PDO->quote($modif_renfort_id, PDO::PARAM_INT);
$sql = <<<SQL
SELECT
d.vd_id, d.vd_renfort_id, d.id_renfort_volontaires, d.nom, d.date_dispo, d.ok,
v.*,
m.date_debut,
m.date_fin
FROM
volontaires_dispo d
INNER JOIN renfort_volontaires v ON v.k_id_renfort_volontaires = d.id_renfort_volontaires
INNER JOIN renfort m ON m.id_renfort = v.k_id_renfort
WHERE
m.id_renfort = {$mission_id}
AND d.date_dispo BETWEEN m.date_debut AND m.date_fin
ORDER BY
d.date_dispo
SQL;
$data = $PDO->query($sql, PDO::FETCH_ASSOC)->fetchAll(); // j'avais oublié le fetchAll()
if (empty($data)) {
// aucun volontaire disponible
}
else {
// les dates sont déterminées par les bornes des dates de la mission
// qu'on récupère du 1er enregistrement
$debut = DateTime::createFromFormat('Y-m-d', $data[0]['date_debut']);
$fin = DateTime::createFromFormat('Y-m-d', $data[0]['date_fin']);
$interval = new DateInterval('P1D');
$dates = array($debut->format('Y-m-d'));
while($debut < $fin) {
$dates[] = $debut->add($interval)->format('Y-m-d');
}
// par défaut on considère qu'il n'y a aucune disponibilité pour chaque date
$default_dispo = array_fill_keys(array_values($dates), false);
$rows = array();
foreach($data as $row) {
$id = $row['k_id_volontaire'];
// si on n'a pas déjà rencontré le volontaire on le crée
if (empty($rows[$id])) {
$rows[$id] = array(
'dispo' => $default_dispo,
'nom' => $row['nom'],
'ok' => array()
// d'autres valeurs à la place des * dans ton SELECT
);
}
// on récupère les diponibilités
$rows[$id]['dispo'][$row['date_dispo']] = true;
// on récupère la colonne ok
$rows[$id]['ok'][$row['date_dispo']] = ( empty($row['ok']));
}
// rendu
?><div class="centrage_submit">
<table bgcolor="#FFFFFF">
<thead><!-- entête du tableau -->
<tr>
<th class="td_140px"><b> Réservistes </b></th>
<?php foreach($dates as $d): ?>
<?php // ... on la convertit donc en format: jj-mm-aaaa
$d = implode('-', array_reverse(explode('-', $d)));
?>
<th class="td_date"><?php echo $d ?></th><!-- <th> est le <td> de l'entête du tableau -->
<?php endforeach ?>
</tr>
</thead>
<tbody>
<?php foreach($rows as $id => $row): ?>
<tr>
<?php $nom = $row['nom']; ?>
<td bgcolor='#CCCCCC'><input type="text" name='nom[]' value="<?php echo $nom ?>" class="tableau_gris"</td>
<?php foreach($row['dispo'] as $date => $v): ?>
<td bgcolor='#CCCCCC'>
<?php if ($v): ?>
<input type="checkbox" name='selection_date[]' <?php echo (($row['ok'][$date]) ? '' : 'checked="checked" ') ?>/>
<?php else: ?>
<?php endif ?>
</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
<?php
}
}
echo '<div class="centrage_pre">';
print_r("<pre>");print_r($data);print_r("</pre>");
print("\n");
echo '</div>';
}
?>
<input type="submit" name="validation_a1" value=" --> Modifier le renfort <-- " /> |
J'ai simplement rajouté la variable $nom pour avoir une valeur dans mon input 'nom'. Avec ce code j'obtiens ce tableau dans mon formulaire:
- id_volontaires_dispo étant l'identifiant de la ligne.
- ok : la case à cocher correpondant à la ligne id_volontaires_dispo donc si ma table est composé comme ceci pour par exemple le renfort 52:
Code:
1 2 3 4 5 6 7 8 9
| Table volontaires_dispo
id_volontaires_dispo | k_id_renfort_volontaires | nom | date_dispo | ok
----------------------------------------------------------------------
1 | 1 | nom1 | 2012-09-01 | 1
2 | 1 | nom1 | 2012-09-02 | 0
3 | 1 | nom1 | 2012-09-03 | 0
4 | 2 | nom2 | 2012-09-01 | 1
5 | 2 | nom2 | 2012-09-02 | 0
6 | 2 | nom2 | 2012-09-03 | 0 |
- id_volontaires_dispo = 1, nom serait = à nom 1, ok = 1 (validé)
- id_volontaires_dispo = 2, nom serait = à nom1, ok = 0 (non validé)
- id_volontaires_dispo = 3, nom serait = à nom1, ok = 0 (non validé)
- id_volontaires_dispo = 4, nom serait = à nom2, ok = 1 (validé)
- id_volontaires_dispo = 5, nom serait = à nom2, ok = 0 (non validé)
- id_volontaires_dispo = 6, nom serait = à nom2, ok = 0 (non validé)
Je visualise bien:
Code:
1 2 3 4
| nom | 01-09-2012 | 02-09-2012 | 03-09-2012 |
--------------------------------------------------------
nom1 | 1 | 0 | 0 |
nom2 | 1 | 0 | 0 | |
Si cela est utile voilà ce que me donne un print_r($data):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| Array
(
[0] => Array
(
[vd_id] => 256
[vd_renfort_id] => 52
[id_renfort_volontaires] => 108
[nom] => 4
[date_dispo] => 2013-05-01
[ok] => 1
[k_id_renfort_volontaires] => 108
[rv_host] =>
[k_id_renfort] => 52
[k_id_volontaire] => 4
[date_debut] => 2013-05-01
[date_fin] => 2013-05-03
)
[1] => Array
(
[vd_id] => 259
[vd_renfort_id] => 52
[id_renfort_volontaires] => 109
[nom] => 6
[date_dispo] => 2013-05-01
[ok] => 1
[k_id_renfort_volontaires] => 109
[rv_host] =>
[k_id_renfort] => 52
[k_id_volontaire] => 6
[date_debut] => 2013-05-01
[date_fin] => 2013-05-03
)
[2] => Array
(
[vd_id] => 257
[vd_renfort_id] => 52
[id_renfort_volontaires] => 108
[nom] => 4
[date_dispo] => 2013-05-02
[ok] => 0
[k_id_renfort_volontaires] => 108
[rv_host] =>
[k_id_renfort] => 52
[k_id_volontaire] => 4
[date_debut] => 2013-05-01
[date_fin] => 2013-05-03
)
[3] => Array
(
[vd_id] => 260
[vd_renfort_id] => 52
[id_renfort_volontaires] => 109
[nom] => 6
[date_dispo] => 2013-05-02
[ok] => 0
[k_id_renfort_volontaires] => 109
[rv_host] =>
[k_id_renfort] => 52
[k_id_volontaire] => 6
[date_debut] => 2013-05-01
[date_fin] => 2013-05-03
)
[4] => Array
(
[vd_id] => 258
[vd_renfort_id] => 52
[id_renfort_volontaires] => 108
[nom] => 4
[date_dispo] => 2013-05-03
[ok] => 0
[k_id_renfort_volontaires] => 108
[rv_host] =>
[k_id_renfort] => 52
[k_id_volontaire] => 4
[date_debut] => 2013-05-01
[date_fin] => 2013-05-03
)
[5] => Array
(
[vd_id] => 261
[vd_renfort_id] => 52
[id_renfort_volontaires] => 109
[nom] => 6
[date_dispo] => 2013-05-03
[ok] => 0
[k_id_renfort_volontaires] => 109
[rv_host] =>
[k_id_renfort] => 52
[k_id_volontaire] => 6
[date_debut] => 2013-05-01
[date_fin] => 2013-05-03
)
) |
Maintenant je n'arrive pas à faire un update de mes cases 'ok' si par exemple je change dans mon formulaire les données:
Code:
1 2 3 4
| nom | 01-09-2012 | 02-09-2012 | 03-09-2012 |
--------------------------------------------------------
nom1 | 1 | 1 | 0 |
nom2 | 0 | 0 | 1 | |
Le code de ma page renfort_modification_ok.php qui traite l'update
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?php
$modif_renfort_id = '';
$selection_date = '';
$nom = '';
// La page d'insertion
if ( $_POST['modif_renfort_id'] > 0 && $_POST['selection_date'] >= 0 && $_POST['nom'] > 0);
{
require"./includes/identifiants.php";
require"includes/header.php";
require"includes/menu_brigade_55989526915988.php";
$nom = $_POST['nom'];
$modif_renfort_id = $_POST['modif_renfort_id'];
$selection_date = $_POST['selection_date'];
print_r( $nom);
print_r( $modif_renfort_id);
print_r( $selection_date);
// Avec une requête préparée
$req = $PDO->prepare('UPDATE volontaires_dispo SET ok = :selection_date WHERE vd_renfort_id = :modif_renfort_id AND nom = :nom');
$req->execute(array('vd_renfort_id' => $modif_renfort_id, 'nom' => $nom, 'ok' => $selection_date));
}
?> |
Alors avant que 'rawsrc' ne se moque :lol: parce que j'ai cherché pendant un mois la solution (seulement 2 jours pour ce cas) je vous demande humblement de l'aide :oops: j'obtiens ces messages d'erreur et de notice:
Citation:
Notice: Array to string conversion in C ligne 20
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C ligne 20
Ce qui correspond à ça
Code:
$req->execute(array('vd_renfort_id' => $modif_renfort_id, 'nom' => $nom, 'ok' => $selection_date));
Encore merci d'avance!
Mimosa21:(
PS: merci rawsrc pour la solution de mon dernier post.