Problem in return of PHP function [closed]

0

Good afternoon, guys I'm starting in php and I have a problem with my code

  function buscarIdProduto($conn,$nome){
    $stmt = $conn->prepare("SELECT id FROM produto WHERE NOME_Produto = ?");
    $stmt->bind_param("i", $nome);
    $stmt->execute();
    $resultado = $stmt->get_result();
    return $resultado->fetch_assoc();
  }

In this function I get the product id, the problem is that it always returns the id 1 then when I insert it reference to the product with id 1.

if(inserirProduto($conn,$idCategoria,$nomeProduto,$descricao,$valor)){
      $produto = buscarIdProduto($conn,$nomeProduto);
}
inserirImagem($conn,$novoNome,$produto["id"],$upload,$tipo,$tamanho);
    
asked by anonymous 27.04.2017 / 18:52

1 answer

2
$stmt->bind_param("i", $nome);

This line is incorrect. You are passing $nome as an integer, but since this variable has an alphanumeric string in it, its function can not transform letters into numbers.

The correct one would be to $stmt->bind_param("s", $nome); or $stmt->bind_value("s", $nome); . The difference between the two is that bind_param associates the variable with the query, modifying the query when the value of the variable is modified and bind_value associates the value of the variable with the query, keeping the query the same even if the variable is modified .

    
29.04.2017 / 01:22