antiblock
diamwall
  • Chatbox

    You don't have permission to chat.
    Load More
Sign in to follow this  
.FaBio

Erro MySql

2 posts in this topic

Bom dia, executo o seguinte código que corre por trás de um script para atualizar as informações da página sem refresh na mesma.

Spoiler

<?php
$c = $_REQUEST['a'];
require("C:/xampp/htdocs/connect.php");
$query = "SELECT cd_p, nome, morada FROM participantes ORDER BY cd_p";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
    <script src="part_ati/js/script.js"></script>
</head>
<body>
<div class="container">
    <div class="table-responsive">
        <div id="employee_table">
            <input type="hidden" value="<?php echo $c; ?>">
            <div id="partTable" style=" overflow: auto">
                <table id="tabela" class="table table-striped table-bordered" cellspacing="0" width="100%">
                    <thead>
                    <tr>
                        <th width="5%">#</th>
                        <th width="50%">Nome</th>
                        <th width="35%">Morada</th>
                        <th width="10%">Estado</th>
                    </tr>
                    </thead>
                    <?php
                    while ($row = mysqli_fetch_array($result)) {
                        $p = $row['cd_p'];
                        $query2 = "SELECT participantes.cd_p, participantes_atividades.cd_ati FROM participantes_atividades, participantes WHERE participantes.cd_p = participantes_atividades.cd_p AND participantes_atividades.cd_ati = $c AND participantes_atividades.cd_p = $p ORDER BY cd_p";
                        $result2 = mysqli_query($connect, $query2) or die ("Erro");
                        ?>
                        <tr>
                            <td><?php echo $row["cd_p"]; ?></td>
                            <td><?php echo $row["nome"]; ?></td>
                            <td><?php echo $row["morada"]; ?></td>
                            <td><?php
                                if ($row2 = mysqli_fetch_array($result2))
                                    echo '<a data-id=', $row['cd_p'], ' name="bRemover" class="btn btn-danger btn-xs bRemover-data">Remover</a>';
                                else echo '<a data-id=', $row['cd_p'], ' name="bAdicionar" class="btn btn-primary btn-xs bAdicionar-data">Adicionar</a>';
                                ?></td>
                        </tr>
                        <?php
                    }
                    ?>
                </table>
            </div>
        </div>
    </div>
</div>
</body>
</html>

 

 com a execução do script:

Spoiler

$(document).ready(function() {
    $(document).on('click', '.bAdicionar-data', function(){
        var id = $(this).attr('data-id');
        var ati = document.getElementById("ati");
        $.ajax({
            url:"part_ati/js/ins.php",
            method:"POST",
            data:{p:id,a:ati.value},
            success: function (data) {
                $('#partTable').html(data);
            }
        });
    });
    $(document).on('click', '.bRemover-data', function(){
        var id = $(this).attr('data-id');
        var ati = document.getElementById("ati");
        $.ajax({
            url:"part_ati/js/del.php",
            method:"POST",
            data:{p:id,a:ati.value},
            success: function (data) {
                $('#partTable').html(data);
            }
        });
    });
});

 

ele chama este ficheiro:

Spoiler

<?php
//session_start();
$connect = mysqli_connect("localhost", "root", "", "Junta");

$p = $_REQUEST['p'];
$cd = $_REQUEST['a'];

$sql = "INSERT INTO participantes_atividades (cd_p,cd_ati) VALUES ('$p','$cd')";
if (mysqli_query($connect, $sql) or die ("Erro ligação à base de dados")) {
    $output = '';
    $select_query = "SELECT cd_p, nome, morada FROM participantes ORDER BY cd_p";
    $result = mysqli_query($connect, $select_query) or die("ERRO: Inserção na Tabela participantes_atividades");
    $output .= '
        <table id="tabela" class="table table-striped table-bordered" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th width="5%">#</th>
                    <th width="50%">Nome</th>
                    <th width="35%">Morada</th>
                    <th width="10%">Estado</th>
                </tr>
            </thead>
    ';
    while ($row = mysqli_fetch_array($result)) {
        $p = $row['cd_p'];
        $query2 = "SELECT participantes.cd_p, participantes_atividades.cd_ati FROM participantes_atividades, participantes WHERE participantes.cd_p = participantes_atividades.cd_p AND participantes_atividades.cd_ati = $cd AND participantes_atividades.cd_p = $p ORDER BY cd_p";
        $result2 = mysqli_query($connect, $query2) or die ("ERRO: Ligação à  Tabela participantes_atividades");

        $output .= '
        <tr>
            <td>' . $row["cd_p"] . '</td>
            <td>' . $row["nome"] . '</td>
            <td>' . $row["morada"] . '</td>
            <td>';
        if ($row2 = mysqli_fetch_array($result2)) {
            $output .= '<a data-id=' . $row['cd_p'] . ' name="bRemover" class="btn btn-danger btn-xs bRemover-data">Remover</a>';
        } else {
            $output .= '<a data-id=' . $row["cd_p"] . '  name="bAdicionar" class="btn btn-primary btn-xs bAdicionar-data">Adicionar</a>';
        }
        $output .= '
            </td>
        </tr>
        ';
    }
    $output .= '</table>';
    echo $output;
}

 

O resultado é este:

Spoiler

1ac486878fa548eead24f6fba32665cc.png

Quando da erro aparece isto: "Duplicate entry '1-2' for key 'PRIMARY'" mesmo adicionando e removendo o mesmo participante varias vezes so acontece às vezes.

 

Alguma sugestão? A minha DB é a seguinte:

Spoiler

43780a2470cf436985571dcb8cfa6784.png

Tabela Participantes:

Spoiler

c082bbca6b66460a912d9cc0c2a71da5.png

Tabela Atividades:

Spoiler

114a2d0cfd1d4d3da05493146c78ff5d.png

Tabela Participantes_Aividades:

Spoiler

fe827a8d601147e9a9aa12354adaab88.png

Share this post


Link to post
Share on other sites
antiblock
https://arwen2.global/

Problema resolvido

razao > duplo registo do evento click sobre o elemento, e consequente dupla chamada do ajax para remover e adicionar o registo

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this