Précédent   Forum des professionnels en informatique > PHP > Langage > Formulaires
Formulaires Forum d'entraide sur les formulaires avec PHP. Avant de poster -> FAQ formulaires, Cours de formulaires et Sources de formulaires
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 01/05/2008, 10h56   #1
Paulinho
Invité(e)
 
Messages : n/a
Détails du profil
Informations forums :
Messages : n/a
Points : 0
Par défaut Formulaire PHP + MySQL

Bonjour,

J'ai le code pour un formulaire avec le code suivant:
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
<!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>
<center><strong>INSCRIÇÃO</strong></center>
<title></title>
</head>
<body bgcolor="#98fb98"><br />
<br />
<form method="post">
 
Nome: <input name="nome" size="40" type="text" /><br />
<br />
 
Data de nascimento: <input name="data_nasc" size="10" type="text" /><br />
<br />
 
Morada: <input name="morada" size="50" type="text" /><br />
<br />
 
Telefone: <input name="telefone" size="9" type="text" /><br />
<br />
 
E-mail <input name="email" size="30" type="text" /><br />
<br />
 
Local de trabalho: <input name="local_trab" size="30" type="text" /><br />
<br />
 
Morada do local de trabalho: <input name="mor_trab" size="50" type="text" /><br />
<br />
 
User: <input name="user" size="20" type="text" /><br />
<br />
 
Password: <input name="password" size="15" type="text" /><br />
<br /><input name="MAX_FILE_SIZE" value="250000" type="hidden" />
Foto: <input name="foto" size="40" type="file" /><br />
<br /><input name="enviar" value="Enviar" type="submit" /><?php
$name=$_POST['nome'];
$birt_date=$_POST['data_nasc'];
$address=$_POST['morada'];
$phone=$_POST['telefone'];
$email=$_POST['email'];
$local_work=$_POST['local_trab'];
$address_work=$_POST['mor_trab'];
$user_login=$_POST['user'];
$password=$_POST['password'];
$picture=$_POST['foto'];
 
