简体   繁体   English

在JavaScript中使用时,MySQL查询未使用.open请求更新数据库

[英]Mysql query not updating db with .open request while using in javascript

I am trying to update a row where I want a checkbox value to update a table field in mysql. 我试图更新一行,我想要一个复选框值来更新mysql中的表字段。 I dont want to use form as I dont want the page to refresh. 我不想使用表格,因为我不想刷新页面。 I have pasted the code below and this is the 3rd consecutive day that I am stuck here. 我粘贴了下面的代码,这是我连续第三天被困在这里。

 <script type="text/javascript">
        function chkit(uid, chk, sid) {
            chk = (chk==true ? "1" : "0");
            var url = "edit_sms_process.php?userid="+uid+"&chkYesNo="+chk+"&sid="+sid+"";
            if(window.XMLHttpRequest) {
                req = new XMLHttpRequest();
            } else if(window.ActiveXObject) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            }
            // Use get instead of post.
            req.open("GET", url, true);

            req.onreadystatechange = function () {
                if (req.readyState === 4) {
                    if (req.status === 200) {
                        console.log(req.responseText);

                    } else {
                        console.log("Error", req.statusText);
                    }
                }
            };

            req.send(null);
        }
    </script>

My php page: 我的php页面:

 <div class="form-group">

                                       <label class="control-label col-md-3 col-sm-3 col-xs-12"
                                               for="chk_<?php echo $row2['teacher_id']; ?>">SMS Send<span
                                                class="required">*</span>
                                        </label>

                                        <div class="slideThree">
                                            <input type="checkbox"
                                                   name="chk"
                                                   id="chk_<?php echo $row2['teacher_id']; ?>"
                                                   value="chk"
                                            onclick="chkit(<?php echo $row2['teacher_id']; ?>, this.checked, <?php echo $row2['teacher_schoolref_id'] ?>);"
                                                <?php echo ($row2['sms_send'] == 1) ? 'checked="checked"' : ''; ?> />
                                            <label for="chk_<?php echo $row2['teacher_id']; ?>"></label>

c C

Now the chrome console shows this 现在chrome控制台显示了这个

edit_sms.php?sms=61:151 XHR finished loading: GET " http://localhost/cloud_school/cloud_school/dashboard_files/communication/edit_sms_process.php?userid=61&chkYesNo=0&sid=10 ".chkit @ edit_sms.php?sms=61:151onclick @ edit_sms.php?sms=61:383 edit_sms.php?sms=61:143 10success edit_sms.php?sms = 61:151 XHR已完成加载:GET“ http://localhost/cloud_school/cloud_school/dashboard_files/communication/edit_sms_process.php?userid = 61&chkYesNo = 0&sid = 10 ” .chkit @ edit_sms.php?sms = 61:151onclick @ edit_sms.php?sms = 61:383 edit_sms.php?sms = 61:143 10成功

// Get the variables.
$userid = $_GET['userid'];
$chkYesNo = $_GET['chkYesNo'];
$schoolcid= $_GET['sid'];

$sql = "UPDATE teachers SET sms_send = $chkYesNo WHERE teacher_id = $userid AND     teacher_schoolref_code = $schoolcid";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

if($result==1) {
 echo $result;
 echo $chkYesNo;
 echo "success";
}

Eventhough console shows as success, the db is not getting updated. 尽管控制台显示为成功,但数据库没有更新。 I have included the db connect string in the php page and have not added here for the sake of simplicity. 我已经将db connect字符串包含在php页面中,并且为了简单起见在此处未添加。

Any help will be Greatly greatly appreciated!!! 任何帮助将不胜感激!!!

Okay with the help of dod29, I was able to resolve the issue. 在dod29的帮助下,我能够解决此问题。 I was using a wrong column name in the second instance changing which resolved the issue. 我在第二个实例更改中使用了错误的列名,从而解决了该问题。 Thank you dod29 谢谢dod29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM