IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PHP & Base de données Discussion :

Mettre a jour une table avec des checkbox


Sujet :

PHP & Base de données

  1. #1
    Membre régulier Avatar de dlecteur
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    147
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2006
    Messages : 147
    Points : 95
    Points
    95
    Par défaut Mettre a jour une table avec des checkbox
    Bonjour,

    Je veux mettre jour une table mysql avec des checkbox, mais je n'arrive pas a le faire!

    Merci pour vos conseils.

    voila mon code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <strong>Update <strong class="highlight">multiple</strong> <strong class="highlight">rows</strong> <strong class="highlight">in</strong> <strong class="highlight">mysql</strong></strong><br> 
     
    <?php 
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password="root"; // Mysql password 
    $db_name="db_test"; // Database name 
    $tbl_name="test_table"; // Table name
     
    // Connect to server and select databse. 
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); 
     
    $sql="SELECT * FROM $tbl_name"; 
    $result=mysql_query($sql); 
     
    // Count table <strong class="highlight">rows</strong> 
    $count=mysql_num_rows($result); 
    ?> 
    <table width="500" border="0" cellspacing="1" cellpadding="0"> 
    <form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>"> 
    <tr> 
    <td> 
    <table width="500" border="0" cellspacing="1" cellpadding="0"> 
     
     
    <tr> 
    <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Phone Number</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Operation</strong></td>
    </tr> 
    <?php 
    while($rows=mysql_fetch_array($result))
    { 
    ?> 
    <tr> 
    <td align="center"><input type="hidden" name="id[]" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td> 
    <td align="center"><input name="name<? echo $rows['id']; ?>" type="text" id="name" value="<? echo $rows['name']; ?>"></td> 
    <td align="center"><input name="email<? echo $rows['id']; ?>" type="text" id="email" value="<? echo $rows['email']; ?>"></td> 
    <td align="center"><input name="description<? echo $rows['id']; ?>" type="text" id="description" value="<? echo $rows['description']; ?>"></td> 
    <td align="center"><input name="phone_number<? echo $rows['id']; ?>" type="text" id="phone_number" value="<? echo $rows['phone_number']; ?>"></td>
    <td align="center"><input name="operation<? echo $rows['id']; ?>" type="text" id="operation" value="<? echo $rows['operation']; ?>"></td>
    <td align="center"><input name="ONOFF<? echo $rows['id']; ?>" type="checkbox" id="ONOFF" value="1" 
    <?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?> 
    </td> 
    </tr> 
    <?php 
    } 
    ?> 
    <tr> 
    <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> 
    </tr> 
    </table> 
    </td> 
    </tr> 
    </form> 
    </table> 
    <?php 
    // Check if button name "Submit" is active, do this 
    if($Submit)
    { 
    	foreach($_POST['id'] as $id)
    	{ 
    		$sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."', email='".$_POST["email".$id]."', description='".$_POST["description".$id]."', phone_number='".$_POST["phone_number".$id]."', operation='".$_POST["operation".$id]."', ONOFF='".$_POST["ONOFF".$id]."' WHERE id='".$id."'"; 
    		$result1=mysql_query($sql1);
    	} 
    } 
     
    if($result1){ 
    header("location:test_update2.php");
    } 
    mysql_close(); 
    ?>
    Cdt.

  2. #2
    Membre expérimenté
    Avatar de Mahefasoa
    Homme Profil pro
    Manager IT
    Inscrit en
    Octobre 2003
    Messages
    835
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Madagascar

    Informations professionnelles :
    Activité : Manager IT

    Informations forums :
    Inscription : Octobre 2003
    Messages : 835
    Points : 1 664
    Points
    1 664
    Par défaut ONOFF
    Bonjour,

    Au niveau de la boucle foreach, avant l'instruction
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."'...
    ajoute un le bloc suivant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    $onoff = 0;
    if (isset($_POST["ONOFF".$id]))
    {
        $onoff = 1
    }
    Je pense que cela devrait aller.

    Sinon, au niveau de la ligne suivante
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <td align="center"><input name="ONOFF<? echo $rows['id']; ?>" type="checkbox" id="ONOFF" value="1" <?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?> </td>
    il manque la fermeture de l'input
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <td align="center"><input name="ONOFF<? echo $rows['id']; ?>" type="checkbox" id="ONOFF" value="1" <?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?> /> </td>
    Bien cordialement,

  3. #3
    Membre expérimenté
    Avatar de Mahefasoa
    Homme Profil pro
    Manager IT
    Inscrit en
    Octobre 2003
    Messages
    835
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Madagascar

    Informations professionnelles :
    Activité : Manager IT

    Informations forums :
    Inscription : Octobre 2003
    Messages : 835
    Points : 1 664
    Points
    1 664
    Par défaut oubli!
    Et au niveau du sql en bas,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."', email='".$_POST["email".$id]."', description='".$_POST["description".$id]."', phone_number='".$_POST["phone_number".$id]."', operation='".$_POST["operation".$id]."', ONOFF='".$onoff."' WHERE id='".$id."'";

  4. #4
    Membre régulier Avatar de dlecteur
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    147
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2006
    Messages : 147
    Points : 95
    Points
    95
    Par défaut
    Merci pour la réponse j'ai fais tous les changements suggérées, mais je n'arrive toujours pas a faire l'update.

    Cdt.

  5. #5
    Membre régulier Avatar de dlecteur
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    147
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2006
    Messages : 147
    Points : 95
    Points
    95
    Par défaut
    voila mon nouveau code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <strong>Update <strong class="highlight">multiple</strong> <strong class="highlight">rows</strong> <strong class="highlight">in</strong> <strong class="highlight">mysql</strong></strong><br> 
     
    <?php 
    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password="root"; // Mysql password 
    $db_name="db_test"; // Database name 
    $tbl_name="test_table"; // Table name
     
    // Connect to server and select databse. 
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); 
     
    $sql="SELECT * FROM $tbl_name"; 
    $result=mysql_query($sql); 
     
    // Count table <strong class="highlight">rows</strong> 
    $count=mysql_num_rows($result); 
    ?> 
    <table width="500" border="0" cellspacing="1" cellpadding="0"> 
    <form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>"> 
    <tr> 
    <td> 
    <table width="500" border="0" cellspacing="1" cellpadding="0"> 
     
     
    <tr> 
    <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Phone Number</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Operation</strong></td>
    </tr> 
    <?php 
    while($rows=mysql_fetch_array($result))
    { 
    ?> 
    <tr> 
    <td align="center"><input type="hidden" name="id[]" value="<? echo $rows['id']; ?>" /><? echo $rows['id']; ?></td> 
    <td align="center"><input name="name<? echo $rows['id']; ?>" type="text" id="name" value="<? echo $rows['name']; ?>"></td> 
    <td align="center"><input name="email<? echo $rows['id']; ?>" type="text" id="email" value="<? echo $rows['email']; ?>"></td> 
    <td align="center"><input name="description<? echo $rows['id']; ?>" type="text" id="description" value="<? echo $rows['description']; ?>"></td> 
    <td align="center"><input name="phone_number<? echo $rows['id']; ?>" type="text" id="phone_number" value="<? echo $rows['phone_number']; ?>"></td>
    <td align="center"><input name="operation<? echo $rows['id']; ?>" type="text" id="operation" value="<? echo $rows['operation']; ?>"></td>
    <td align="center"><input name="ONOFF<? echo $rows['id']; ?>" type="checkbox" id="ONOFF" value="1" 
    <?php if ($rows['ONOFF'] ==1) { echo "checked";} else {} ?> 
    </td> 
    </tr> 
    <?php 
    } 
    ?> 
    <tr> 
    <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> 
    </tr> 
    </table> 
    </td> 
    </tr> 
    </form> 
    </table> 
    <?php 
    // Check if button name "Submit" is active, do this 
    if($Submit)
    { 
    	foreach($_POST['id'] as $id)
    	{ 
    		$onoff = 0;
    				if (isset($_POST["ONOFF".$id]))
    				{
    		   			$onoff = 1;
    				}
     
    		$sql1="UPDATE ".$tbl_name." SET name='".$_POST["name".$id]."', email='".$_POST["email".$id]."', description='".$_POST["description".$id]."', phone_number='".$_POST["phone_number".$id]."', operation='".$_POST["operation".$id]."', ONOFF='".$onoff."' WHERE id='".$id."'";  
    		$result1=mysql_query($sql1);
    	} 
    } 
     
    if($result1){ 
    header("location:test_update2.php");
    } 
    mysql_close(); 
    ?>
    Cdt.

  6. #6
    Membre expérimenté
    Avatar de Mahefasoa
    Homme Profil pro
    Manager IT
    Inscrit en
    Octobre 2003
    Messages
    835
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Madagascar

    Informations professionnelles :
    Activité : Manager IT

    Informations forums :
    Inscription : Octobre 2003
    Messages : 835
    Points : 1 664
    Points
    1 664
    Par défaut
    A ce niveau
    plutôt
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if (isset($_POST['Submit']))
    Bien cordialement,

  7. #7
    Membre régulier Avatar de dlecteur
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    147
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2006
    Messages : 147
    Points : 95
    Points
    95
    Par défaut
    Sorry, mais ca marche toujours pas!!!

    Cdt.

  8. #8
    Membre expérimenté
    Avatar de Mahefasoa
    Homme Profil pro
    Manager IT
    Inscrit en
    Octobre 2003
    Messages
    835
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Madagascar

    Informations professionnelles :
    Activité : Manager IT

    Informations forums :
    Inscription : Octobre 2003
    Messages : 835
    Points : 1 664
    Points
    1 664
    Par défaut
    Bonjour,

    Voici les essais que j'ai effectués:

    SQL:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    DROP TABLE IF EXISTS `essai`;
    CREATE TABLE IF NOT EXISTS `essai` (
      `id_essai` int(11) NOT NULL,
      `essai` text NOT NULL,
      `onoff` int(11) NOT NULL DEFAULT '0'
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
     
    --
    -- Contenu de la table `essai`
    --
     
    INSERT INTO `essai` (`id_essai`, `essai`, `onoff`) VALUES
    (1, 'Essai 1', 0),
    (2, 'Essai 2', 1);
    page PHP:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <strong>
        Update
        <strong class="highlight">multiple</strong>
        <strong class="highlight">rows</strong>
        <strong class="highlight">in</strong>
        <strong class="highlight">mysql</strong>
    </strong>
    <br> 
     
    <?php 
        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password="mysql"; // Mysql password 
        $db_name="edg"; // Database name 
        $tbl_name="essai"; // Table name
     
        // Connect to server and select databse. 
        $link = mysql_connect($host, $username, $password)or die("cannot connect"); 
        mysql_select_db($db_name)or die("cannot select DB"); 
     
        $sql = "
            SELECT * 
            FROM ".$tbl_name."
        "; 
        $result = mysql_query($sql,$link) or die(mysql_error()); 
     
        // Count table <strong class="highlight">rows</strong> 
        $count = mysql_num_rows($result); 
    ?> 
    <table width="500" border="0" cellspacing="1" cellpadding="0"> 
        <form name="form1" method="post" action=""> 
            <tr> 
                <td> 
                    <table width="500" border="0" cellspacing="1" cellpadding="0"> 
                        <tr> 
                            <td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
                            <td align="center" bgcolor="#FFFFFF"><strong>Essai</strong></td>
                            <td align="center" bgcolor="#FFFFFF"><strong>On/Off</strong></td>
                        </tr> 
                        <?php 
                            while($rows = mysql_fetch_assoc($result))
                            { 
                                $id = $rows['id_essai'];
                                $essai = $rows['essai'];
                                $checked = "";
                                if ($rows['onoff'] == 1)
                                {
                                    $checked = ' checked="checked"';
                                }
                        ?> 
                        <tr> 
                            <td align="center"><input type="hidden" name="id[]" value="<?php echo $id; ?>" /><?php echo $id; ?></td> 
                            <td align="center"><input name="name<?php echo $id; ?>" type="text" id="name" value="<?php echo $essai; ?>"></td> 
                            <td align="center"><input name="ONOFF<?php echo $id; ?>" type="checkbox" id="ONOFF" value="1"<?php echo $checked; ?> /></td> 
                        </tr> 
                        <?php 
                            } 
                        ?> 
                        <tr> 
                            <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> 
                        </tr> 
                    </table> 
                </td> 
            </tr> 
        </form> 
    </table> 
    <?php 
        // Check if button name "Submit" is active, do this 
        if(isset($_POST['Submit']))
        { 
            print_r($_POST);
            foreach($_POST['id'] as $id)
            { 
                $onoff = 0;
                if (isset($_POST["ONOFF".$id]))
                {
                    $onoff = 1;
                }
     
                $sql1 = "
                    UPDATE ".$tbl_name." 
                    SET essai='".$_POST["name".$id]."', onoff='".$onoff."' 
                    WHERE id_essai='".$id."'
                    LIMIT 1
                ";  
                $result1 = mysql_query($sql1,$link) or die(mysql_error());
            } 
            if ($result1)
            {
                header("Location:dlecteur_copie.php");
            }
        } 
        mysql_close(); 
    ?>
    Cela fonctionne correctement.

    Bien cordialement,

  9. #9
    Membre régulier Avatar de dlecteur
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    147
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juin 2006
    Messages : 147
    Points : 95
    Points
    95
    Par défaut
    Merci,

    `onoff` int(11) NOT NULL DEFAULT '0'
    moi je n'ai pas de champ onoff dans ma table?!!!

    Cdt.

  10. #10
    Membre expérimenté
    Avatar de Mahefasoa
    Homme Profil pro
    Manager IT
    Inscrit en
    Octobre 2003
    Messages
    835
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Madagascar

    Informations professionnelles :
    Activité : Manager IT

    Informations forums :
    Inscription : Octobre 2003
    Messages : 835
    Points : 1 664
    Points
    1 664
    Par défaut
    Je sais mais j'ai envoyé tout ce code + le SQL pour que tu le teste car ceci est fonctionnel chez moi. Ensuite à toi d'adapter le truc pour que cela fonctionne. Sinon, une autre alternative, communique le SQL des tes tables et je ferais un essai avec.

    Bien cordialement,

Discussions similaires

  1. Réponses: 3
    Dernier message: 03/04/2013, 17h34
  2. Réponses: 2
    Dernier message: 03/04/2013, 12h04
  3. [VB6]Mettre à jour une table avec les valeurs d'une DATAGrid
    Par mbzhackers dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 26/05/2006, 20h56
  4. Réponses: 2
    Dernier message: 02/05/2006, 09h50
  5. Réponses: 1
    Dernier message: 23/01/2006, 18h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo