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

Langage PHP Discussion :

Formulaire PHP + MySQL


Sujet :

Langage PHP

  1. #1
    Paulinho
    Invité(e)
    Par défaut Formulaire PHP + MySQL
    Bonjour,

    J'ai le code pour un formulaire avec le code suivant:
    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
    <!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.

  2. #2
    Paulinho
    Invité(e)
    Par défaut
    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.

  3. #3
    Paulinho
    Invité(e)
    Par défaut
    L'erreur est résolu.

    Voici le 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
     
    <!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.

  4. #4
    Rédacteur
    Avatar de darrylsite
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 299
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 299
    Points : 2 501
    Points
    2 501
    Par défaut
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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.

  5. #5
    Paulinho
    Invité(e)
    Par défaut
    J'avais mis ça mais il ne m'enregistre pas...

    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
     
    <!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.

  6. #6
    Invité
    Invité(e)
    Par défaut
    il y a un probleme dans ton insert :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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.

  7. #7
    Paulinho
    Invité(e)
    Par défaut
    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...

  8. #8
    Invité
    Invité(e)
    Par défaut
    en php 5.3 tu a date_create_from_format()

  9. #9
    Paulinho
    Invité(e)
    Par défaut
    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...

  10. #10
    Invité
    Invité(e)
    Par défaut
    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).

  11. #11
    Paulinho
    Invité(e)
    Par défaut
    J'ai fait cela:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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...

  12. #12
    Invité
    Invité(e)
    Par défaut
    a tu rajouté un 'or die(mysql_error());' apres ton mysql_query() ?

  13. #13
    Paulinho
    Invité(e)
    Par défaut
    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.

  14. #14
    Paulinho
    Invité(e)
    Par défaut
    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
     
    <!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.

  15. #15
    Membre émérite Avatar de darkstar123456
    Homme Profil pro
    Développeur Web
    Inscrit en
    Mars 2008
    Messages
    1 896
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Mars 2008
    Messages : 1 896
    Points : 2 835
    Points
    2 835
    Par défaut
    Tu fais ceci
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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']; ?>">

  16. #16
    Membre régulier Avatar de loopback
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    170
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 170
    Points : 113
    Points
    113
    Par défaut
    Tu dois aussi vérifier que le formulaire a bien été envoyé avec isset():

    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
    <?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.

Discussions similaires

  1. [MySQL] Formulaire PHP ->MySQL
    Par shub dans le forum PHP & Base de données
    Réponses: 14
    Dernier message: 09/02/2010, 22h42
  2. [MySQL] Probléme de apostrophe a mettre en formulaire php/mysql
    Par mecmec dans le forum PHP & Base de données
    Réponses: 1
    Dernier message: 02/06/2007, 20h42
  3. [MySQL] Formulaire PHP --> MySQL
    Par Badaboumpanpan dans le forum PHP & Base de données
    Réponses: 11
    Dernier message: 23/06/2006, 17h19
  4. Formulaire PHP-MySQL
    Par fara morgana dans le forum Langage
    Réponses: 11
    Dernier message: 09/05/2006, 15h46
  5. [MySQL] Bug Formulaire PHP-MySQL
    Par Davboc dans le forum PHP & Base de données
    Réponses: 17
    Dernier message: 01/03/2006, 15h15

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