Inserting data into postgresSQL database with PHO, AJAX and JQUERY

Asked

Viewed 165 times

0

I am trying to insert data into a postgres database using php and ajax with jquery. But always error. It is for a college job. Could someone explain to me the error in the code?

$("#registrar").click(function(){
        $.ajax ({
            url : "registra.php",
            method: "POST",
            dataType : "json",
            data : {participante1 : $("#participante1 option:selected").val(),
                    participante2 : $("#participante2 option:selected").val(),
                    score1 : $("#score1").val(),
                    score2 : $("#score2").val()},

            success : function(resp){
                alert("Registro efetuado com sucesso!");
            },

            error : function(err){
                alert("ERRO: " + err.status);
            }
        });
    });

<?php 
    $con = pg_connect("host=localhost port=5432 dbname=Jogos user=postgres password=postgres");
    $participante1 = $_POST['participante1'];
    $participante2 = $_POST['participante2'];
    $score1 = $_POST['score1'];
    $score2 = $_POST['score2'];
    $comando = "INSERT INTO disputas (participante1, participante2, score1, score2) VALUES ('$participante1', '$participante2', '$score1', '$score2')"
    pg_query($con, $comando);
    pg_close($con);
?>
  • What’s the mistake that comes?

  • Do so and see which error appears. pg_query($con, $comando); or die(pg_last_error($con));

  • Same error appears. "ERROR : 200"

2 answers

0

Try it like this:

INSERT INTO disputas (participante1, participante2, score1, score2) VALUES (".$participante1.", ".$participante2.", ".$score1.", ".$score2.")

  • Also doesn’t work.

  • Install a Pgadmin on your machine connect with the database to run this query on the console changing the variables to real values and see if the error repeats!

0


Thanks for the help person, was missing the blessed ";" at the end of this stretch

$comando = "INSERT INTO disputas (participante1, participante2, score1, score2) VALUES ('$participante1', '$participante2', '$score1', '$score2')"

As for the 200 error I was presenting, it is because PHP was not returning a valid JSON.

Browser other questions tagged

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