Ajax函数未运行

时间:2014-02-24 15:39:58

标签: php jquery mysql sql ajax

所以我试图在我的数据库中插入一行。我正在调用一个类似ajax的函数来向我的表中插入一个新行。但它没有插入一行。

function showResult(first, last)
    {

    var First = first;
    var Last = last;

       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("POST","http://www.website.ca/portal/MyChapter2/cgi-bin/DetermineUser.php?FirstName="+First+"&LastName="+Last,true);
       xmlhttp.send();    

  }

,这是它所到达的文件,以便将行插入表中。

<?php
require_once (dirname(__FILE__) . '/../../include/Initialization.php');
require_once (PORTAL_PATH . '/include/FormLibrary.php');
require_once (PORTAL_PATH . '/include/SingleRowQuery.php');
require_once (PORTAL_PATH . '/include/Functions.php');
require_once (PORTAL_PATH . '/include/VolunteerInterests.php');
require_once (PORTAL_PATH . '/TaskManager/cgi-bin/AutoTaskFunctions.php');

$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];

$sql="INSERT INTO `Track_Notification`(`Track_ID`, `Track_UserID`) VALUES    ('$FirstName','$LastName')";
echo ("success");

?>

2 个答案:

答案 0 :(得分:3)

您正在进行POST,但不通过该POST发送任何数据。您正在URL中发送数据,这实际上是一种GET技术:

xmlhttp.open([..snip...] /DetermineUser.php?FirstName="+First+"&LastName="+Last,true);
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

无论你使用什么HTTP动词,如果URL中有查询参数,它们都在$ _GET中,所以

$_GET['FirstName'];
$_GET['Lastname'];'

除此之外,你很容易受到SQL注入攻击,所以喜欢你的服务器pwn3d。

答案 1 :(得分:0)

您没有运行查询

添加

$result = mysqli_query($sql);

(或mysql_query,根据您的使用情况而定)