Informations qui ne s'ajoutent pas à une base de données ...

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : Informations qui ne s'ajoutent pas à une base de données ...

par Luverger » 09 févr. 2007, 12:07

C'est bon j'ai trouvé d'ou venais le problème :

Voici le code ma page (+ ou - "optimisé") :


<?php require_once('../Connexions/ConnexionArchives.php'); ?>
<?php

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "varchar":
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Numaff";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "Client";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "Nomaff";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Resprojet";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . $_SERVER['QUERY_STRING'];
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
  $insertSQL = sprintf("INSERT INTO Archives (Numaff, Client, Nomaff, Resprojet) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['Numaff'], "text"),
                       GetSQLValueString($_POST['Client'], "text"),
                       GetSQLValueString($_POST['Nomaff'], "text"),
                       GetSQLValueString($_POST['Resprojet'], "text"));

  mysql_select_db($database_ConnexionArchives, $ConnexionArchives);
  $Result1 = mysql_query($insertSQL, $ConnexionArchives) or die(mysql_error());
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO Archives (Numaff, Client, Nomaff, Resprojet) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['Numaff'], "text"),
                       GetSQLValueString($_POST['Client'], "text"),
                       GetSQLValueString($_POST['Nomaff'], "text"),
                       GetSQLValueString($_POST['Resprojet'], "text"));
  //!- DEBUT AJOUT
  echo $insertSQL,"<br/>\n";
  //!- FIN AJOUT
  mysql_select_db($database_ConnexionArchives, $ConnexionArchives);
  $Result1 = mysql_query($insertSQL, $ConnexionArchives) or die(mysql_error());

  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_ConnexionArchives, $ConnexionArchives);
$query_rsDossiers = "SELECT Numaff, Client, Nomaff, Resprojet FROM Archives ORDER BY Numaff";
$rsDossiers = mysql_query($query_rsDossiers, $ConnexionArchives) or die(mysql_error());
$row_rsDossiers = mysql_fetch_assoc($rsDossiers);
$totalRows_rsDossiers = mysql_num_rows($rsDossiers);
?>
<html><!-- InstanceBegin template="/Templates/PageArchives.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Pajout d'ajout de dossier aux archives</title>
<!-- InstanceEndEditable --><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body bgcolor="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF">
<table width="803" height="256" border="0" align="center" cellspacing="0">
  <tr align="right" valign="top">
    <td height="100" colspan="3" bordercolor="0"><div align="center">
        <p align="left"><strong><img src="../Images/logo%20CMF.jpg" width="339" height="100"><img src="../Images/logo%20Archives.jpg" width="461" height="100"></strong></p>
    </div>      </td>
  </tr>
  <tr valign="bottom">
    <td height="18" colspan="2"><div align="left"><font color="0494bc" size="2" face="Arial, Helvetica, sans-serif">L.V.</font></div></td>
    <td height="18"><div align="right"><font color="0494bc" size="2" face="Arial, Helvetica, sans-serif">v.1.2007</font></div></td>
  </tr>
  <tr align="center" valign="middle" background="../Images/Degrade2.jpg">
    <td width="230" height="35">
    <a href="../index.php"><font size="2"><font color="#666666" face="Arial, Helvetica, sans-serif"><strong>RECHERCHE</strong></font></font></a>   </td>
    <td width="335" height="35"><div align="center">
    <div align="center">    </div>      </td>
    <td width="238" height="35"><div align="center">
        <select name="menuListes" onChange="MM_jumpMenu('parent',this,0)">
          <option>-- LISTES DES DOSSIERS -- </option>
          <option value="../Listes/listenumaff.php">Par N&deg; affaire</option>
          <option value="../Listes/listeclient.php">Par nom du client</option>
          <option value="../Listes/listenomaff.php">Par titre de l'affaire</option>
          <option value="../Listes/listeresprojet.php">Par responsables de projets</option>
        </select>
    </div></td>
  </tr>
  <tr bgcolor="#F2F2F2">
    <td height="58" colspan="3"><div align="center"><font size="6"><font face="Arial, Helvetica, sans-serif"></font></font>
      <p><font size="6"><!-- InstanceBeginEditable name="EditRegion1" --><u><font face="Arial, Helvetica, sans-serif">AJOUT
              d'un dossier aux archives</font></u><!-- InstanceEndEditable --></font></p>
      </div>      
    <div align="center"></div>      <div align="right"></div>      <table width="97%" height="43%" align="center">
        <tr>
          <td width="80%" height="34"><div align="center"><!-- InstanceBeginEditable name="EditRegion2" -->
    <form name="form1" method="form1" action="<?php echo $editFormAction; ?>">
        <p align="left"><font size="2"> </font></p>
        </form>
    <form method="post" name="form2" action="<?php echo $editFormAction; ?>">
      <table width="530" align="center">
        <tr valign="baseline">
          <td width="0" align="right" nowrap><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">N&deg; d'affaire:</font></div></td>
          <td width="370"><font size="2" face="Arial, Helvetica, sans-serif">
            <input type="text" name="Numaff" value="" size="50">
