get input variable data

1

Speak People! I have a system that registers products and creates what I need in the data field, however in a crucial part, after I create the fields I need it generates input with these fields! The problem is:

When I send via post the input ta as below

<input type="text" name="campo[]" class="campo_input" placeholder="<?php echo $campo; ?>" />

I need to send the value of this field and separate to insert in the bd, because it is a while!

<?php
include "conecta.php";
   mysql_set_charset('utf8');

$lista =$_POST['lista'];

$sku = implode(",", array_map(function ($item) {
 return sprintf('"%s"', $item);
}, $_POST['campo']));


$sqli = mysql_query ("INSERT INTO $lista VALUES ('', '".$sku."')")  or die(mysql_error());

if($sqli){
    echo $sku;
}
else {
    echo 'deu erro';
}

?>

It takes legal, but I can not separate to include in the bank, it gives the answer

Column count doesn't match value count at row 1

Just because it takes the answer and does not separate, throw everything in a single column

    
asked by anonymous 03.06.2018 / 20:21

1 answer

1
  

Column count does not match value count at row 1

     

=

     

The column count does not match the count of values in row 1

Verify that the number of colunas in the query matches the number of values passed in value

  

Everything will depend on the value of $lista

The query also has error '".$sku."' The correct one is simply $sku according to solutions below

  • Assuming that HTML is composed of 4 inputs as shown below

    <input type="text" name="campo[]" class.....

  • Assuming that the variable $lista is composed with the name of the table followed by the names of the columns in parentheses

    $lista = "nomeTabela(nomeColuna,nomeColuna1,nomeColuna2,nomeColuna3,nomeColuna4)";
    
  • Your query should be

    $sqli = mysql_query ("INSERT INTO $lista VALUES ('', $sku)")  or die(mysql_error());
    
  • If the table name is not in the variable $lista

    $sqli = mysql_query ("INSERT INTO nomeTabela $lista VALUES ('', $sku)")  or die(mysql_error());
    
  • If the first column is AUTO_INCREMENT

    $lista = "nomeTabela(nomeColuna1,nomeColuna2,nomeColuna3,nomeColuna4)";
    
    $sqli = mysql_query ("INSERT INTO $lista VALUES ($sku)")  or die(mysql_error());
    
  • It is possible to only pass the table name through the variable

    $lista = "nomeTabela"; 
    
    $sqli = mysql_query ("INSERT INTO $lista VALUES ($sku)")  or die(mysql_error());
    
  • 04.06.2018 / 01:31