How to update database in form with Select input?

Asked

Viewed 91 times

0

in the file cadFilhote.php I have the function:

function atualizarFilhote(str){
    var id_filhote = str;
    var filhote = $('#filhote-'+str).val();
    var raca = $('#raca-'+str).val();
    var opcao = $('#opcao-'+str).val();
    var descricao = $('#descricao-'+str).val();
    var preco = $('#preco-'+str).val();
    var peso = $('#peso-'+str).val();
    var nascimento = $('#nascimento-'+str).val();
    var mae = $('#mae-'+str).val();
    var pai = $('#pai-'+str).val();

    $.ajax({
        type: "POST",
        url: "filhote.php?p=editar",
        data: "filhote="+filhote+"&raca="+raca+"&opcao="+opcao+"&descricao="+descricao+"&preco="+preco+"&peso="+peso+"&nascimento="+nascimento+"&mae="+mae+"&pai="+pai+"&id_filhote="+id_filhote,
        success: function(data){
            verDados();
        }
    });

}

in the PUP.php file I have:

else if($page=='editar'){
$id_filhote = $_POST['id_filhote'];
$filhote = $_POST['filhote'];
$raca = $_POST['raca'];
$opcao = $_POST['opcao'];
$descricao = $_POST['descricao'];
$preco = $_POST['preco'];
$peso = $_POST['peso'];
$nascimento = $_POST['nascimento'];
$mae = $_POST['mae'];
$pai = $_POST['pai'];
$stmt = $db->prepare("UPDATE filhote SET filhote=?, raca=?, opcao=?, descricao=?, preco=?, peso=?, nascimento=?, mae=?, pai=? WHERE id_filhote=?");
$stmt->bindParam(1,$filhote);
$stmt->bindParam(2,$raca);
$stmt->bindParam(3,$opcao);
$stmt->bindParam(4,$descricao);
$stmt->bindParam(5,$preco);
$stmt->bindParam(6,$peso);
$stmt->bindParam(7,$nascimento);
$stmt->bindParam(8,$mae);
$stmt->bindParam(9,$pai);
$stmt->bindParam(10,$id_filhote);
if($stmt->execute()){
    echo "Filhote atualizado com sucesso!";
}else{
    echo "Falha ao atualizar filhote";
}

}

and in that same file I have the form to update the data:

<div class="modal fade" id="editModal-<?php echo $row['id_filhote'] ?>" tabindex="-1" role="dialog" aria-labelledby="editLabel-<?php echo $row['id_filhote'] ?>">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="editLabel-<?php echo $row['id_filhote']?>">Atualizar Filhote</h4>
                        </div>
                        <form>
                            <div class="modal-body">
                                <input type="hidden" id="<?php echo $row['id_filhote']?>" value="<?php echo $row['id_filhote'] ?>">

                                <div class="form-group">
                                    <label for="filhote">Filhote</label>
                                    <input type="text" class="form-control" id="filhote-<?php echo $row['id_filhote']?>" value="<?php echo $row['filhote'] ?>">
                                </div>

                                <div class="form-group">
                                    <label for="raca">Raça</label>
                                    <select class="form-control" id="raca-<?php echo $row['id_filhote']?>">
                                        <?php
                                        $stmt = $db->prepare("SELECT * FROM categoria ORDER BY categoria ASC");
                                        $stmt->execute();
                                        while($row = $stmt->fetch()){
                                            ?>
                                            <option value="<?php echo $row['categoria']; ?>">
                                                <?php echo $row['categoria']; ?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="opcao">Opção</label>
                                    <select class="form-control" id="opcao-<?php echo $row['id_filhote']?>">
                                        <option value="Disponivel">Disponivel</option>
                                        <option value="Reserva">Reserva</option>
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label for="descricao">Descrição</label>
                                    <textarea class="form-control" rows="3" id="descricao-<?php echo $row['id_filhote']?>"><?= $row['descricao']?></textarea>
                                </div>
                                <div class="form-group">
                                    <label for="preco">Preço</label>
                                    <input type="text" class="form-control" id="preco-<?php echo $row['id_filhote']?>" value="<?= $row['preco']?>">
                                </div>
                                <div class="form-group">
                                    <label for="peso">Peso</label>
                                    <input type="text" class="form-control" id="peso-<?php echo $row['id_filhote']?>" value="<?= $row['peso']?>">
                                </div>
                                <div class="form-group">
                                    <label for="nascimento">Data de Nascimento</label>
                                    <input type="date" class="form-control" id="nascimento-<?php echo $row['id_filhote']?>" value="<?= $row['nascimento']?>">
                                </div>




                                <div class="form-group">
                                    <label for="mae">Matriz</label>
                                    <select class="form-control" id="mae-<?php echo $row['id_filhote']?>">
                                        <?php
                                        $stmt = $db->prepare("SELECT * FROM matriz ORDER BY matriz ASC");
                                        $stmt->execute();
                                        while($row = $stmt->fetch()){
                                            ?>
                                            <option value="<?php echo $row['matriz']; ?>">
                                                <?php echo $row['matriz']; ?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                </div>



                                <div class="form-group">
                                    <label for="pai">Reprodutor</label>
                                    <select class="form-control" id="pai-<?php echo $row['id_filhote']?>">
                                        <?php
                                        $stmt = $db->prepare("SELECT * FROM reprodutor ORDER BY reprodutor ASC");
                                        $stmt->execute();
                                        while($row = $stmt->fetch()){
                                            ?>
                                            <option value="<?php echo $row['reprodutor']; ?>">
                                                <?php echo $row['reprodutor']; ?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                </div>


                            </div>
                            <div class="modal-footer">

                                <button type="button" class="btn btn-default" data-dismiss="modal">FECHAR</button>
                                <button type="submit" onclick="atualizarFilhote(<?php echo $row['id_filhote'] ?>)" class="btn btn-primary">Atualizar</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

What happens is that if I take the input Select fields and the textarea I can change the data, but with these fields it is not working. what can be?

  • That’s right: <?= $row['descricao']?>? Would not be <?php $row['descricao']; ?>? It has several parts of your code written like this.

  • 1

    Yes, that’s right, when you put = replace echo, I solved the problem, in the loop of the select field I traded Row for row2 and it worked

No answers

Browser other questions tagged

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