Formulaire php vers base MySQL

Résolu/Fermé
Dario - 24 mai 2012 à 12:53
 Dario - 24 mai 2012 à 14:31
Salut les amis
je vous soumets ce code.. il s'agit d'un formulaire en PHP qui renvoi les données vers une base. Je ne comprends pas ce qui ne va pas, il marche par coup.
Tant vous saisissez, les données entre dans la base, tantôt ça n'entre pas!!

Aidez moi à trouver l'erreur SVP !!!

<?php
$server="localhost";
$user="root";
$pwd="";
$conn=@mysql_connect($server,$user,$pwd);

If ($conn == FALSE)
{
die ("connexion impossible<br/>");
}

//SELECTION DE LA BASE DE DONNEES
$bdd="gestionbase";
$connect_base = @mysql_select_db($bdd);

If ($connect_base == FALSE)
{
die ("Sélection BASE Impossible<br/>");
}

//INSERE LES DONNEES DE LA BASE
if(isset($_POST['save'])) //ce test c'est pour que PHP attende le clic sur enregistrer
{
$matricule=$_POST["matricule"];
$name=$_POST["id"][0];
$prename=$_POST["id"][1];
$email=$_POST["mail"];
$pass=$_POST["pass"];
$profil=$_POST["profil"];
foreach($profil as $valeur)
{
$mind_profil = $valeur;
}


$cod=$_POST["code"];
foreach($cod as $val)
{
$mind_cod = $val;
}

$qualif=$_POST["qualification"];
$tel=$_POST["telphone"];
if(empty($matricule))
{
echo"<script type=\"text/javascript\">" ;
echo "alert('Veuillez renseigner les champs sont obligatoires s\'il vous plaît !');";
echo "</script>";
}
else
{

$sql = "INSERT INTO agent (matricule_agent, nom_agent, prenom_agent, qualification, email, motdepasse, num_tel, code_bureau, code_profil) values ('$matricule', '$name', '$prename', '$qualif', '$email', '$pass', '$tel', '$mind_cod', '$mind_profil')";
mysql_query ($sql, $conn);

header('Location: succes.html');
}
}
?>

<!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>Connexion | KVIK ProductionS</title>
</head>

<body style="margin:30px; font-family:Geneva, Arial, Helvetica, sans-serif">

<center>

<form method="post" name="inscrire_agent" action="Enregistrement_Agent.php" style="width:500px;">
<fieldset style="text-align:left">
<legend><b>Vos coordonnées</b></legend>
<table>
<tr>
<td width="150"> <b>Matricule * : </b> </td>
<td width="200">

<div align="justify">
<input name="matricule" type="text" maxlength="50" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Nom : </b> </td>
<td width="200">

<div align="justify">
<input name="id[]" type="text" maxlength="50" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Prénom * : </b> </td>
<td width="200">

<div align="justify">
<input name="id[]" type="text" maxlength="50" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Email : </b> </td>
<td width="200">

<div align="justify">
<input name="mail" type="text" maxlength="50" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Mot de passe : </b> </td>
<td width="200">

<div align="justify">
<input name="pass" type="password" maxlength="20" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Code Profil : </b> </td>
<td width="200">

<div align="justify">
<select name="profil[]" style="width:150px">
<?php
$ssql = "select * from profil";
$result = mysql_query ($ssql);

while ($row = mysql_fetch_object($result)) {
if ($row->code_profil==$code_profil_int) {$selected="selected";} else {$selected="";}
echo "<option value='$row->code_profil' $selected>$row->code_profil -- $row->libelle_profil</option>";
}
?>
</select>
</div></td>
</tr>

<tr>
<td width="150"> <b>Qualification : </b> </td>
<td width="200">

<div align="justify">
<input name="qualification" type="text" maxlength="50" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Code bureau : </b> </td>
<td width="200">

<div align="justify">
<select name="code[]" style="width:150px">
<?php
$ssql = "select * from bureau";
$result = mysql_query ($ssql);

while ($row = mysql_fetch_object($result)) {
if ($row->code_bureau==$code_bureau_int) {$selected="selected";} else {$selected="";}
echo "<option value='$row->code_bureau' $selected>$row->code_bureau -- $row->libelle_bureau</option>";
}
?>
</select>
</div></td>
</tr>

<tr>
<td width="150"> <b>Téléphone : </b> </td>
<td width="200">

<div align="justify">
<input name="telphone" type="text" maxlength="20" />
</div></td>
</tr>
</table>
</fieldset>
<br/>

<table width="100%">
<tr align="center">
<td width="130" align="center"> <input type="submit" name="save" value="Enregistrer" /> </td>
<td width="130" align="center"> <input type="reset" name="efface" value="Annuler" /> </td>
</tr>
</table>
</form>

</center>

</body>
</html>


A voir également:

2 réponses

Alain_42 Messages postés 5361 Date d'inscription dimanche 3 février 2008 Statut Membre Dernière intervention 13 février 2017 894
24 mai 2012 à 13:50
pourquoi tu t'embêtes avec des name= ....[] sous forme d'array quand ce n'est pas indispensable

