Précédent   Forum des professionnels en informatique > PHP > PHP & SGBD > PHP & MySQL
PHP & MySQL Forum d'entraide sur les fonctions MySQL avec PHP. Avant de poster -> FAQ MySQL, Cours MySQL et Sources MySQL. Pour les questions concernant le moteur MySQL plutôt que les fonctions PHP, merci d'utiliser le forum MySQL.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 22/04/2011, 21h13   #1
Invité de passage
 
Homme alain gaillard
Technicien maintenance
Inscription : avril 2011
Messages : 15
Détails du profil
Informations personnelles :
Nom : Homme alain gaillard
Localisation : France

Informations professionnelles :
Activité : Technicien maintenance
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : avril 2011
Messages : 15
Points : 3
Points : 3
Par défaut Requete ok mais pas pour tout

Bonjours à tous,

Voici mon soucis :

Dans un tableau html je recupere les données d'une table SQL.
A partir de ce tableau je recopie les lignes choisies par checkbox vers une autre table SQL.

Le soucis c'est que cela me recopie que l'ID et non le reste.

Voici mes codes:

Choix.php

Code php :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
<?php
$db = mysql_connect('localhost', 'root', '') or die('HOST ?'); 
mysql_select_db('Adherents',$db) or die('DB ?');
 
 $id=$_POST['choix'];
 if(isset($_POST['choix']))
  {
  foreach($_POST['choix'] as $val)
    {
    //affichage des elements du tableau
    $sql = "INSERT INTO sortie (Nom, Prenom, id  )  VALUES ('$Nom','$Prenom', '$val')";
 
               mysql_query($sql) or die('Erreur SQL !'.$sql.'<br>'.mysql_error());
          echo 'Vos infos on été ajoutées.';
    }
  }
 
 
header ('location: tri.php');
 
?>

Et tableau.php

Code php :
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
 
 
<!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>TRI</title>
</head>
 
<body background="Images/WB02134_.GIF">
 
 
 <form method='POST' action='choix.php'>
<table align="center" border="1">
<?php 
$db = mysql_connect('localhost', 'root', '') or die('HOST ?'); 
mysql_select_db('Adherents',$db) or die('DB ?'); 
 
      $query='Select * from enfants ' ;
 
      $result = mysql_query( $query )or exit ('Erreur SQL !'.$query.'<br>'.mysql_error());
echo '<tr>
<td>id</td>
<td>Nom</td>
<td>Prenom</td>
<td>Choix</choix></tr';
 
      while($data = mysql_fetch_array($result))
 
{
 
 
echo ' <tr><td>'.$data['id'].'</td>
 
 <td>'.$data['Nom'].'</td>
 
<td>'.$data['Prenom'].'</td>
 
<td><input type="checkbox" name="choix[]" values='.$data['id'].'/></td></tr>';
 
}
 
?> 	
</table> 
<div align="center"><input type="submit" name="submit" value="Validez" /></div>
 
</form>
 
 
 
</body>
</html>

Si vous avez une réponse, je vous en remercie par avance

Alain
casper77 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/04/2011, 22h30   #2
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
Normal puisqu'il n'y a que l'id dans ton formulaire.

Mais tu peux tout faire en un seul coup à partir de l'id en copiant directement de table à table :
Code :
1
2
3
4
 
if(isset($_POST['choix'])) {
$sql = 'INSERT INTO sortie (SELECT Nom, Prenom, Id FROM enfants WHERE Id IN(' . implode(',', $_POST['choix']) .')';
}
Par contre d'un point de vue relationnel, ce n'est peut etre pas une bonne idée de dupliquer les informations nom et prenom sur les deux tables.
A moins qu'un enfant change de nom entre son inscription et la sortie.
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/04/2011, 23h09   #3
Invité de passage
 
Homme alain gaillard
Technicien maintenance
Inscription : avril 2011
Messages : 15
Détails du profil
Informations personnelles :
Nom : Homme alain gaillard
Localisation : France

Informations professionnelles :
Activité : Technicien maintenance
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : avril 2011
Messages : 15
Points : 3
Points : 3
Par défaut re

