PHP Error " mysqli_query() expects at least 2 Parameters, 1 Given", when sending multiple input information

Asked

Viewed 689 times

-1

I am making a page with registration of product values, I would like to know how I do a Insert Into of the values of a while?

<table style="width: 100%;">
 <thead>
  <tr>
   <th>Item</th>
   <th>Código</th>
   <th>Produto</th>
   <th>Valor</th>
  </tr>
 </thead>
<tbody>
<?php while($dado_produto = $result_produtos->fetch_array()){ ?>
 <tr>
  <td>1</td>
  <td><?php echo $dado_produto['cod']; ?></td>
  <td><?php echo $dado_produto['descricao']; ?></td>
  <td><input type = "text" name="valor[<?php echo $dado_produto['cod']; ?>]"/></td>
 </tr>
<?php } ?> 
 </tbody>
</table>
</div> 
<input type="submit"/>
</form>    

The file that receives the php value.

I searched I used a code I found on the internet to, but I’m not getting, this giving error.

<?php
header('Content-Type: text/html; charset=utf-8');
include_once("../../controle/conexao.php");
// Início da consulta
$sql = "INSERT INTO `produtos` (`cod`, `valor`) VALUES";

// Para cada elemento produto:
foreach($_POST['valor'] as $cod=>$val){
$produto = $cod['codigo'];
$valor = $val['valores'];

// Monta a parte consulta de cada produto
$sql .= " (NULL, '{$produto}', '{$valor}'),";}

// Tira o último caractere (vírgula extra)
$sql = substr($sql, 0, -1);

// Executa a consulta
mysqli_query($sql);
$cadastrados = mysqli_affected_rows();
?>

Error appears:

PHP Parse error: syntax error, Unexpected T_VARIABLE in E: home value.php on line 10

Line 10 is the :

$sql .= " (NULL, '{$produto', '{$valor}'),";}

Can someone help me what I’m doing wrong?

  • 1

    Are we missing a comma but not a } ? $sql .= " (NULL, '{$product}', '{$value}')";}

  • I put } right after product and removed the comma, but still presents the same error, I returned the comma and also presents the same error.

  • 1

    Looks like missing a semicolon: $valor = $val['valores']

  • put, and now appears the error PHP Warning: mysqli_query() expects at least 2 Parameters, 1 Given in E: home value.php on line 11 PHP Warning: mysqli_affected_rows() expects Exactly 1 Parameter, 0 Given in E: home value.php on line 12

  • The field you are setting NULL will not be set to "not null"? Try putting "1" instead of NULL

  • When you use the functions mysqli the first argument is almost always the connection, the second the query in the case of the myqli_query(). Look at you mysqli_query($sql);

  • @rray really the errors that André Baill and more what you pointed out, was one of the problems, deleted $product = $Cod['code']; $value = $val['values']; is now working correctly. Thanks for the help

Show 2 more comments

2 answers

2

If you are using PDO.

The best thing to do is to prepare your SQL, even to ensure some SQL Injection project.

$sql = 'INSERT INTO produtos VALUES (:cod,:valor)'

$sth = $conn->prepare($sql);
$stmt = $sth->execute(array(':cod' => 150, ':valor' => 10));
  • I’m not using PDO

  • What are you using? Mysqli? Something like that? $Conn = new mysqli($servername, $username, $password, $dbname);

  • Correct mysqli.

  • I posted another answer.

2

Using mysqli, would look like this.

<?php
// Criando a conexão
$conn = new mysqli($server, $user, $password, $database);

// Verificando a conexão
if ($conn->connect_error) {
    die("Falhou a Conexão: " . $conn->connect_error);
}

// preparando o bind
$stmt = $conn->prepare("INSERT INTO (tabela) (nome, sobrenome, email) VALUES (?, ?, ?)");
//sss são variáveis tipo String. Consulte aqui: http://php.net/manual/en/mysqli-stmt.bind-param.php
$stmt->bind_param("sss", $nome, $sobrenome, $email);

//executando
$stmt->execute();

//Você pode executar várias vezes, em um for, por exemplo.

$firstname = "Maria";
$lastname = "Da Silna";
$email = "[email protected]";
$stmt->execute();

$firstname = "Julia";
$lastname = "dos Santos";
$email = "[email protected]";
$stmt->execute();


$stmt->close();
$conn->close();
?>
  • 1

    I didn’t understand a thing

  • Now I’ve formatted the answer.

Browser other questions tagged

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