簡體   English   中英

使用Ajax和php更新數據庫

[英]Updating database using ajax and php

我正在嘗試將我的數據庫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);
 ?>
<?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);
 ?>

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM