PHP update with for

0

UPDATED

I need to update a tables with input fields as below:

  <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']; ?>]"/>
     <input type = "hidden" name="linha[<?php echo $dado_produto['linha']; ?>]"/>
     </td>
   </tr>
   <?php } ?> 
   </tbody>
   </table>
   </div> 
   <input type="submit"/>
   </form>

When sending to the value.php file, the error is appearing:

  

PHP Fatal error: Call to a member function prepare () on a non-object

The code follows below:

if($stmt->prepare("UPDATE 'produto' SET ('valor'='?' WHERE 'codigo'='?' AND 'linha' = '?'")) {  

    $stmt->bind_param('sii', $valor, $cod, $linha);

    for($i=0;$i<count($_POST['novo_valor']);$i++){
        $valor = $POST['novo_valor'][$i];
        $cod = $_POST['cod'][$i];
        $linha = $_POST['linha'][$i];
        $stmt->execute();
    }


    $stmt->close();
}

line 35 is the

 if($stmt->prepare("UPDATE 'produto' SET ('valor'='?' WHERE 'codigo'='?' AND 'linha' = '?'")) {

The error refers to what?

    
asked by anonymous 04.07.2017 / 14:57

2 answers

4

underline and ; were missing at the end:

$valor = $_POST['novo_valor'][$i];
    
04.07.2017 / 15:00
0

You must confirm that you have the correct initialization of the $stmt object that must come from the object representing the database connection:

$mysqli = new mysqli($servidor, $utilizador, $password, $nomeBaseDados);

Then if becomes:

if($stmt = $mysqli->prepare("UPDATE 'produto' SET 'valor'='?' WHERE 'codigo'='?' AND 'linha' = '?'")) { 

Because prepare has to be called on the object that represents the binding and returns the prepared statement , which will then be used within if .

    
04.07.2017 / 16:17