Why don’t you update?

Asked

Viewed 146 times

2

inserir a descrição da imagem aquiSe buscar so um assim e rodar o foreach ele faz o update mas se tiver mais ele ja não faz mais...Ai na imagem ele não altera nenhum e não da nenhum erro If you search only one like this and run the foreach it does the update but if you have more it no longer does... But if there’s more of one like in the other image it doesn’t change and it doesn’t make a mistake what I do?

Why not update ? ... It not of error none at all does not update

//Prepra ligacao php mysql
$stm = $pdo->prepare('SELECT * FROM sol_camiseta 
                             WHERE cod_nome_aluno = :id' );
//Atribui o paramentro ao $_GET['id'] que é o id que esta na url e coloca ele no prepare acima
$stm->bindValue( ':id', $_GET['id'] );
//Executa o pdo
$stm->execute();

//Tranforma o consulta em matriz
$consultas = $stm->fetchAll( PDO::FETCH_ASSOC );
include 'views/listar.php'; 

<?php
error_reporting(E_ALL);
require 'conexao.php';

//Pega o id da url
if ( isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ){

if ( $_POST )  {
    //Ligação php mysql
    $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> 
  • Include a error_reporting(E_ALL); at the beginning of the script, some error should appear.

  • At first just after <php? ?

  • Yeah, at the beginning right after the <?php.

  • No error has appeared

  • It runs FOREACH but if it has two it no longer does the update

  • But is it connecting with the bank? You need to catch the error ... At this link you can see how to catch connection error with PDO.

  • Worse than it is not giving error in connection...

  • You saw the image I put there and I want that when I click on the save it changes the dates according to what I put... what you advise me?

  • What I don’t understand is the cod_student name... it is part of the table sol_t-shirts right? But it is the primary key of the table?

  • So man, I don’t know about PDO, I use mysqli, but try to put if (!$stm) {&#xA; echo "\nPDO::errorInfo():\n";&#xA; print_r($dbh->errorInfo());&#xA;} or $stm -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); to see if there are any errors... fountain

  • The primary key is the table ID

  • I’ll try to put that

  • place after $stm->execute();

  • had an error in the first example, try so: if (!$stm) { echo "\nPDO::errorInfo():\n"; print_r($pdo->errorInfo()); }

  • So comrade... the problem is there... you are making a looping that applies several Forms according to the amount of T-shirt per student id... you have to include the primary key in each form tbm so that when the person save the Pdo will know which record is... I think it is this

  • Check if the id to be changed exists and also see if the auto commit is turned on

  • So I changed the code yesterday... when I made it call by the id but then it only calls the first id and as if it included the first id, then all the lines idependente which save I click will be the first id of the foreach

Show 12 more comments

2 answers

2


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

PHP accepts to put the name of the fields as an array, so it is possible to separate for each row of the database, for example with the T-shirt line id, but then I think it needs a little more work.

Obs.:

  1. You are creating and executing a statement outside of if, then one inside the if with the same query from outside, then in the loop for, is throwing away the result of the statement from within the if when assigning to the variable that saved the result, each line of the statement output outside the if.
  2. I didn’t know this foreach construction, only knew with keys.
  3. The inserted code snippet has unopened delimiters, so the answer may be inaccurate.
  4. In the example below, the field sol_camiseta_id would be the table row id you want to update, I don’t know what the correct name is, but there should be a unique id field for each row. If no, replace your DBA then ask the new help to resolve.
  5. There being the field sol_camiseta_id, the field cod_nome_aluno would only be to ensure that a student cannot manipulate the data and change value to 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 of the arrays, if all goes well and the user doesn’t try to break your system, all fields will be arrays with the same size and correct contents.

...
$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();
}
...
  • I will try really thank you, I think it will solve, so my table has the id yesterday I managed to reformulate a little by changing the get by the id of the line but it did not work very well I will change the code of my question there you give a look

1

It worked fbiazi Vlw even I will put the final code that

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

Browser other questions tagged

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