doukan4795 2016-01-27 14:23
浏览 20
已采纳

新值不会在db上更新

I want to get data from database and change it, all with ajax but when I try to change the input ajax send the same data that was on database.

    <script>
    function showMore(str) {
      var xhttp;
      if (str.length == 0) { 
        document.getElementById("txtMore").innerHTML = "";
        return;
      }
      xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
          document.getElementById("txtMore").innerHTML = xhttp.responseText;
        }
      };
      xhttp.open("GET", "getmore.php?q="+str, true);
      xhttp.send();   
    }
    </script>
    <body>

    <div class="content">
        <button id="edit">Update</button>
        <form action=""> 
            <h3>Last Name:</h3><input type="text" id="txt1" onkeyup="showHint(this.value)">
        </form>
            <span id="txtMore"></span>
    </div>


    </body>
<script type="text/javascript">

$('#edit').click(function(){

    var ID = $('#id_field').attr('value');
    var name_field = $('#FirstName').attr('value');
    var last_field = $('#LastName').attr('value');
    var telefone_field = $('#Telefone').attr('value');
    var email_field = $('#Email').attr('value');
    var check = $('#CheckIn').attr('value');

    var dataString = 'id=' +ID+ '&FirstName=' +name_field+ '&LastName=' +last_field+ '&Telefone=' +telefone_field+ '&Email=' +email_field+ '&CheckIn=' +check;
    alert(dataString);


    $.ajax({
        type: "GET",
        url: "edit_ajax.php",
        data: dataString,
        cache: false,
        success: function(html)
        {
        $("#txtHint").html('Actualizado');
        }
    });
});

</script>

And the php file where I get the data from database

<?php


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$con = mysqli_connect('localhost','root','root','client_db');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

$q = mysqli_real_escape_String($con, $_GET['q']);

mysqli_select_db($con,"client_db");
$sql='SELECT * FROM user WHERE id = "'.$q.'" ';
$result = mysqli_query($con,$sql);


while($row = mysqli_fetch_array($result)) {


    echo'<form method="GET" class="form_user">';
    echo'<input type="hidden" name="id" id="id_field" value="'.$row['id'].'" class="inputForm"><br><br>';
    echo'Primeiro Nome<br>';
    echo'<input type="text" name="FirstName" id="FirstName" value="'.$row['FirstName'].'" class="inputForm"><br><br>';
    echo'Ultimo Nome<br>';
    echo'<input type="text" name="LastName" id="LastName" value="'.$row['LastName'].'" class="inputForm"><br><br>';
    echo'Telefone<br>';
    echo'<input type="text" name="Telefone" id="Telefone" value="'.$row['Telefone'].'" class="inputForm"><br><br>';
    echo'Email<br>';
    echo'<input type="text" name="Email" id="Email" value="'.$row['Email'].'" class="inputForm"><br><br>';
    echo'<input type="checkbox" name="Check" id="CheckIn" value="Check">Check-in<br><br>';
    echo'</form>';
}
mysqli_close($con);
?>

Thanks.

EDIT: The problem isn't the sql query but the values inside <input> if I change that values javascript read the old values and send them to php.

  • 写回答

1条回答 默认 最新

  • dongshang1768 2016-01-27 14:36
    关注

    You're missing an UPDATE request on your DB. Right now your request only do a SELECT, so no matter what data are sent to your server, you're not using it.

    $sql='SELECT * FROM user WHERE id = "'.$q.'" ';
    $result = mysqli_query($con,$sql);
    

    You could to something like this :

    <?php
    
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    
    $con = mysqli_connect('localhost','root','root','client_db');
    if (!$con) {
       die('Could not connect: ' . mysqli_error($con));
    }
    
    $q = mysqli_real_escape_String($con, $_GET['q']);
    
    
    $Username = $GET['Firstname'];
    $Lastname= $_GET['Lastname'] 
    // Do so for every inputs your form will give you
    
    $sql = "UPDATE 'user' SET 'Firstname' = '$Firstname','Lastname' = '$Lastname', etc...";
    
    
    $result = mysqli_query($con,$sql);
    
    
    mysqli_close($con);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度