Insértion des lignes vides dans la base

kami_24 Messages postés 46 Statut Membre -  
pitxu Messages postés 708 Statut Membre -
Bonjour,


mon nouveau problème est le suivant:
j'ai une page qui contient un formulaire sous forme de tableau standard..l'utilisateur entre le nombre de lignes qu'il souhaite..et je voudrais que ces lignes s'insèrent dans ma base de donnée..mais le problème c que les lignes qu'il ne remplie pas s'insère vide dans ma base de donnée ce qui est dérangeant..
voici mon le premier code qui envoi les données du formulaire:


<table>
<tr align=center bgcolor="#EEEEEE">
<TD width=20%>ASSET NO</TD>
<TD width=20%>DESCRIPTION</TD>
<TD width=20%>serial number</TD>
<TD width=20%>code alpha</TD>
<TD width=20%>month & year of acquisition</TD>
<TD width=20%>original cost(local currency)</TD>
<TD width=20%>depreciation(local currency)</TD>
<TD width=20%>residual value(local currency)</TD>
<TD width=20%>residual value(invoicing currency)</TD>
</tr>


<tr align=center class=viewentryfont bgcolor="">

<TD> <input type="text" name="asset_no" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="description" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="serial_number" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="code_alpha" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="aquisition" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="original_cost" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="depreciation" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="local_curr" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="invoicing_curr" class="entryfont2" maxlength=20> </TD>
</TR>

<tr align=center class=viewentryfont bgcolor="">

<TD> <input type="text" name="asset_no1" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="description1" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="serial_number1" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="code_alpha1" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="aquisition1" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="original_cost1" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="depreciation1" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="local_curr1" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="invoicing_curr1" class="entryfont2" maxlength=20> </TD>
</TR>

<tr align=center class=viewentryfont bgcolor="">

<TD> <input type="text" name="asset_no2" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="description2" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="serial_number2" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="code_alpha2" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="aquisition2" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="original_cost2" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="depreciation2" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="local_curr2" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="invoicing_curr2" class="entryfont2" maxlength=20> </TD>
</TR>

<tr align=center class=viewentryfont bgcolor="">

<TD> <input type="text" name="asset_no3" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="description3" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="serial_number3" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="code_alpha3" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="aquisition3" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="original_cost3" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="depreciation3" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="local_curr3" class="entryfont2" maxlength=20></TD>
<TD> <input type="text" name="invoicing_curr3" class="entryfont2" maxlength=20> </TD>
</TR>
</table>


et la page qui traite le formulaire:

if(isset($_POST['asset_no']))
$asset_no=addslashes($_POST['asset_no']);
else $asset_no="";
if(isset($_POST['description']))
$description=addslashes($_POST['description']);
else $description="";
if(isset($_POST['serial_number']))
$serial_number=addslashes($_POST['serial_number']);
else $serial_number="";
if(isset($_POST['code_alpha']))
$code_alpha=addslashes($_POST['code_alpha']);
else $code_alpha="";
if(isset($_POST['aquisition']))
$aquisition=addslashes($_POST['aquisition']);
else $aquisition="";
if(isset($_POST['original_cost']))
$original_cost=addslashes($_POST['original_cost']);
else $original_cost="";
if(isset($_POST['depreciation']))
$depreciation=addslashes($_POST['depreciation']);
else $depreciation="";
if(isset($_POST['local_curr']))
$local_curr=addslashes($_POST['local_curr']);
else $local_curr="";
if(isset($_POST['invoicing_curr']))
$invoicing_curr=addslashes($_POST['invoicing_curr']);
else $invoicing_curr="";
//
if(isset($_POST['asset_no1']))
$asset_no1=addslashes($_POST['asset_no1']);
else $asset_no1="";
if(isset($_POST['description1']))
$description1=addslashes($_POST['description1']);
else $description1="";
if(isset($_POST['serial_number1']))
$serial_number1=addslashes($_POST['serial_number1']);
else $serial_number1="";
if(isset($_POST['code_alpha1']))
$code_alpha1=addslashes($_POST['code_alpha1']);
else $code_alpha1="";
if(isset($_POST['aquisition1']))
$aquisition1=addslashes($_POST['aquisition1']);
else $aquisition1="";
if(isset($_POST['original_cost1']))
$original_cost1=addslashes($_POST['original_cost1']);
else $original_cost1="";
if(isset($_POST['depreciation1']))
$depreciation1=addslashes($_POST['depreciation1']);
else $depreciation1="";
if(isset($_POST['local_curr1']))
$local_curr1=addslashes($_POST['local_curr1']);
else $local_curr1="";
if(isset($_POST['invoicing_curr1']))
$invoicing_curr1=addslashes($_POST['invoicing_curr1']);
else $invoicing_curr1="";
//
if(isset($_POST['asset_no2']))
$asset_no2=addslashes($_POST['asset_no2']);
else $asset_no2="";
if(isset($_POST['description2']))
$description2=addslashes($_POST['description2']);
else $description2="";
if(isset($_POST['serial_number2']))
$serial_number2=addslashes($_POST['serial_number2']);
else $serial_number2="";
if(isset($_POST['code_alpha2']))
$code_alpha2=addslashes($_POST['code_alpha2']);
else $code_alpha2="";
if(isset($_POST['aquisition2']))
$aquisition2=addslashes($_POST['aquisition2']);
else $aquisition2="";
if(isset($_POST['original_cost2']))
$original_cost2=addslashes($_POST['original_cost2']);
else $original_cost2="";
if(isset($_POST['depreciation2']))
$depreciation2=addslashes($_POST['depreciation2']);
else $depreciation2="";
if(isset($_POST['local_curr2']))
$local_curr2=addslashes($_POST['local_curr2']);
else $local_curr2="";
if(isset($_POST['invoicing_curr2']))
$invoicing_curr2=addslashes($_POST['invoicing_curr2']);
else $invoicing_curr2="";
//
if(isset($_POST['asset_no3']))
$asset_no3=addslashes($_POST['asset_no3']);
else $asset_no3="";
if(isset($_POST['description3']))
$description3=addslashes($_POST['description3']);
else $description3="";
if(isset($_POST['serial_number3']))
$serial_number3=addslashes($_POST['serial_number3']);
else $serial_number3="";
if(isset($_POST['code_alpha3']))
$code_alpha3=addslashes($_POST['code_alpha3']);
else $code_alpha3="";
if(isset($_POST['aquisition3']))
$aquisition3=addslashes($_POST['aquisition3']);
else $aquisition3="";
if(isset($_POST['original_cost3']))
$original_cost3=addslashes($_POST['original_cost3']);
else $original_cost3="";
if(isset($_POST['depreciation3']))
$depreciation3=addslashes($_POST['depreciation3']);
else $depreciation3="";
if(isset($_POST['local_curr3']))
$local_curr3=addslashes($_POST['local_curr3']);
else $local_curr3="";
if(isset($_POST['invoicing_curr3']))
$invoicing_curr3=addslashes($_POST['invoicing_curr3']);
else $invoicing_curr3="";

mysql_connect('localhost','root','')or die('Erreur de connexion '.mysql_error());
mysql_select_db('mabase') or die('Erreur de selection '.mysql_error());




$rq = "INSERT INTO tableau(id,ref_no, asset_no, description, serial_number, code_alpha, aquisition, original_cost, depreciation, local_curr, invoicing_curr) VALUES('','$ref_no','$asset_no','$description', '$serial_number', '$code_alpha',
'$aquisition', '$original_cost', '$depreciation', '$local_curr', '$invoicing_curr'),
('','$ref_no', '$asset_no1','$description1', '$serial_number1', '$code_alpha', '$aquisition1', '$original_cost1', '$depreciation1', '$local_curr1', '$invoicing_curr1'),
('','$ref_no','$asset_no2','$description2', '$serial_number2', '$code_alpha2', '$aquisition2', '$original_cost2', '$depreciation2', '$local_curr2', '$invoicing_curr2'),
('','$ref_no','$asset_no3','$description3', '$serial_number3', '$code_alpha3', '$aquisition3', '$original_cost3', '$depreciation3', '$local_curr3', '$invoicing_curr3')";

$r= mysql_query($rq) or die('Erreur SQL !'.$rq.'<br>'.mysql_error());


donc j'aimerai savoir comment faire pour que seule les lignes remplie soient inséré dans ma base...
A voir également:

5 réponses

creadiff Messages postés 483 Statut Membre 57
 
Argh.
Code overflow.

C'est un peu illisible, là, il faudrait que tu soies plus synthétique (pense à ceux qui vont te relire).

Petite astuce de débogage : en phase de développement, essaie de placer l'instruction echo mysql_error(); après chaque appel de mysql_query pour mieux identifier les requêtes qui posent problème.
0
kami_24 Messages postés 46 Statut Membre
 
merci creadiff pour ton conseil mais même si je fai mysql error ça donne pas d'erreur..tout mon problème est d'empêcher l'insertion des lignes vide dans ma base
0
creadiff Messages postés 483 Statut Membre 57
 
Commence par rendre ton code plus lisible... moins on en écrit, plus on y voit clair, et plus le script est facile à modifier !



Pour la génération du formulaire :

<table><thead>
    <tr>
        <th>Asset number</th>
        <th>Description</th>
        <th>Serial number</th>
    </tr>
</thead><tbody><?php
    $champs = array("asset_no", "description", "serial_number");
    for ($i=0; $i<4; $i++) {
        echo "<tr>";
        foreach ($champs as $champ) {
            echo '<td>';
            echo '<input type="text" name="';
            echo $champ.$i;
            echo '" maxlength="20" />';
            echo '</td>';
        }
        echo "</tr>";
?></tbody></table>
0
creadiff Messages postés 483 Statut Membre 57
 
Puis, pour le traitement des données :

<?php  
    mysql_connect('localhost','root','');   
    mysql_select_db('mabase');  

    $champs = array("asset_no", "description", "serial_number");  

    for ($i=0; $i<4; $i++) {  
        $lignevide = true;  
        $donnees = array(); 
        foreach ($champs as $champ) { 
            if (isset($_POST[$champ.$i])){ 
                $donnees[] = mysql_real_escape_string($_POST[$champ.$i]); 
                $lignevide = $lignevide ? : ($_POST[$champ.$i] != ""); 
            } 
        }  
        if (!$lignevide) {  
            $SQL = "INSERT INTO tableau("; 
            $SQL.= implode(",", $champs); 
            $SQL.= ") VALUES ('"; 
            $SQL.= implode("','", $donnees); 
            $SQL = "')"; 
            mysql_query($SQL); 
        }  
    }  
?>  
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
pitxu Messages postés 708 Statut Membre 95
 
if(isset($_POST['asset_no1']))
$asset_no1=addslashes($_POST['asset_no1']); 


=

$asset_no1 = isset($_POST['asset_no1']) ? addslashes($_POST['asset_no1']) : "";
0