Merci pour le code, message d'erreur

Erreur SQL !INSERT INTO sortie (SELECT Nom, Prenom, Id FROM enfants WHERE Id IN(1/)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

Ce n'est pas une copie de la table que je veux faire, mais selectionner les enfants qui participeront à une sortie, et de le mettre en ligne sur le site afin qu'ils puissent voir si ils en seront plus rapidement

Alain
casper77 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/04/2011, 12h11   #4
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
Tu n'as donc besoin d'enregistrer dans ta table que l'id des enfants.
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/04/2011, 13h26   #5
Invité de passage
 
Homme alain gaillard
Technicien maintenance
Inscription : avril 2011
Messages : 15
Détails du profil
Informations personnelles :
Nom : Homme alain gaillard
Localisation : France

Informations professionnelles :
Activité : Technicien maintenance
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : avril 2011
Messages : 15
Points : 3
Points : 3
Par défaut re

Bin non, cette liste est aussi faite pour être imprimée pour un listing.
Là je n'ai mis qu'une partie de la table, il y a N° de tel portable et autre en plus.
J'ai recherche en rajoutant des checkbox dans toutes les cases, mais cela ne renvois que le dernier ID coché.
tri.php
Code php :
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
 
<!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>TRI</title>
</head>
 
<body background="Images/WB02134_.GIF">
 
 
 <form method='POST' action='choix.php'>
<table align="center" border="1">
<?php 
$db = mysql_connect('localhost', 'root', '') or die('HOST ?'); 
mysql_select_db('Adherents',$db) or die('DB ?'); 
 
      $query='Select * from enfants ' ;
 
      $result = mysql_query( $query )or exit ('Erreur SQL !'.$query.'<br>'.mysql_error());
echo '<tr>
<td>id</td>
<td>Nom</td>
<td>Prenom</td>
 
</tr>';
 
      while($data = mysql_fetch_array($result))
 
{
 
 
echo ' <tr><input type="checkbox" name="choix[]" values='.$data['id'].'/><td>'.$data['id'].'</td>
 
 <input type="checkbox" name="choix1[]" values='.$data['id'].'/><td>'.$data['Nom'].'</td>
 
<input type="checkbox" name="choix2[]" values='.$data['id'].'/><td>'.$data['Prenom'].'</td>
 
</tr>';
 
}
 
?> 	
</table> 
<div align="center"><input type="submit" name="submit" value="Validez" /></div>
 
</form>
 
 
 
</body>
</html>
choix.php
Code php :
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
 
<?php
$db = mysql_connect('localhost', 'root', '') or die('HOST ?'); 
mysql_select_db('Adherents',$db) or die('DB ?');
 
 
 
$id=$_POST['choix'];
$id=$_POST['choix1'];
$id=$_POST['choix2'];
 
 
 
 
 if(isset($_POST['choix']))
 if(isset($_POST['choix1']))
if(isset($_POST['choix2']));
  {
 
  foreach($_POST['choix'] as $val) 
  foreach($_POST['choix1'] as $val1)
  foreach($_POST['choix2'] as $val2);
} 
 
    {
 
    //affichage des elements du tableau
 
    $sql = "INSERT INTO sortie( Nom, Prenom,id) VALUES('$val1','$val2','$val')";
 
 
 
               mysql_query($sql) or die('Erreur SQL !'.$sql.'<br>'.mysql_error());
 
          echo 'Vos infos on été ajoutées.';
 
    }
 
 }
 
header ('location: tri.php');
 
?>
casper77 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/04/2011, 16h05   #6
Modérateur
 
Avatar de sabotage
 
Homme Vincent
Inscription : juillet 2005
Messages : 14 929
Détails du profil
Informations personnelles :
Nom : Homme Vincent

Informations forums :
Inscription : juillet 2005
Messages : 14 929
Points : 16 381
Points : 16 381
Les informations sont déjà dans la table "enfants" tu n'as pas besoin de les avoir en double dans la table "sortie", tu as juste besoin de l'id pour faire la liaison entre les deux tables.
sabotage est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 06h08.


 
 
 
 
Partenaires

Hébergement Web