$con=mysql_connect("localhost","Paulo","fe2008")
or die("Não foi possível estabelecer conexão com o servidor MySQL.";
 
$db=mysql_select_db("user_profile",$con)
or die("Não foi possível estabelecer ligação com a base de dados.");
 
$res=mysql_query(INSERT INTO user_profile VALUES('','$name','$birt_date', '$address', '$phone', '$email', '$local_work', '$address_work', 'user_login', '$password', '$picture');
?></form>
</body>
</html>
Quand je le teste il m'apparait dans l'écran: Method Not Allowed
The requested method POST is not allowed for the URL /programdor_l/index.html.

Quelqu'un sait pourquoi?

Merci d'avance.
  Envoyer un message privé Réponse avec citation 00
Vieux 01/05/2008, 18h04   #2
Paulinho
Invité(e)
 
Messages : n/a
Détails du profil
Informations forums :
Messages : n/a
Points : 0
L'erreur est le http 405. Il me faut une authentification pour faire ça. J'utilise phpmyadmin sur cPanel. J'ai modifié les permission pour éxécuter le fichier formulario.php mais ça n'a rien changé.

Les donnés insérés ne sont pas dans la base de donnés.

Il me faut quelque chose dans le code?

Dernière modification par Paulinho ; 01/05/2008 à 19h00.
  Envoyer un message privé Réponse avec citation 00
Vieux 01/05/2008, 18h59   #3
Paulinho
Invité(e)
 
Messages : n/a
Détails du profil
Informations forums :
Messages : n/a
Points : 0
L'erreur est résolu.

Voici le code
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
 
<!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>
<center><strong>INSCRIÇÃO</strong></center>
<title></title>
</head>
<body bgcolor="#98fb98"><br />
<br />
<form method="post">
 
Nome: <input name="nome" size="40" type="text" /><br />
<br />
 
Data de nascimento: <input name="data_nasc" size="10" type="text" /><br />
<br />
 
Morada: <input name="morada" size="50" type="text" /><br />
<br />
 
Telefone: <input name="telefone" size="9" type="text" /><br />
<br />
 
E-mail <input name="email" size="30" type="text" /><br />
<br />
 
Local de trabalho: <input name="local_trab" size="30" type="text" /><br />
<br />
 
Morada do local de trabalho: <input name="mor_trab" size="50" type="text" /><br />
<br />
 
User: <input name="user" size="20" type="text" /><br />
<br />
 
Password: <input name="password" size="15" type="text" /><br />
<br /><input name="MAX_FILE_SIZE" value="250000" type="hidden" />
Foto: <input name="foto" size="40" type="file" /><br />
<br /><input name="enviar" value="Enviar" type="submit" /><?php
$name=$_POST['nome'];
$birt_date=$_POST['data_nasc'];
$address=$_POST['morada'];
$phone=$_POST['telefone'];
$email=$_POST['email'];
$local_work=$_POST['local_trab'];
$address_work=$_POST['mor_trab'];
$user_login=$_POST['user'];
$password=$_POST['password'];
$picture=$_POST['foto'];
 
$con=mysql_connect("localhost","pronatur_Paulo","fe2008");
 
$db=mysql_select_db("user_profile",$con);
 
$res=mysql_query("INSERT INTO user_profile('', 'name', 'birt_date', 'address', 'phone', 'email', 'local_work', 'address_work', 'user_login', 'password', 'picture') VALUES('','$name','$birt_date', '$address', '$phone', '$email', '$local_work', '$address_work', '$user_login', '$password', '$picture')");
mysql_close();
?></form>
</body>
</html>
J'envoye les donnés mais ils ne sont pas enregistrés sur la BD. Pourtant je pense que le code est correct.

Dernière modification par Paulinho ; 01/05/2008 à 19h16.
  Envoyer un message privé Réponse avec citation 00
Vieux 01/05/2008, 22h50   #4
Rédacteur
 
Avatar de darrylsite
 
Inscription : juillet 2007
Messages : 1 287
Détails du profil
Informations forums :
Inscription : juillet 2007
Messages : 1 287
Points : 1 756
Points : 1 756
Je crois que l' erreur provient de la requete sql.
Mettre des $variable des chaines comme tu l' as fait provoque toujours des erreurs.
Essaie plutot de faire un truc du genre :
Code :
1
2
3
 
$sql='INSERT INTO user_profile("", "name", "birt_date", "address", "phone", "email", "local_work", "address_work", "user_login", "password", "picture") VALUES';
$sql.='("","'.$name.'","'.$birt_date.'","'.$address.'", "'.$phone.'","'.$email.'","'.$local_work.'"," '.$address_work.'","'.$user_login.'","'.$password.'", "'.$picture.'")' ;
C' est mieux de concatener les variables. J' ai mis l' espace entre les ' et les " juste pour la presentation du code.
darrylsite est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/05/2008, 09h45   #5
Paulinho
Invité(e)
 
Messages : n/a
Détails du profil
Informations forums :
Messages : n/a
Points : 0
J'avais mis ça mais il ne m'enregistre pas...

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
 
<!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>
<center><strong>INSCRIÇÃO</strong></center>
<title></title>
</head>
<body bgcolor="#98fb98"><br />
<br />
<form method="post">
 
Nome: <input name="nome" size="40" type="text" /><br />
<br />
 
Data de nascimento: <input name="data_nasc" size="10" type="date" /><br />
<br />
 
Morada: <input name="morada" size="50" type="text" /><br />
<br />
 
Telefone: <input name="telefone" size="9" type="text" /><br />
<br />
 
E-mail <input name="email" size="30" type="text" /><br />
<br />
 
Local de trabalho: <input name="local_trab" size="30" type="text" /><br />
<br />
 
Morada do local de trabalho: <input name="mor_trab" size="50" type="text" /><br />
<br />
 
User: <input name="user" size="20" type="text" /><br />
<br />
 
Password: <input name="password" size="15" type="text" /><br />
<br /><input name="MAX_FILE_SIZE" value="250000" type="hidden" />
Foto: <input name="foto" size="40" type="file" /><br />
<br /><input name="enviar" value="Enviar" type="submit" /><?php
$name=$_POST['nome'];
$birt_date=$_POST['data_nasc'];
$address=$_POST['morada'];
$phone=$_POST['telefone'];
$email=$_POST['email'];
$local_work=$_POST['local_trab'];
$address_work=$_POST['mor_trab'];
$user_login=$_POST['user'];
$password=$_POST['password'];
$picture=$_POST['foto'];
 
$con=mysql_connect("localhost","pronatur_Paulo","fe2008");
 
$db=mysql_select_db("pronatur_socios",$con);
 
$sql='INSERT INTO user_profile("", "name", "birt_date", "address", "phone", "email", "local_work", "address_work", "user_login", "password", "picture") VALUES';
$sql.='("","'.$name.'","'.$birt_date.'","'.$address.'", "'.$phone.'","'.$email.'","'.$local_work.'"," '.$address_work.'","'.$user_login.'","'.$password.'", "'.$picture.'")';
 
?></form>
</body>
</html>
Dans la BD le champ data_nasc est du type DATE et dans le formulaire aussi. Cela est correct?

Merci.

Dernière modification par Paulinho ; 02/05/2008 à 09h57.
  Envoyer un message privé Réponse avec citation 00
Vieux 02/05/2008, 10h33   #6
Membre expérimenté
 
Homme Rémi BOURGAREL
Développeur .NET
Inscription : juin 2006
Messages : 426
Détails du profil
Informations personnelles :
Nom : Homme Rémi BOURGAREL
Âge : 25
Localisation : France, Haute Garonne (Midi Pyrénées)

Informations professionnelles :
Activité : Développeur .NET
Secteur : Tourisme - Loisirs

Informations forums :
Inscription : juin 2006
Messages : 426
Points : 584
Points : 584
il y a un probleme dans ton insert :
Code :
INSERT INTO user_profile("", "name", "birt_date", "address", "phone", "email", "local_work", "address_work", "user_login", "password", "picture") VALUES
apres user_profile il ne doit apparaitre que les noms des colonnes, donc t'es pas obligé de mettre les "" , et la colonne "" ça veut pas dire grand chose. Est ce que tu as un message d'erreur quelquonque ? Apres il y a peut etre un problème de date, essaie de voir du cotédu format.
Bourgui est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/05/2008, 11h28   #7
Paulinho
Invité(e)
 
Messages : n/a
Détails du profil
Informations forums :
Messages : n/a
Points : 0
J'ai éliminé "".

La date en MySQL est YYYY-MM-DD. Pourrais-je définir la date dans php comme text comme celui-ci? Je suis un peu perdu...
  Envoyer un message privé Réponse avec citation 00
Vieux 02/05/2008, 11h41   #8
Membre expérimenté
 
Homme Rémi BOURGAREL
Développeur .NET
Inscription : juin 2006
Messages : 426
Détails du profil
Informations personnelles :
Nom : Homme Rémi BOURGAREL
Âge : 25
Localisation : France, Haute Garonne (Midi Pyrénées)

Informations professionnelles :
Activité : Développeur .NET
Secteur : Tourisme - Loisirs

Informations forums :
Inscription : juin 2006
Messages : 426
Points : 584
Points : 584
en php 5.3 tu a date_create_from_format()
Bourgui est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/05/2008, 16h38   #9
Paulinho
Invité(e)
 
Messages : n/a
Détails du profil
Informations forums :
Messages : n/a
Points : 0
Cette function change l'ordre de la date. Mais pour moi c'est bien comme ça, YYYY-MM-DD. Seulement je ne sais pas le type que je dois utiliser dans lors de l'input...
Je suis bloqué et pourtant je n'ai pas de message d'erreur...
  Envoyer un message privé Réponse avec citation 00
Vieux 02/05/2008, 16h48   #10
Membre expérimenté
 
Homme Rémi BOURGAREL
Développeur .NET
Inscription : juin 2006
Messages : 426
Détails du profil
Informations personnelles :
Nom : Homme Rémi BOURGAREL
Âge : 25
Localisation : France, Haute Garonne (Midi Pyrénées)

Informations professionnelles :
Activité : Développeur .NET
Secteur : Tourisme - Loisirs

Informations forums :
Inscription : juin 2006
Messages : 426
Points : 584
Points : 584
ben le type texte suffit, apres faut "parser" correctement la chaine rentrée , et pour envoyer une date a mysql .

http://dev.mysql.com/doc/refman/5.0/en/datetime.html

Donc si c'est de type date, il te faut envoyer a mysql une chaine sous le format YYYY-MM-DD (yyyy-MM-dd en php).
Bourgui est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/05/2008, 16h26   #11
Paulinho
Invité(e)
 
Messages : n/a
Détails du profil
Informations forums :
Messages : n/a
Points : 0
J'ai fait cela:
Code :
1
2
$bir_date=$_POST['birt_date'];
$birt_date=date('Ymd');
C'est correct?

Je trouve étrange c'est que quand j'envoye les données il ne m'apparaît pas d'erreur. Il accepte ma connexion. Pourquoi pas il n'enregistre pas mes données?

Il me faut seulement cela pour terminer mon travail...
  Envoyer un message privé Réponse avec citation 00
Vieux 04/05/2008, 17h50   #12
Membre expérimenté
 
Homme Rémi BOURGAREL
Développeur .NET
Inscription : juin 2006
Messages : 426
Détails du profil
Informations personnelles :
Nom : Homme Rémi BOURGAREL
Âge : 25
Localisation : France, Haute Garonne (Midi Pyrénées)

Informations professionnelles :
Activité : Développeur .NET
Secteur : Tourisme - Loisirs

Informations forums :
Inscription : juin 2006
Messages : 426
Points : 584
Points : 584
a tu rajouté un 'or die(mysql_error());' apres ton mysql_query() ?
Bourgui est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/05/2008, 17h56   #13
Paulinho
Invité(e)
 
Messages : n/a
Détails du profil
Informations forums :
Messages : n/a
Points : 0
Oui.

$dbh=mysql_connect ("localhost", "pronatur_Paulo", "fe2008") or die ('I
cannot connect to the database because: ' . mysql_error());
mysql_select_db ("pronatur_socios");

Y-a-t-il un test en php pour vérifier si la base de données peut être modifié, voir insérer des nouvelles entrées? J'ai tous les privilèges sur la base. C'est vraiment étrange!

Merci encore.
  Envoyer un message privé Réponse avec citation 00
Vieux 04/05/2008, 18h27   #14
Paulinho
Invité(e)
 
Messages : n/a
Détails du profil
Informations forums :
Messages : n/a
Points : 0
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
 
<!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" xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml">
<head>
<center><strong>A Minha Página</strong></center>
<title></title>
</head>
<body bgcolor="#98fb98"><br />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body><br />
<?php
 
// connect and select a database
$link = mysql_connect("localhost", "pronatur_Paulo", "fe2008")
or die ("Couldn't connect: Check to make sure that:<br>" .
"<ul><li>your MySQL server is running</li>" .
"<li>you used the correct hostname (<tt>vergil/ovid</tt>)<li>" .
"<li>you didn't forget the 'u' in the hostname</li>" .
"<li>you added a colon with your port number after the hostname</li>" .
"<li>you used the username 'root'</li>" .
"<li>you used the correct root password</li>" .
"<li>you didn't forget to close a set of quotation marks</li><br><br>");
print "Connected successfully.<br>";
$db = "pronatur_socios";
mysql_select_db($db) or die("Could not select the database '" . $db . "'. Are you sure it exists?");
 
// perform an SQL query
$query = "SELECT * FROM user_profile";
$result = mysql_query($query) or die("Query failed");
print($result);
 
// free result set
mysql_free_result($result);
 
// close the connection
mysql_close($link);
 
?>
</body>
</html>
J'ai fait ce test et tout est parfait... sauf la requête que m'envoye ça: Resource id #2.

Dernière modification par Paulinho ; 04/05/2008 à 19h26.
  Envoyer un message privé Réponse avec citation 00
Vieux 05/05/2008, 07h28   #15
Membre Expert
 
Avatar de darkstar123456
 
Inscription : mars 2008
Messages : 1 180
Détails du profil
Informations personnelles :
Âge : 28
Localisation : Belgique

Informations forums :
Inscription : mars 2008
Messages : 1 180
Points : 1 357
Points : 1 357
Envoyer un message via Skype™ à darkstar123456
Tu fais ceci
Code php :
1
2
3
$query = "SELECT * FROM user_profile";
$result = mysql_query($query) or die("Query failed");
print($result);

Or, tu ne peux pas afficher le contenu d'une requête de la sorte !

Quoi qu'il arrive, si ton code ne marche pas, je pense tout simplement que c'est parcque tu n'as pas d'action dans ton <form> du coup, il ne charge pas la bonne page à l'envoi du formulaire.

Correction :

Code html :
1
2
3
4
5
6
 
<!-- Tu as écrit ceci : -->
<form method="post">
 
<!-- Ecris plutôt ceci : -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
__________________
N'hésitez pas à me contacter !
darkstar123456 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/05/2008, 18h48   #16
Membre régulier
 
Avatar de loopback
 
Inscription : septembre 2006
Messages : 170
Détails du profil
Informations forums :
Inscription : septembre 2006
Messages : 170
Points : 75
Points : 75
Tu dois aussi vérifier que le formulaire a bien été envoyé avec isset():

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
<?php
if(isset($_POST['enviar']) && $_POST['enviar'] == 'Enviar') { //On vérifie que le formulaire a bien été envoyé
 
$name = mysql_real_escape_string(htmlspecialchars($_POST['nome'])); //On protège les variables
$birt_date = mysql_real_escape_string(htmlspecialchars($_POST['data_nasc']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['morada']));
$phone = mysql_real_escape_string(htmlspecialchars($_POST['telefone']));
$email = mysql_real_escape_string(htmlspecialchars($_POST['email']));
$local_work = mysql_real_escape_string(htmlspecialchars($_POST['local_trab']));
$address_work = mysql_real_escape_string(htmlspecialchars($_POST['mor_trab']));
$user_login = mysql_real_escape_string(htmlspecialchars($_POST['user']));
$password= mysql_real_escape_string(htmlspecialchars($_POST['password']));
 
//On vérifie qu'un fichier a été posté
if(isset($_POST['foto'])) {
	$fichier = fopen($_FILES['foto']['tmp_name'], "rb");
	if(!$fichier)
	die('Une erreur est survenue lors de l\'ouverture du fichier');
	$attachement = fread($fichier, filesize($_FILES['foto']['tmp_name']));
	$attachement = chunk_split(base64_encode($attachement));
	fclose($fichier);
	}
 
//On se connecte 
$con = mysql_connect("localhost","pronatur_Paulo","fe2008") or die(mysql_error());
mysql_select_db("user_profile",$con) or die(mysql_error());
 
$res = mysql_query("INSERT INTO user_profile VALUES('','".$name."','".$birt_date."', '".$address."', '".$phone."', '".$email."', '".$local_work."', '".$address_work."', '".$user_login."', '".$password."', '".$picture."')",$con) or die(mysql_error());
mysql_close($con);//On se déconnecte
}
else { // Si le formulaire n'a pas été envoyé on l'affiche
print '<!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>
<center><strong>INSCRIÇÃO</strong></center>
<title></title>
</head>
<body bgcolor="#98fb98"><br />
<br />
<form method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
 
Nome: <input name="nome" size="40" type="text" /><br />
<br />
 
Data de nascimento: <input name="data_nasc" size="10" type="text" /><br />
<br />
 
Morada: <input name="morada" size="50" type="text" /><br />
<br />
 
Telefone: <input name="telefone" size="9" type="text" /><br />
<br />
 
E-mail <input name="email" size="30" type="text" /><br />
<br />
 
Local de trabalho: <input name="local_trab" size="30" type="text" /><br />
<br />
 
Morada do local de trabalho: <input name="mor_trab" size="50" type="text" /><br />
<br />
 
User: <input name="user" size="20" type="text" /><br />
<br />
 
Password: <input name="password" size="15" type="password" /><br />
<br /><input name="MAX_FILE_SIZE" value="250000" type="hidden" />
Foto: <input name="foto" size="40" type="file" /> (250Ko max)<br />
<br /><input name="enviar" value="Enviar" type="submit" />
</form>
</body>
</html>';
}
?>

et si tu ne places pas ceci enctype="multipart/form-data" dans la balise form de ton formulaire il y a peu de chance que tu puisses envoyer le fichier joint.
loopback est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Mettre Résolu
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 00h14.


 
 
 
 
Partenaires

Hébergement Web