Error: Column Count doesn’t match value Count at Row 1

Asked

Viewed 957 times

1

include "config_db.php";

$sql = "INSERT INTO tb_produto (NOME_PROD,DESC_PROD,VALOR_PROD,VL_COMP_PROD,STATUS_PROD,COD_CAT,TAM_PROD,COR_PROD,COD_BARRA_PROD,ID_FORN,MAT_FUNC, QTD_PROD,FT_PRODUTO)VALUES('$nome',$desc,$val,$vld,'$status',$cat,'$tam','$cor',$cbr,$forn,$func,$qtd,'$ft')";
$sel = mysqli_query($con,$sql) or die (mysqli_error($con)); 
$caminho_imagem = "prod/ft/".$ft;
if(move_uploaded_file($ftmp, $caminho_imagem)){
    echo"
    <html>
    <style>
    @font-face
    {
        font-family:horatiodbol;
        src: url('horatiodbol.ttf') format('truetype');
    }
    </style>
    <body>
    <font size='+3' face='horatiodbol' color='#FFFFFF'>Produto Cadastrado</font>
    <br>
    <a href='prod.php' color='#FFFFFF' style='font-family:horatiodbol; text-decoration:none;' >Voltar</a>";
}

This code shows the error quoted in the title, how to solve?

  • Some of the variables are coming with single quotes?

  • $sql = "INSERT INTO tb_produto (NOME_PROD,DESC_PROD,VALOR_PROD,VL_COMP_PROD,STATUS_PROD,COD_CAT,TAM_PROD,COR_PROD,COD_BARRA_PROD,ID_FORN,MAT_FUNC, QTD_PROD,FT_PRODUTO)VALUES($nome,$desc,$val,$vld,$status,$cat,$tam,$cor,$cbr,$forn,$func,$qtd,$ft)";

  • without the simple quotes.. test there..

  • I had already tried but without success this method, but I’ve solved the problem, Thank you

2 answers

0


Some of the variables in the query are coming with simple quotes ', causing more than one VALUE to become only 1, and thus the number of VALUES becomes less than the number of columns in the query. For example:

If I have this query:

"insert into tabela (coluna1,coluna2)values($abc,$def)"

And the values of the variables are:

$abc = "'foo";
$def = "bla'"

The query would look like this:

"insert into tabela (coluna1,coluna2)values('foo,bla')"

Note that VALUES now has only 1 parameter, delimited between single quotes, resulting in the error quoted in the question (Column Count doesn’t match value Count at Row 1):

inserir a descrição da imagem aqui

Solution and Suggestion:

Before inserting into the bank, make a replace all variables apart from single quotes. A suggestion would be to replace single quotes with HTML code that represents an apostrophe:

$abc = str_replace("'","&rsquo;",$abc);

0

In this case of your INSERT the ideal serial change this line

$sql = "INSERT INTO tb_produto (NOME_PROD,DESC_PROD,VALOR_PROD,VL_COMP_PROD,STATUS_PROD,COD_CAT,TAM_PROD,COR_PROD,COD_BARRA_PROD,ID_FORN,MAT_FUNC, QTD_PROD,FT_PRODUTO)VALUES('$nome',$desc,$val,$vld,'$status',$cat,'$tam','$cor',$cbr,$forn,$func,$qtd,'$ft')";

For this below line, with all variables within single quotes identifying that each field will only receive one value avoiding conflicts and also that the number of values is greater or less than the number of fields reported in the query

$sql = "INSERT INTO tb_produto (NOME_PROD, DESC_PROD, VALOR_PROD, VL_COMP_PROD, STATUS_PROD, COD_CAT, TAM_PROD, COR_PROD, COD_BARRA_PROD, ID_FORN, MAT_FUNC, QTD_PROD, FT_PRODUTO) VALUES ('$nome', '$desc', '$val', '$vld', '$status', '$cat', '$tam', '$cor', '$cbr', '$forn', '$func', '$qtd', '$ft')";
  • I used your method and also str_replace to replace "," with "." when entering cash values (decimal) in the database, thank you very much

  • If this answer met you, mark as the best spouse for other users to understand this and not use the time to solve a question that has already been answered

Browser other questions tagged

You are not signed in. Login or sign up in order to post.