AJAX更新数据库 [英] AJAX update database

查看:77
本文介绍了AJAX更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我的问题还没有说出来.实际上,我想更新数据库中的数据. 但是现在的问题是,即使我尝试选择批准还是拒绝,ajax仍然不会更新. 我是ajax的新手,请尝试在网上搜索,但是我的代码仍然有问题

Sorry for not stated my problem. Actually I want to update my data in database. But the problem now is even i tried to choose approve or reject the ajax still won't update. I am new in ajax and try search around net but my code still got problem

这是我的php页面

<?php
          $querysel = "SELECT * FROM tblinternapplication WHERE course_code = '{$course_codeapp}' ORDER BY student_id, 1 DESC " ;
      $resultsel = mysql_query($querysel, $connection);

      echo "<h2><div class=\"h_title\">Status still in pending</div></h2>";  
      echo "<table>";
      echo "<thead>";
      echo "<tr>";
      echo "<th scope=\"col\">Matric ID</th>";
      echo "<th scope=\"col\">Company name</th>";
      echo "<th scope=\"col\" width = \"200\">Job Scope</th>";
      echo "<th scope=\"col\">Status</th>";
      echo "<th scope=\"col\">Action</th>";
      echo "</tr>";
      echo "</thead>";

          while($rowsel = mysql_fetch_array($resultsel)){
                    if($rowsel['status_approval'] == NULL){
            $id = $rowsel['id'];
                    echo "<tr>";
                    echo "<tr>"."<td class=\"align-center\">".$rowsel['student_id']."</td>";
                    echo "<td class=\"align-center\">".$rowsel['company_name']."</td>";
                    echo "<td class=\"align-center\" width = \"200\">".$rowsel['job_scope']."</td>";
        echo "<td class=\"align-center\">";
                    if($rowsel['status_approval'] != NULL){
                        if( $rowsel['status_approval'] == 0)
            {
               echo "Reject";
            }
            else
            {
               echo "Approve";
            }
                        }
                    else
                    {   echo "Pending";
                        }
                    echo "</td>";
        echo "<td class=\"align-center\"><select name=\"approve\" 
         onchange=\"getstatus(this.value)\">";
            echo "<option value=\"\">Select status:</option>";
            echo "<option value=\"1\">Approve</option>";
            echo "<option value=\"0\">Reject</option>";
        echo "</select>";
        echo "</td>";
                    echo "</tr>";

                    }
      }
      echo "</table>";

这是我的jscript页面

here is my jscript page

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript"> 
function getstatus(id, approve)
{
if (approve=="")
{
} 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("GET","updatestatus.php?id=" + id + "&status=" + approve,true);
xmlhttp.send();
}

</script>

然后这是我的updatestatus.php

then here is my updatestatus.php

<?php require_once("../includes/session.php"); ?>
<?php require_once("sessioncourse.php"); ?>
<?php $course_codeapp = $_SESSION['course_code'] ; ?>
<?php confirm_logged_in(); ?>
<?php require_once("../includes/connection.php") ?>
<?php require_once("../includes/functions.php") ?>

<?php

$id = $_GET['id'];
$status =$_GET['status'];

$sql="UPDATE tblinternapplication set status_approval  = $status WHERE id = $id ";

$result = mysql_query($sql);
?>

我工作了几天,但问题仍然无法解决.希望可以有人帮帮我.感谢您的帮助!

I work for few days but problem still cannot solve. Hope someone can help me. I will appreciate your help!

推荐答案

echo "<td class=\"align-center\"><select name=\"approve\" 
         onchange=\"getstatus(this.value)\">";

在上面的那一行中,您仅传递一个值.同时,您的JS函数等待2个参数.当前,id始终等于0或1,并且状态始终是未定义的. 假设您只需要更改该行,例如:

In that line above, you are passing only one value. At the same time your JS function waits for 2 parameters. Currently, you have id always equal to 0 or 1 and status is always undefined. Suppose you just need to change that line like:

echo "<td class=\"align-center\"><select name=\"approve\" 
         onchange=\"getstatus("+ $id +", this.value)\">";

一个注释:

$sql="UPDATE tblinternapplication set status_approval  = $status WHERE id = $id ";

除了不再使用mysql_ *函数外,上面的代码已对sql注入开放.为了避免使用mysql_扩展名进行SQL注入,您应该执行以下操作:

Except that you should not use mysql_* functions anymore, code above is opened to sql injections. To avoid sql injections with mysql_ extension you should do something like this:

 $sql="UPDATE tblinternapplication set status_approval  = ".mysql_real_escape_string($status) ." WHERE id = " .mysql_real_escape_string($id);

mysql_real_escape_string 此处中查看文档.另外,请阅读该页面上的警告消息-它告诉您在替换mysql扩展名时应该使用什么

See docs for mysql_real_escape_string here. Also, read a warning message on that page - it tells you what you should use in replacement for mysql extension

这篇关于AJAX更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