Why does not Update?

2

Whydonotyouupdate?...Noerrornonesodonotupdate

//Prepraligacaophpmysql$stm=$pdo->prepare('SELECT*FROMsol_camisetaWHEREcod_nome_aluno=:id');//Atribuioparamentroao$_GET['id']queéoidqueestanaurlecolocaelenoprepareacima$stm->bindValue(':id',$_GET['id']);//Executaopdo$stm->execute();//Tranformaoconsultaemmatriz$consultas=$stm->fetchAll(PDO::FETCH_ASSOC);include'views/listar.php';<?phperror_reporting(E_ALL);require'conexao.php';//Pegaoiddaurlif(isset($_GET['id'])&&!empty($_GET['id'])){if($_POST){//Ligaçãophpmysql$stm=$pdo->prepare("UPDATE sol_camiseta 
                           SET data_pagamento = :data_pagamento, 
                           data_retirada = :data_retirada
                           WHERE id = :id");

    //Atribui o paramentro ao $_POST['e a referecia onde ele esta'] e coloca ele no prepare acima

    $stm->bindValue( ':data_pagamento', $_POST['data_pagamento'] );
    $stm->bindValue( ':data_retirada', $_POST['data_retirada'] );
    $stm->bindValue( ':id', $_GET['id'] );

    //Executa o pdo
    $stm->execute();

    //depois de executar o header o rediciona para outro local
    header( "Refresh:5, index.php" );
}
}



    <fieldset>
    <legend>Camiseta</legend>
        <table>
            <?php foreach ( $consultas as $consulta ) : ?>
            <form action='pagarcamiseta.php?id=<?php echo $consulta['id']; ?>'  method='post'>
                <tr>
                    <td>Tipo:</td>
                    <td><?php echo $consulta['tipo']; ?></td>
                    <td>Cor:</td>
                    <td><?php echo $consulta['cor']; ?></td>
                    <td>Tamanho</td>
                    <td><?php echo $consulta['tamanho']; ?></td>
                    <td>Quantidade</td>
                    <td><?php echo $consulta['qtd']; ?></td>
                    <td>Valor</td>
                    <td><?php echo $consulta['valor']; ?></td>
                </tr>
                <tr>
                    <td>Data Pedido:</td>
                    <td><?php echo $consulta['data_pedido']; ?></td>
                </tr>
                <tr>
                    <td>Data Pagamento:</td>
                    <td><input type='date' name='data_pagamento' value='<?php if ( isset( $_POST['data_pagamento'] ) ) echo $_POST['data_pagamento']; else echo $consulta['data_pagamento']; ?>'></td>
                    <td>Data Retirada:</td>
                    <td><input type='date' name='data_retirada' value='<?php if ( isset( $_POST['data_retirada'] ) ) echo $_POST['data_retirada']; else echo $consulta['data_retirada']; ?>'></td>
                    <td><input type="submit" value="Salvar" /></td>
                </tr>
            <?php endforeach; ?>
            <td><a href='index.php'>Voltar</a></td>
        </table>
    </fieldset>
</form> 
    
asked by anonymous 13.10.2015 / 01:40

2 answers

2

If there are several shirts for the student, there will be a lot of fields with the same name, for example 'date_payment', there is one for each shirt.

PHP accepts to put the field names as an array, so it's possible to separate for each row in the database, for example with the id of the line of the shirt, but then I think it needs a little extra work. p>

