Bonjour,

Je suis novice en php et mysql et je voudrais répéter la fonction UPDATE x fois en fonction d'un form. Dans mon cas actuel, j'aimerai changer des données dans 5 id différentes en seule fois selon un form.

Voici mon formulaire de départ où je récupére mes données. Comme il y a 5 id qui correspond qui mes critéres, j'ai 5 lignes qui apparaissent.

<?php
include("../connection.php");
$standings_name_data = $connectdb->query('SELECT * FROM standings_name WHERE sport=\''.$_COOKIE'sport'].'\' ');
while($standings_name = $standings_name_data->fetch()){
    ?>
<form method="post" action="action/standings_edit.php">
    <table border="0" cellpadding="0" cellspacing="0">
        <?php if($_COOKIE'sport'] == "nhl"){
            ?>
        <tr style="text-align:center;">
            <td style="text-align:left;">Team</td>
            <td>W</td>
            <td>L</td>
            <td>OTL</td>
            <td>L10</td>
        </tr>
        <?php
        }
        elseif($_COOKIE'sport'] == "nfl"){
            ?>
        <tr style="text-align:center;">
            <td style="text-align:left;">Team</td>
            <td>W</td>
            <td>L</td>
            <td>T</td>
            <td>L10</td>
        </tr>
        <?php
        }
        else{
            ?>
        <tr style="text-align:center;">
            <td style="text-align:left;">Team</td>
            <td>W</td>
            <td>L</td>
            <td>STRK</td>
            <td>L10</td>
        </tr>
        <?php
        }
        $standings_content_data = $connectdb->query('SELECT * FROM standings_content WHERE sport=\''.$_COOKIE'sport'].'\' ');
        while($standings_content = $standings_content_data->fetch()){
            ?>
        <tr id="<?php echo $standings_content'id']; ?>">
            <td><select name="team">
            <?php
            $teams_name = $connectdb->query('SELECT * FROM teams WHERE sport=\''.$_COOKIE'sport'].'\' ');
            while($team = $teams_name->fetch()){
                if($team'id'] == $standings_content'team']){
                    $selected = "selected";
                }
                else{
                    $selected = "";
                }
                ?>
                    <option value="<?php echo $team'id']; ?>" <?php echo $selected; ?>><?php echo $team'team']; ?></option>
            <?php
            }
            $teams_name->closeCursor();
            ?>
                </select></td>
                <td><input type="text" name="cat1" value="<?php echo $standings_content'cat1']; ?>" /></td>
                <td><input type="text" name="cat2" value="<?php echo $standings_content'cat2']; ?>" /></td>
                <td><input type="text" name="cat3" value="<?php echo $standings_content'cat3']; ?>" /></td>
                <td><input type="text" name="cat4" value="<?php echo $standings_content'cat4']; ?>" /></td>
                <td><input type="hidden" name="id" value="<?php echo $standings_content'id']; ?>" /></td>
        </tr>
        <?php
        }
        $standings_content_data->closeCursor();
        ?>
    </table>
    <input type="submit" value="Submit" />
</form>
<?php
}
?>

Maintenant voici ma requête SQL. Le problème est que pour les id 1, 2, 3, 4 il ne se passe rien mais pour la 5 sa fonctionne.

<?php
include('../../connection.php');
$standings_edit = $connectdb->prepare('UPDATE standings_content SET team = :team, cat1 = :cat1, cat2 = :cat2, cat3 = :cat3, cat4 = :cat4 WHERE sport=\''.$_COOKIE'sport'].'\' AND id=\''.$_POST'id'].'\' ');
$standings_edit->execute(array(
    'team' => $_POST'team'],
    'cat1' => $_POST'cat1'],
    'cat2' => $_POST'cat2'],
    'cat3' => $_POST'cat3'],
    'cat4' => $_POST'cat4']
));
header('location:../index.php?view=standings');
?>

1 réponse


GentlemanOwl
Réponse acceptée

Salut,

En fait, le problème c'est que php ne va prendre en compte que la dernière ligne. A cause du fait que les names sont identiques donc ils s'annulent entre eux.

La solution est assez simple. Il suffit donc de définir un nom différent pour chaque name enfin pas exactement enfin regarde le code ce sera plus clair:

<form method="post" action="index.php">
<?php
for($i=1; $i<=5; $i++):
?>
    <table border="0" cellpadding="0" cellspacing="0">
        <tr style="text-align:center;">
            <td style="text-align:left;">Team</td>
            <td>W</td>
            <td>L</td>
            <td>STRK</td>
            <td>L10</td>
        </tr>
        <tr id="<?php echo $i ?>">
            <td><select name="team<?php echo $i ?>][id]">
        <?php
            echo "<option value='".$i."'>$i</option>";
        ?>
                </select></td>
                <td><input type="text" name="team<?php echo $i ?>][cat1]" value="Value" /></td>
                <td><input type="text" name="team<?php echo $i ?>][cat2]" value="Value" /></td>
                <td><input type="text" name="team<?php echo $i ?>][cat3]" value="Value" /></td>
                <td><input type="text" name="team<?php echo $i ?>][cat4]" value="Value" /></td>
        </tr>
        <?php
        ?>
    </table>
<?php
endfor;
?>
    <input type="submit" value="Submit" />
</form>

Bon là, j'ai enlever les accès à la base de donnée. Mais c'est le principe et au final tu obtient un tableau de $_POST qui est sous cette forme-ci :

Array
(
    [team] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [cat1] => Value
                    [cat2] => Value
                    [cat3] => Value
                    [cat4] => Value
                )
            [2] => Array
                (
                    [id] => 2
                    [cat1] => Value
                    [cat2] => Value
                    [cat3] => Value
                    [cat4] => Value
                )
            [3] => Array
                (
                    [id] => 3
                    [cat1] => Value
                    [cat2] => Value
                    [cat3] => Value
                    [cat4] => Value
                )
            [4] => Array
                (
                    [id] => 4
                    [cat1] => Value
                    [cat2] => Value
                    [cat3] => Value
                    [cat4] => Value
                )
            [5] => Array
                (
                    [id] => 5
                    [cat1] => Value
                    [cat2] => Value
                    [cat3] => Value
                    [cat4] => Value
                )
        )
)

Ici le premier niveau du tableau "team" n'est pas utile ... ça donnerais plutôt ça :

$i][nom du champ]

Mais je continue avec mon team lol ^^.

Ensuite il te suffit de faire un foreach du tableau comme ceci :

if(isset($_POST)){
    /* Tu parcour le tableau $_POST
        $k est l'id;
        $v les valeurs;
    */
    foreach($_POST'team'] as $k=>$v){
        $id=$v'id'];

        /*
        Ici tu fait ton update en remplacant juste tes anciennes variables par les nouvelles.
        */
    }
    }

Après tu pourra être gêner par tes id donc dans ce cas la il te faudra juste utilisé un peu jQuery pour pouvoir lié le select est les ids.

Il y avait un tuto de Grafikart dans le même genre : Tutoriel

Celui-ci je crois bien.

J'espère que ça t'aidera.

A+.