0

I've done this function to insert a new product:

function insertProduct($product) {
$db = connect_db();
$columnsNames = $paramsNames = "";
foreach ($product as $key => $value) {
    $columnsNames .= "$key,";
    $paramsNames .= ":$key,";
}
$columnsNames = substr($columnsNames,0,strlen($columnsNames)-1);
$paramsNames = substr($paramsNames,0,strlen($paramsNames)-1);
$insertProductQuery = "INSERT INTO products ($columnsNames) VALUES ($paramsNames)";
$insertProduct = $db->prepare($insertProductQuery);
foreach ($product as $key => $value) {
    $insertProduct->bindParam(":$key", $value);
}
$insertProduct->execute();
$idProduct = $db->lastInsertId();
return $idProduct;
}

But, into "internal code" it insert the value of "ean code". Now I give you more details.
Into "bindParam foreach" I'v inserted:

    echo "$key | $value <br>";

And it echos:

  1. internalCode | Codice Interno
  2. eanCode | Codice EAN6

Into database it insert "Codice EAN6" into "internalCode" too... can you find what I can't see?
Thank you!

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
ProtoTyPus
  • 1,272
  • 3
  • 19
  • 40

0 Answers0