Error: Column count does not match value count at row 1

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 mentioned in the title, how to solve?

    
asked by anonymous 15.11.2017 / 15:36

2 answers

0

Some of the variables in the query are coming with single quotation marks ' , causing more than one VALUE to become only 1, and thus the number of VALUES becomes smaller in relation to 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 the single quotation marks, resulting in the error quoted in the question ( Column count does not match value count at row 1):

SolutionandSuggestion:

Beforeenteringintothedatabase,makeareplaceonallvariablesbytakingthesinglequotationmarks.OnesuggestionwouldbetoreplacethesinglequotationmarkswiththeHTMLthatrepresentsanapostrophe:

$abc=str_replace("'","&rsquo;",$abc);
    
15.11.2017 / 17:06
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')";

To this below line, with all variables inside single quotation marks identifying that each field will only receive a value avoiding conflicts and also that the number of values is greater or less than the number of fields informed 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')";
    
15.11.2017 / 17:42