probleme d'insertion dans une base de donnée mysql

esteller
Invité n'ayant pas de compte PHPfrance

21 juin 2010, 16:09

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Document sans titre</title>
</head>

<body>
<?php
function afficherformulaire($nom='',$prenom='',$genre=1,$datnais='',$datemb='',$salbrut='',$sitmat=1,$nbenf='',$numtel='',$email='',$photo='',$loisir1=1,$loisir2=1,$loisir3=1,$loisir4=1,$loisir5=1,$loisir6=1,$loisir7=1,$error='')
{if ($error!='') echo '<font color="red">'.$error.'</font>';
?>
<?php
function checkForm()
{
   $tabChamps = array('nom', 'prenom','genre','datnais','datemb', 'salbrut','sitmat','loisir1','loisir2','loisir3','loisir4','loisir5','loisir6','loisir7','nbenf', 'numtel', 'email','photo');
		
   for($i=0; $i<count($tabChamps); $i++)
   if(empty($_POST[$tabChamps[$i]]))
   {
      echo $tabChamps[$i]." n'a pas été renseigné";
      return false;
	  afficherformulaire($_POST['nom'],$_POST['prenom'],$_POST['genre'],$_POST['datnais'],
	$_POST['datemb'],$_POST['salbrut'],$_POST['sitmat'],$_POST['loisir1'],$_POST['loisir2'],$_POST['loisir3'],$_POST['loisir4'],$_POST['loisir5'],$_POST['loisir6'],$_POST['loisir7'],$_POST['nbenf'],
	$_POST['numtel'],$_POST['email'],$_FILES['photo'],$error);
	die();
   }
		
   echo 'Le formulaire a bien été rempli';
   return true;
}
?>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <input type="hidden"
name="MAX_FILE_SIZE"
value="100000000">

    NOM:<input type="text" name="nom" value="<?php echo $nom; ?>">
    <p>PRENOMS:<input type="text" name="prenom" value="<?php echo $prenom; ?>">
	<p>GENRE:<input type="radio" name="genre" width ="5" <?php if($genre==1) echo'checked';?>>Masculin
<input type="radio" name="genre" width ="5" <?php if($genre==2) echo'checked';?>>Feminin
	DATE DE NAISSANCE:<input type="text" name="datnais" value="<?php echo $datnais; ?>">
	DATE D'EMBAUCHE:<input type="text" name="datemb" value="<?php echo $datemb; ?>">
	SALAIRE BRUT:<input type="text" name="salbrut" value="<?php echo $salbrut; ?>">
	<p>SITUATION MATRIMONIALE:<input type="radio" name="sitmat" width ="5" <?php if($sitmat==1) echo'checked';?>>Célibataire
<input type="radio" name="sitmat" width ="5" <?php if($sitmat==2) echo'checked';?>>Marié
<input type="radio" name="sitmat" width ="5" <?php if($sitmat==3) echo'checked';?>>Veuf(ve)
<input type="radio" name="sitmat" width ="5" <?php if($sitmat==4) echo'checked';?>>Divorcé
	<p>LOISIR1:<input type="radio" name="loisir1" value='cinema' <?php echo ($_POST['loisir1']=='cinema') ? 'checked="checked"':''; ?>>Cinéma
<input type="radio" name="loisir1" value='pascinema' <?php echo ($_POST['loisir1']=='pascinema') ? 'checked="checked"':''; ?>>N'aime pas Cinéma


	<p>LOISIR2:<input type="radio" name="loisir2" value='musique' <?php echo ($_POST['loisir2']=='musique') ? 'checked="checked"':''; ?>>Musique
<input type="radio" name="loisir2" value='pasmusique' <?php echo ($_POST['loisir2']=='pasmusique') ? 'checked="checked"':''; ?>>N'aime pas Musique
	<p>LOISIR3:<input type="radio" name="loisir3" value='litterature' <?php echo ($_POST['loisir3']=='litterature') ? 'checked="checked"':''; ?>>Litterature
<input type="radio" name="loisir3" value='paslitterature' <?php echo ($_POST['loisir3']=='paslitterature') ? 'checked="checked"':''; ?>>N'aime pas Litteraure
	<p>LOISIR4:<input type="radio" name="loisir4" value='sport' <?php echo ($_POST['loisir4']=='sport') ? 'checked="checked"':''; ?>>Sport
<input type="radio" name="loisir4" value='passport' <?php echo ($_POST['loisir4']=='passport') ? 'checked="checked"':''; ?>>N'aime pas Sport
	<p>LOISIR5:<input type="radio" name="loisir5" value='religion' <?php echo ($_POST['loisir5']=='religion') ? 'checked="checked"':''; ?>>Religion
<input type="radio" name="loisir5" value='pasreligion' <?php echo ($_POST['loisir5']=='pasreligion') ? 'checked="checked"':''; ?>>N'aime pas Religion
	<p>LOISIR6:<input type="radio" name="loisir6" value='television' <?php echo ($_POST['loisir6']=='religion') ? 'checked="checked"':''; ?>>Religion
<input type="radio" name="loisir6" value='pasreligion' <?php echo ($_POST['loisir6']=='pasreligion') ? 'checked="checked"':''; ?>>N'aime pas Religion
	<p>LOISIR7:<input type="radio" name="loisir7" value='politique' <?php echo ($_POST['loisir7']=='politique') ? 'checked="checked"':''; ?>>Politique
<input type="radio" name="loisir7" value='paspolitique' <?php echo ($_POST['loisir7']=='paspolitique') ? 'checked="checked"':''; ?>>N'aime pas Politique
	<p>NOMBRE D'ENFANTS:<input type="text" name="nbenf" value="<?php echo $nbenf; ?>">
	NUMERO DE TELEPHONE:<input type="text" name="numtel" value="<?php echo $numtel; ?>">
	EMAIL:<input type="text" name="email" value="<?php echo $email; ?>">
	<p>PHOTO:<input type="file" name="photo" />
	<p><input type="submit" name="Valider" value="Enregister" />
	</form>
	<?php
	}
	?>
	<?php
	if(!isset($_POST['Valider']))
	{
	afficherformulaire();
	die();
	}