</font></td>
          </tr>
        <tr valign="baseline">
          <td nowrap align="right"><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">Client:  </font></div></td>
          <td><font size="2" face="Arial, Helvetica, sans-serif">
            <input type="text" name="Client" value="" size="50">
          </font></td>
          </tr>
        <tr valign="baseline">
          <td nowrap align="right"><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">Nom de l'affaire:</font></div></td>
          <td><font size="2" face="Arial, Helvetica, sans-serif">
            <input type="text" name="Nomaff" value="" size="50">
          </font></td>
          </tr>
        <tr valign="baseline">
          <td align="right" nowrap><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">Responsable projet :</font></div></td>
          <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif">
             <input type="text" name="Resprojet" value="" size="50">
          </font></div></td>
        </tr>
        <tr valign="baseline">
          <td colspan="2" align="right" nowrap><div align="center"><font size="2" face="Arial, Helvetica, sans-serif">
              <input name="submit" type="submit" value="Valider l'ajout du dossier">
          </font></div></td>
        </tr>
      </table>
 	  <input type="hidden" name="MM_insert" value="form2">
    </form>
    <!-- InstanceEndEditable --></div>          </td>
        </tr>
      </table>    </td>
  </tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
<!-- InstanceEnd --></html>


L'erreur venait du faite que j'avais essayé en mettant dans ma BDD le champ Numaff en PRIMARY et j'ai essayé aussi en UNIQUE ... AUCUN N° Affaire de s'insérait.

Alors j'ai mis le champ en INDEX avec un autre champ ID pour en AUTO_INCREMENT ca me permettra de savoir a combien de dossier j'en suis rapidement ...ET LA NICKEL le numéro d'affaire D1111 ou autre s'insère ....

Merci de votre participation ...

Ca c'est une affaire qui roule !!!!!!!!!

par Ajoloca » 09 févr. 2007, 12:00

San voir ton code, je ne pas te dire d'avantage.

Poste la partie du code en question sans oublier de le mettre entre les balises
 et [ /php].

par Luverger » 09 févr. 2007, 11:48

De toute façon il ne devrait y avoir que moi qui puisse rajouter des dossiers aux archives donc même si les autres ne sont pas au courant que ce qu'il faut mettre ce n'est pas si grave que ça ...

Donc j'ai rajouter une colonne ID en AUTO_INCREMENT ...

Il ne me pose plus le problème de DUPLICATA de champ à la clef 1

Déjà c'est très bien ...

Par contre il ne m'ajoute tjs pas le numéro de l'affaire dans la case ....

Et je ne vois tjs pas d'ou ça peut venir ...

par Ajoloca » 09 févr. 2007, 11:43

Re,

Personne ne t'empêche d'utiliser deux colonnes pour en faire ta clé primaire.

Si tu as une combinaison de lettre plus numéro d'ordre je te conseille de générer cette clé par programme au lieu de laisser l'utilisateur la saisir.

Une méthode consiste à chercher le plus grand chiffre dont la clé commence par la lettre en question en utilisant MAX(champ) WHERE champ LIKE 'lettre%', extraire la lettre et ajouter 1 (un) à la valeur du chiffre obtenu.

