简体   繁体   English

使用Ajax和php更新数据库

[英]Updating database using ajax and php

I'm trying to update my database tinyint to 1 when the checkbox is checked and to 0 when the checkbox is unchecked. 我正在尝试将我的数据库tinyint更新为1(选中该复选框时),将其更新为0(如果取消选中该复选框)。 My Javascript/Ajax code is: 我的Javascript / Ajax代码是:

<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: 试图更新我的数据库tinyint的php(marca_enviado.php)代码是:

<?php
    include_once 'dbh.php';

    $address = $_GET['address'];

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

    $result = mysqli_query($conn , $updateEnviado);
 ?>
<?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! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM