PHP update com for

Asked

Viewed 65 times

0

UPDATED

I need to update a table 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.php value file, the error appears:

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 concerns that?

  • How are you starting the variable $stmt? You are trying to access a method in a variable that is not an object.

2 answers

4

Missed the underline and the ; in the end:

$valor = $_POST['novo_valor'][$i];
  • Thanks :), now appears new error PHP Fatal error: Call to a Member Function prepare() on a non-object

  • His variable $stmt is receiving an instance before calling the procedure prepare()? Because the code you posted above doesn’t have that line.

0

It is necessary to confirm that you have the correct initialization of the object $stmt that has to come from the object representing the database connection:

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

Then the if becomes:

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

Because the prepare must be called on the object representing the link and returns the Prepared statement, which will then be used within the if.

  • i did as you indicated, but now no error appears, but it does not change anything in my database

  • Now I’m noticing that the consultation itself isn’t right because it has a ( the most in SET(

Browser other questions tagged

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