par Luverger » 09 févr. 2007, 11:35

Alors :

J'ai essayé ton astuce ...

Ca marche mais comme ma première colonne était pour les N° d'affaires qui ne se suivent pas, ex : D1200, D1345, D1684 ...

Il m'a tout changé en 1, 2, 3 ...

Ce que je pourrais faire ce serait de rajouter une colonne ID ...

Comme ça il m'incrémenterait chaque entrée au fur et à mesure mais je pourrais garder mes N° d'affaires tels qu'ils doivent être ???

Qu'en penses-tu ??

En tout cas merci pour ton aide...

Je vais essayer ça de suite ...

par Ajoloca » 09 févr. 2007, 11:20

Bonjour,

Je crois que le message est clair.

Tu tentes d'insérer une information existante dans une colonne qui ne supporte pas les doublons.

Une façon simple d'éviter ce type de PB c'est de laisser MySQL gérer ce numéro en déclarant ta colonne de type AUTO_INCREMENT.

Je te propose cette solution car tu mentionnes (pour la cle 1).

Informations qui ne s'ajoutent pas à une base de données ...

par Luverger » 09 févr. 2007, 11:10

Bonjour,

J'ai créer une page PHP dans laquelle je veux pouvoir remplir 4 champs pour les injecter dans ma base de données qui compte 4 champs

(Numaff, Client, Nomaff, Resprojet)

Voici le bout de code de ma page:

Code : Tout sélectionner

