新值不会在db上更新

时间:2016-01-27 14:23:39

标签: javascript php jquery ajax

我想从数据库中获取数据并进行更改,所有这些都使用ajax,但是当我尝试更改输入时,ajax会发送数据库中的相同数据。

    <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>

我从数据库中获取数据的php文件

<?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);
?>

感谢。

编辑:问题不是sql查询,而是<input>内的值,如果我更改了这些值javascript读取旧值并将它们发送到php。

1 个答案:

答案 0 :(得分:0)

您在数据库上遗漏了ctx.beginPath(); // x y radius startAngle, endAngle ctx.arc(95, 50 + 100*id, 40, 0, Math.PI*2); ctx.fill(); ctx.stroke(); 个请求。 现在您的请求只需UPDATE,因此无论发送到您的服务器的数据是什么,您都不会使用它。

SELECT

你可以这样:

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