surtout si ensuite tu récupères la valeur comme tu as fait par une boucle qui ne garde que la dernière de l'array

<?php
$server="localhost";
$user="root";
$pwd="";
$conn=@mysql_connect($server,$user,$pwd);

If ($conn == FALSE)
{
die ("connexion impossible<br/>");
}

//SELECTION DE LA BASE DE DONNEES
$bdd="gestionbase";
$connect_base = @mysql_select_db($bdd);

If ($connect_base == FALSE)
{
die ("Sélection BASE Impossible<br/>");
}

//INSERE LES DONNEES DE LA BASE
if(isset($_POST['save'])) //ce test c'est pour que PHP attende le clic sur enregistrer
{
$matricule=mysql_real_escape_string($_POST["matricule"]);
$name=mysql_real_escape_string($_POST["nom"]);
$prename=mysql_real_escape_string($_POST["prenom"]);
$email=mysql_real_escape_string($_POST["mail"]);
$pass=mysql_real_escape_string($_POST["pass"]);
$mind_profil=mysql_real_escape_string($_POST["profil"]);
$mind_cod=mysql_real_escape_string($_POST["code"]);
$qualif=mysql_real_escape_string($_POST["qualification"]);
$tel=mysql_real_escape_string($_POST["telphone"]);

if(empty($matricule))
{
echo"<script type=\"text/javascript\">" ;
echo "alert('Veuillez renseigner les champs sont obligatoires s\'il vous plaît !');";
echo "</script>";
}
else
{

$sql = "INSERT INTO agent (matricule_agent, nom_agent, prenom_agent, qualification, email, motdepasse, num_tel, code_bureau, code_profil) values ('$matricule', '$name', '$prename', '$qualif', '$email', '$pass', '$tel', '$mind_cod', '$mind_profil')";

$insert=mysql_query ($sql, $conn);
if($insert){
	header('Location: succes.html');
}else{
	echo "Pb lors de l'insertion BDD !";
}
}
}
?>

<!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>Connexion | KVIK ProductionS</title>
</head>

<body style="margin:30px; font-family:Geneva, Arial, Helvetica, sans-serif">

<center>

<form method="post" name="inscrire_agent" action="Enregistrement_Agent.php" style="width:500px;">
<fieldset style="text-align:left">
<legend><b>Vos coordonnées</b></legend>
<table>
<tr>
<td width="150"> <b>Matricule * : </b> </td>
<td width="200">

<div align="justify">
<input name="matricule" type="text" maxlength="50" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Nom : </b> </td>
<td width="200">

<div align="justify">
<input name="nom" type="text" maxlength="50" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Prénom : </b> </td>
<td width="200">

<div align="justify">
<input name="prenom" type="text" maxlength="50" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Email : </b> </td>
<td width="200">

<div align="justify">
<input name="mail" type="text" maxlength="50" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Mot de passe : </b> </td>
<td width="200">

<div align="justify">
<input name="pass" type="password" maxlength="20" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Code Profil : </b> </td>
<td width="200">

<div align="justify">
<select name="profil" style="width:150px">
<?php
$ssql = "SELECT * FROM profil";
$result = mysql_query ($ssql);

while ($row = mysql_fetch_object($result)) {
if ($row->code_profil==$code_profil_int) {$selected="selected";} else {$selected="";}
echo "<option value='$row->code_profil' $selected>$row->code_profil -- $row->libelle_profil</option>";
}
?>
</select>
</div></td>
</tr>

<tr>
<td width="150"> <b>Qualification : </b> </td>
<td width="200">

<div align="justify">
<input name="qualification" type="text" maxlength="50" />
</div></td>
</tr>

<tr>
<td width="150"> <b>Code bureau : </b> </td>
<td width="200">

<div align="justify">
<select name="code" style="width:150px">
<?php
$ssql = "select * from bureau";
$result = mysql_query ($ssql);

while ($row = mysql_fetch_object($result)) {
if ($row->code_bureau==$code_bureau_int) {$selected="selected";} else {$selected="";}
echo "<option value='$row->code_bureau' $selected>$row->code_bureau -- $row->libelle_bureau</option>";
}
?>
</select>
</div></td>
</tr>

<tr>
<td width="150"> <b>Téléphone : </b> </td>
<td width="200">

<div align="justify">
<input name="telphone" type="text" maxlength="20" />
</div></td>
</tr>
</table>
</fieldset>
<br/>

<table width="100%">
<tr align="center">
<td width="130" align="center"> <input type="submit" name="save" value="Enregistrer" /> </td>
<td width="130" align="center"> <input type="reset" name="efface" value="Annuler" /> </td>
</tr>
</table>
</form>

</center>

</body>
</html> 
0
merci !!! je m'en étais en sorti notamment avec "mysql_real_escape_string".. Mais si j'avais pas pu, c'est clair que c'est vous qui m'aurez aidé !! MERCI BEAUCOUP !!
0