Note:

  • You are creating and executing a statement out of if , then one within if with the same query as out, then in loop for , is throwing out the statement result from within if when you assign to the variable that saved the result, each line of the statement result out of if .
  • I did not know this build of the foreach, only known with keys.
  • The inserted code snippet has unclosed delimiters, so the response may be inaccurate.
  • In the example below, the sol_camiseta_id field would be the id of the table row you want to update, I do not know what the correct name is, but there should be a unique id field for each row. If there is not, replace your DBA then ask the new one to help.
  • If the field sol_camiseta_id , the cod_nome_aluno field would only be to ensure that a student can not manipulate the data and change value for other students' t-shirts.
  • For example:

    ...
    $i = 0;
    foreach
      ...
                            <td>Data Pagamento:<input type="hiden" name="cId[<?php echo $i; ?>]" value="<?php echo $sol_camiseta_id; ?>"></td>
                            <td><input type='date' name='data_pagamento[<?php echo $i; ?>]' value='<?php if ( isset( $_POST['data_pagamento'] ) ) echo $_POST['data_pagamento']; else echo $consulta['data_pagamento']; ?>'></td>
                            <td>Data Retirada:</td>
                            <td><input type='date' name='data_retirada[<?php echo $i; ?>]' value='<?php if ( isset( $_POST['data_retirada'] ) ) echo $_POST['data_retirada']; else echo $consulta['data_retirada']; ?>'></td>
                            <td><input type="submit" value="Salvar" /></td>
      $i++;
      ...
    endforeach;
    ...
    

    Each field you receive will be an array, so you must execute the statement for each data set in the arrays, if all goes well and the user does not try to break your system, all fields will be arrays of the same size and correct indexes.

    ...
    $stm = $pdo->prepare( "UPDATE sol_camiseta 
                           SET data_pagamento = :data_pagamento, 
                           data_retirada = :data_retirada
                           WHERE cod_nome_aluno = :cod_nome_aluno
                             AND sol_camiseta_id = :sol_camiseta_id");
    
    $stm->bindValue( ':data_pagamento', $data_pag );
    $stm->bindValue( ':data_retirada', $data_ret );
    $stm->bindValue( ':cod_nome_aluno', $cod_aluno );
    $stm->bindValue( ':sol_camiseta_id', $sol_cam_id);
    
    $cod_aluno = $_GET['id']; // este campo é sempre igual
    foreach ($_POST['cId'] as $i => $sol_cam_id) { // campo id definido aqui
      $data_pag = $_POST['data_pagamento'][$i]; // campo data pagamento
      $data_ret = $_POST['data_retirada'][$i]; // campo retirada
      $stm->execute();
    }
    ...
    
        
    13.10.2015 / 08:14
    1

    It's been a while, I'm going to put the final code on

    <?php
    error_reporting(E_ALL);
    require 'conexao.php';
    
    //Pega o id da url
    if ( isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ){
    
    if ( $_POST )  {
        $stm = $pdo->prepare( "UPDATE sol_camiseta 
                               SET data_pagamento = :data_pagamento, 
                               data_retirada = :data_retirada
                               WHERE cod_nome_aluno = :cod_nome_aluno
                               AND id = :id");
    
        foreach ($_POST['id'] as $i => $id) { // campo id definido aqui
        $data_pagamento = $_POST['data_pagamento'][$i]; // campo data pagamento
        $data_retirada = $_POST['data_retirada'][$i]; // campo retirada
    
        $stm->bindValue( ':data_pagamento', $data_pagamento );
        $stm->bindValue( ':data_retirada', $data_retirada );
        $stm->bindValue( ':cod_nome_aluno', $_GET['id'] );
        $stm->bindValue( ':id', $id);
    
    
        $stm->execute();
        }
    
    <fieldset>
        <legend>Camiseta</legend>
            <table>
                <?php foreach ( $consultas as $consulta ) : ?>
                <form action='pagarcamiseta.php?id=<?php echo $consulta['cod_nome_aluno']; ?>'  method='post'>
                    <tr>
                        <td>Tipo:</td>
                        <td><?php echo $consulta['tipo']; ?></td>
                        <td>Cor:</td>
                        <td><?php echo $consulta['cor']; ?></td>
                        <td>Tamanho</td>
                        <td><?php echo $consulta['tamanho']; ?></td>
                        <td>Quantidade</td>
                        <td><?php echo $consulta['qtd']; ?></td>
                        <td>Valor</td>
                        <td><?php echo $consulta['valor']; ?></td>
                    </tr>
                    <tr>
                        <td>Data Pedido:</td>
                        <td><?php echo $consulta['data_pedido']; ?></td>
                    </tr>
                    <tr>    
                        <td>Data Pagamento:<input type="hidden" name="id[<?php echo $consulta['id']; ?>]" value="<?php echo $consulta['id']; ?>"></td>
                        <td><input type='date' name='data_pagamento[<?php echo $consulta['id']; ?>]' value='<?php if ( isset( $_POST['data_pagamento'] ) ) echo $_POST['data_pagamento']; else echo $consulta['data_pagamento']; ?>'></td>
                        <td>Data Retirada:</td>
                        <td><input type='date' name='data_retirada[<?php echo $consulta['id']; ?>]' value='<?php if ( isset( $_POST['data_retirada'] ) ) echo $_POST['data_retirada']; else echo $consulta['data_retirada']; ?>'></td>
                        <td><input type="submit" value="Salvar" /></td>
                    </tr>
                <?php endforeach; ?>
                <td><a href='index.php'>Voltar</a></td>
            </table>
        </fieldset>
    </form> 
    
        
    13.10.2015 / 19:02