使用ajax和php更新数据库

时间:2018-02-03 17:32:28

标签: javascript php jquery ajax

我选中复选框时尝试将我的数据库tinyint更新为1,取消选中该复选框时尝试将其更新为0。我的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>

试图更新我的数据库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);
 ?>

1 个答案:

答案 0 :(得分: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);
 ?>

希望它有所帮助!