Delete empty fields in PHP array

0

With php and html I am making a page that contains a form, through which a database is consulted in Firebird.

I have two models:

  • The first , works correctly, since it only does search queries and updates ONLY one record each time:

    //Por un lado tengo el formulario en html (pongo sólo el botón y la cabecera):
    
    form name="formulario" id="formulario" method="post"
    
    input type="submit" name="btn1" value="Actualizar" 
    
    //Y por otro lado (en la misma página) tengo el .php
    
    if (isset($_POST["btn1"])){
     $btn=$_POST["btn1"];
    
        if ($btn=="Actualizar"){
        $sqlStr= ' ';
    
         //Saco un nuevo array con los campos vacíos del formulario eliminados.
          if (!empty($POST_){
          unset ($_POST['btn1']);
          for each ($_POST as $key => $value) {
                       if (trim($value) != '') {
                         $sqlArr[]="$key='$value'";
    }}}
    
         //convierto array a string
        $sqlStr=implode(", ", $sqlArr);
    
       $sql = "update tabla set $sqlStr";
    

In short, doing this I update the registry without problems, because even if the fields are empty, the last string remains as I want (ID = '1', NAME = '2' ...).

  • The second method is to do a search and print more than one record. Each resulting row will have an "update" button next to it, which will update that row. The code I have is the following:

list.php:

//en php:

if (isset($_POST["btn1"])){
     $btn=$_POST["btn1"];

        if ($btn=="Buscar"){

   $sql="consulta sql"

<form name='ejecuta' action='ejecuta2.php' method='post'>

<tr>
<td>ID_AGENTE</td>
<td>ID_COLABORADOR</td>
<td>ID_PERSONA</td>
</tr>

$i=0;

    while ($resul=ibase_fetch_row($cs)){
   $var=$resul[0];
   $var1='resul[1];
   $var2=$resul[2];

echo "<tr>
<td>
<input name='ID_AGENTE[$i]' value='".$var."' /></td>
<input name='ID_ COLABORADOR[$i]' value='".$var1."' /></td>
<input name='ID_PERSONA[$i]' value='".$var2."' /></td>
<td><input type="submit" name='seleccion[$i]' value='Actualizar'></td>
</tr>";
$i++;
}
echo "</table></form>;

This works relatively well for me, since it shows the form well and when it comes to doing the search it shows all the records on the screen.

The problem comes when using the submit with the name select [$ i] and value Update .

I tried to base myself on the php code of the first model, doing this:

run2.php:

<?php

foreach ($_POST['seleccion'] as $indice => $valor){
//verificamos que se ha dado al boton actualizar.
$opcion=substr ($_POST['seleccion'][$indice],0,-1); //extraemos la parte de la cadena que será "Actualizar".

switch ($opcion) {

     case: "Actualizar":

          if (!empty($_POST)){

               if (trim($valor) != ''){
              $sqlArr[]="$indice='$valor'";}}

print_r $sqlArr;

     sql="$sql = "update tabla set $sqlStr";
    break;

?>

Surely there is something wrong when I pass my code to this, but in conclusion, what I do not know is how to eliminate empty fields in the array according to the form I have in the second model so that at the end I have a string of the type : ID = '1', NAME = 'JOSE'.

I have tried to put it in several ways and always always, when doing the procedure of deletion with the array, I always get: 0 = 'Update'.

    
asked by user34776 30.03.2017 в 11:57
source

1 answer

0

In the first script you give form to the update statement making

foreach($_POST as $key => $value) {
   if (trim($value) != '') {
      $sqlArr[]="$key='$value'";
   }
}

That is a fairly artisan way of putting together an SQL query, and very prone to SQL injections , but I do not want to expand on that topic because it is beyond the scope of the question.

In your last script you are putting

foreach ($_POST['seleccion'] as $indice => $valor){
    ....
    if (!empty($_POST)){
       if (trim($valor) != ''){ // <-- pero si ya sabes que $valor es "Actualizar"
         $sqlArr[]="$indice='$valor'";
       }
    }
    ....
}

It turns out that $_POST['seleccion'] is a numeric array that contains one or more values 'Actualizar' and only that you are trying to arm your update statement.

I think that, on the one hand, you have to modify the second script to go through the content of $_POST instead of $_POST['seleccion'] and, on the other hand, build your HTML in another way, because at this moment you have only one Form for N submits, but they all do the same, send the full table.

    
answered by 30.03.2017 в 13:05