警报正在显示,但数据未更新,并且无法单击警报的“确定”按钮

时间:2018-11-30 08:55:36

标签: javascript php jquery ajax

“我的警报”显示已成功更新,但数据库中的数据未更新,并且无法单击警报的“确定”按钮。这是我的upresult.php的php代码。希望这会有所帮助。预先谢谢你

我的jQuery

 $(document).ready(function(){

        $("#form1").submit(function(event){
            event.preventDefault();
            var formData = new FormData(this);
              $.ajax({

                 url:"upresult.php",
                 type:"POST",
                 data:formData,
                 async:true,
                 success:function(data) {
                   alert(data);

                },
                cache:false,
                contentType:false,
                processData:false
            });
         });
    });

upresult.php

<?php
    include("connection.php");

    $no=trim($_POST['upno']);
    $name=trim($_POST['upname']);
    $mob=trim($_POST['upmob_no']);
    $dob=trim($_POST['updob']);
    $add=trim($_POST['upadd']);
    $photo=trim($_FILES['upphoto']['name']);
    $gen=trim($_POST['gender']);
    $cn=trim($_POST['upcountry']);
    $st=trim($_POST['upstate']);
    $ct=trim($_POST['upcity']);

    $qry="update stud set stud_name='".$name."',mobile='".$mob."',dob='".$dob."',address='".$add."',gender='".$gen."',country='".$cn."',state='".$st."',city='".$ct."' where stud_no='".$no."'";
    $data=mysqli_query($conn,$qry);
    if($data)
    {
        echo '<script language="javascript">';
        echo 'alert("Updated Successfully")';
        echo '</script>';
    }
    else {
        echo '<script language="javascript">';
        echo 'alert("Cannot update record")';
        echo '</script>';
    }
?>

1 个答案:

答案 0 :(得分:1)

您要提醒警报。尝试像这样编辑流控制结构:

<?php

    include("connection.php");

    // you need to validate this data before sending it to update query 
    $no=trim($_POST['upno']);
    $name=trim($_POST['upname']);
    $mob=trim($_POST['upmob_no']);
    $dob=trim($_POST['updob']);
    $add=trim($_POST['upadd']);
    $photo=trim($_FILES['upphoto']['name']);
    $gen=trim($_POST['gender']);
    $cn=trim($_POST['upcountry']);
    $st=trim($_POST['upstate']);
    $ct=trim($_POST['upcity']);

    // this parameters should be binded to avoid SQL injection
    $query = "
        update stud 
        set
            stud_name = '$name',
            mobile = '$mob', 
            dob = '$dob', 
            address = '$add',
            gender = '$gen',
            country = '$cn',
            state = '$st',
            city = '$ct'
        where stud_no = '$no';
    ";

    /** This may be query for checking.
    *   Just execute it after first query and grab response from it.
    *   Depends of response you will return appropirate text message.
    */
    $checkUpdateQuery = "
        select if(count(*) = 1, true, false) as response
        from stud
        where stud_name = '$name',
        and mobile = '$mob', 
        and dob = '$dob', 
        and address = '$add',
        and gender = '$gen',
        and country = '$cn',
        and state = '$st',
        and city = '$ct'
        and stud_no = '$no'; 
    ";

    /** mysqli_query will return false only if some error occurred. 
    *   In other cases you will get true, 
    *   so you need to check if data is updated by another query.
    */
    $data = mysqli_query($conn, $query);

    echo $data ? 'Updated Successfully' : 'Cannot update record';

您应该考虑的几件事是数据库中是否有stud_no,失败时mysqli_query返回FALSE。对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,mysqli_query()将返回mysqli_result对象。对于其他成功的查询,mysqli_query()将返回TRUE。

如果您愿意,我们可以更改此查询。可以使用PDO代替mysqli吗?