if(isset($_POST['Valider']))
	{
	checkForm();
	}
	
$dossierphotos="photo"; // dossier des photos
if($_FILES["photo"]["error"]!=0)
{
	$error="Erreur lors du chargement du fichier";
	afficherformulaire($_POST['nom'],$_POST['prenom'],$_POST['nbenf'],$_POST['salbrut'],$error);
	die;
}
$destinationfile=$dossierphotos.'/'.$_FILES["photo"]["name"];
while (is_file(destinationfile))
	$destinationfile=$dossierphotos.'/'.rand(0,1000).$_FILES["photo"]["name"];
	$b=move_uploaded_file($_FILES["photo"]["trop_name"],$destinationfile);
	if (!$b)
	{
	$error="Erruer lors de l'enregistrement de photo";
	afficherformulaire($_POST['nom'],$_POST['prenom'],$_POST['mus'],$_POST['tel'],$error);
	die;}
	
afficherformulaire($_POST['nom'],$_POST['prenom'],$_POST['numtel'],$_POST['salbrut'],$error);

require_once ("connexion1.php");
$link = mysql_connect ($host, $username, $password) or die (mysql_errno() ." : ".mysql_error());
$db=mysql_select_db($dbasename,$link); // ou $db=mysql_select_db(dbasename);
$query="INSERT INTO employe1(Nom,Prenoms,Genre,DateNaissance,DateEmbauche,SalaireBrut,SituationMatrimoniale,NombreEnfant,
NumTel,Email,Photo,Loisir1,Loisir2,Loisir3,Loisir4,Loisir5,Loisir6,Loisir7) VALUES('".$_POST['nom']."','".$_POST['prenom']."','".$_POST['datnais']."',".$_POST['datemb'].",". $_POST['salbrut'].",'".$_POST['sitmat']."',". $_POST['nbenf'].",'".$_POST['numtel']."','".$_POST['emal']."','".$destinationfile."','".$_POST['loisir1'].",'".$_POST['loisir2'].",'".$_POST['loisir3'].",'".$_POST['loisir4'].",'".$_POST['loisir5'].",'".$_POST['loisir6'].",'".$_POST['loisir7']."')";
$result=mysql_query($query);
?>
</p>
</body>
voici l'erreur:
Notice: Undefined index: loisir1 in C:\wamp\www\MonProjetWeb\PHP\enregistrements.php on line 48
>Cinéma
si vous pouvez m'aidez

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 10684 Messages

21 juin 2010, 22:24

voici l'erreur:
Notice: Undefined index: loisir1 in C:\wamp\www\MonProjetWeb\PHP\enregistrements.php on line 48
>Cinéma
si vous pouvez m'aidez
Ligne 48, tu fais appel à l'index "loisir1" d'un tableau alors que celui-ci n'est pas défini :
<p>LOISIR1:<input type="radio" name="loisir1" value='cinema' <?php echo ($_POST['loisir1']=='cinema') ? 'checked="checked"':''; ?>>Cinéma
<input type="radio" name="loisir1" value='pascinema' <?php echo ($_POST['loisir1']=='pascinema') ? 'checked="checked"':''; ?>>N'aime pas Cinéma
En fait, quand tu ouvres la page, tu n'as pas de valeurs passées en post, donc rien dans $_POST. Vérifies que ta variable existe pour pouvoir l'utiliser, par exemple :
<?php
$loisir = 'valeur par défaut'; // ex : 'cinema'
if (isSet($_POST['loisir1']))
  $loisir = $_POST['loisir1'];
?>
<p>LOISIR1:<input type="radio" name="loisir1" value='cinema' <?php echo ($loisir=='cinema') ? 'checked="checked"':''; ?>>Cinéma
<input type="radio" name="loisir1" value='pascinema' <?php echo ($loisir=='pascinema') ? 'checked="checked"':''; ?>>N'aime pas Cinéma
Ce n'est pas en améliorant la bougie que l'on a inventé l'ampoule...