0

I'm trying to update my database tinyint to 1 when the checkbox is checked and to 0 when the checkbox is unchecked. My Javascript/Ajax code is:

<script>
function emailNextWithAddress(chk,address) {
    var nextEmail, inside_where;
    if(chk.checked === true){
        $.ajax({
            url: "marca_enviado.php",
            type: "get",
            data: address,
            success: function(){
                nextEmail = document.createElement('input');
                nextEmail.id = address;
                nextEmail.value = address;
                nextEmail.type = 'text';
                nextEmail.name = 'email[]';
                nextEmail.className = 'insemail';
                nextEmail.style.display = 'inline-block';
                inside_where = document.getElementById('addEmail');
                inside_where.appendChild(nextEmail);
            },
            error: function(){
                console.log("Erro!");
            }
        });
    } else {
        $.ajax({
            url: "desmarca_enviado.php",
            type: "get",
            data: address,
            success: function(){
                inside_where = document.getElementById(address);
                inside_where.parentNode.removeChild(inside_where);
            },
            error: function(){
                console.log("Erro!");
            }
        });
    }
    return false;
}
</script>

And the php (marca_enviado.php) code which tries to update my database tinyint is:

<?php
    include_once 'dbh.php';

    $address = $_GET['address'];

    $updateEnviado = "UPDATE escolas SET Enviado = 1 WHERE id= 'address' ";

    $result = mysqli_query($conn , $updateEnviado);
 ?>
8
  • What is the error you got when run this code??? Commented Feb 3, 2018 at 17:34
  • @LakmalAbesekara No error, it just doesn't update the database. Commented Feb 3, 2018 at 17:36
  • $updateEnviado = "UPDATE escolas SET Enviado = 1 WHERE id= 'address' " There are multiple things wrong here, you are always updating to 1 and you are always updating a record having id= 'address' which probably does not exist Commented Feb 3, 2018 at 17:38
  • @Herco I have one php file to update to 1 and one php file to update to 0. Doesn't the id= 'address' come from the Ajax data: address ? Commented Feb 3, 2018 at 17:43
  • in data: address, what is address? Commented Feb 3, 2018 at 17:44

1 Answer 1

1
<?php
    include_once 'dbh.php';

    $address = $_GET['address'];

    //-- escape the input data. But be sure to add some validations before this line.
    $address = mysqli_real_escape_string($conn, $address);    

    //-- use "$address" variable there, assuming that variable contains the id
    $updateEnviado = "UPDATE escolas SET Enviado = 1 WHERE id= '$address' ";

    $result = mysqli_query($conn , $updateEnviado);
 ?>

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.