<?php require_once('../Connexions/ConnexionArchives.php'); ?> <?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "varchar": case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Numaff"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "Client"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "Nomaff"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "Resprojet"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . $_SERVER['QUERY_STRING']; } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) { $insertSQL = sprintf("INSERT INTO Archives (Numaff, Client, Nomaff, Resprojet) VALUES (%s, %s, %s, %s)", GetSQLValueString($_POST['Numaff'], "text"), GetSQLValueString($_POST['Client'], "text"), GetSQLValueString($_POST['Nomaff'], "text"), GetSQLValueString($_POST['Resprojet'], "text")); mysql_select_db($database_ConnexionArchives, $ConnexionArchives); $Result1 = mysql_query($insertSQL, $ConnexionArchives) or die(mysql_error()); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO Archives (Numaff, Client, Nomaff, Resprojet) VALUES (%s, %s, %s, %s)", GetSQLValueString($_POST['Numaff'], "text"), GetSQLValueString($_POST['Client'], "text"), GetSQLValueString($_POST['Nomaff'], "text"), GetSQLValueString($_POST['Resprojet'], "text")); //!- DEBUT AJOUT echo $insertSQL,"<br/>\n"; //!- FIN AJOUT mysql_select_db($database_ConnexionArchives, $ConnexionArchives); $Result1 = mysql_query($insertSQL, $ConnexionArchives) or die(mysql_error()); if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } mysql_select_db($database_ConnexionArchives, $ConnexionArchives); $query_rsDossiers = "SELECT Numaff, Client, Nomaff, Resprojet FROM Archives ORDER BY Numaff"; $rsDossiers = mysql_query($query_rsDossiers, $ConnexionArchives) or die(mysql_error()); $row_rsDossiers = mysql_fetch_assoc($rsDossiers); $totalRows_rsDossiers = mysql_num_rows($rsDossiers); ?> <html><!-- InstanceBegin template="/Templates/PageArchives.dwt.php" codeOutsideHTMLIsLocked="false" --> <head> <!-- InstanceBeginEditable name="doctitle" --> <title>Pajout d'ajout de dossier aux archives</title> <!-- InstanceEndEditable --><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable --> <script language="JavaScript" type="text/JavaScript"> <!-- function MM_jumpMenu(targ,selObj,restore){ //v3.0 eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); if (restore) selObj.selectedIndex=0; } //--> </script> </head> <body bgcolor="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF"> <table width="803" height="256" border="0" align="center" cellspacing="0"> <tr align="right" valign="top"> <td height="100" colspan="3" bordercolor="0"><div align="center"> <p align="left"><strong><img src="../Images/logo.jpg" width="339" height="100"><img src="../Images/logo%20Archives.jpg" width="461" height="100"></strong></p> </div> </td> </tr> <tr valign="bottom"> <td height="18" colspan="2"><div align="left"><font color="0494bc" size="2" face="Arial, Helvetica, sans-serif">L.V.</font></div></td> <td height="18"><div align="right"><font color="0494bc" size="2" face="Arial, Helvetica, sans-serif">v.1.2007</font></div></td> </tr> <tr align="center" valign="middle" background="../Images/Degrade2.jpg"> <td width="230" height="35"> <a href="../index.php"><font size="2"><font color="#666666" face="Arial, Helvetica, sans-serif"><strong>RECHERCHE</strong></font></font></a> </td> <td width="335" height="35"><div align="center"> <div align="center"> </div> </td> <td width="238" height="35"><div align="center"> <select name="menuListes" onChange="MM_jumpMenu('parent',this,0)"> <option>-- LISTES DES DOSSIERS -- </option> <option value="../Listes/listenumaff.php">Par N&deg; affaire</option> <option value="../Listes/listeclient.php">Par nom du client</option> <option value="../Listes/listenomaff.php">Par titre de l'affaire</option> <option value="../Listes/listeresprojet.php">Par responsables de projets</option> </select> </div></td> </tr> <tr bgcolor="#F2F2F2"> <td height="58" colspan="3"><div align="center"><font size="6"><font face="Arial, Helvetica, sans-serif"></font></font> <p><font size="6"><!-- InstanceBeginEditable name="EditRegion1" --><u><font face="Arial, Helvetica, sans-serif">AJOUT d'un dossier aux archives</font></u><!-- InstanceEndEditable --></font></p> </div> <div align="center"></div> <div align="right"></div> <table width="97%" height="43%" align="center"> <tr> <td width="80%" height="34"><div align="center"><!-- InstanceBeginEditable name="EditRegion2" --> <form name="form1" method="form1" action="<?php echo $editFormAction; ?>"> <p align="left"><font size="2"> </font></p> </form> <form method="post" name="form2" action="<?php echo $editFormAction; ?>"> <table width="530" align="center"> <tr valign="baseline"> <td width="0" align="right" nowrap><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">N&deg; d'affaire:</font></div></td> <td width="370"><font size="2" face="Arial, Helvetica, sans-serif"> <input type="text" name="Numaff" value="" size="50"> </font></td> </tr> <tr valign="baseline"> <td nowrap align="right"><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">Client: </font></div></td> <td><font size="2" face="Arial, Helvetica, sans-serif"> <input type="text" name="Client" value="" size="50"> </font></td> </tr> <tr valign="baseline"> <td nowrap align="right"><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">Nom de l'affaire:</font></div></td> <td><font size="2" face="Arial, Helvetica, sans-serif"> <input type="text" name="Nomaff" value="" size="50"> </font></td> </tr> <tr valign="baseline"> <td align="right" nowrap><div align="right"><font size="2" face="Arial, Helvetica, sans-serif">Responsable projet :</font></div></td> <td><div align="left"><font size="2" face="Arial, Helvetica, sans-serif"> <input type="text" name="Resprojet" value="" size="50"> </font></div></td> </tr> <tr valign="baseline"> <td colspan="2" align="right" nowrap><div align="center"><font size="2" face="Arial, Helvetica, sans-serif"> <input name="submit" type="submit" value="Valider l'ajout du dossier"> </font></div></td> </tr> </table> <input type="hidden" name="Numaff" value=""> <input type="hidden" name="MM_insert" value="form2"> </form> <!-- InstanceEndEditable --></div> </td> </tr> </table> </td> </tr> </table> <p>&nbsp;</p> <p>&nbsp;</p> </body> <!-- InstanceEnd --></html>
En remplissant les champs dans l'ordre suivant :

N° affaire : D1111
Client: 1
Nom affaire: 2
Resp. projet: 3

Lorsque je valide j'obtiens ce genre de message:

Duplicata du champ '' pour la clef 1

Sachant que Numaff est définie en UNIQUE

Merci de votre aide