2014-02-24 59 views
0

所以我试图插入一行到我的数据库。我打电话给一个类似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"); 

?> 
+0

它实际上做了什么? – Archer

+0

为什么使用jQuery进行标记? –

+0

**'Ajax Function Not Running' ** ...它在网络浏览器中失败了吗?由于语法错误,它没有运行吗?你的PHP文件是否精美和华丽?请按照以下步骤正确调试AJAX问题,然后您可以正确地指责哪些代码被破坏:http://stackoverflow.com/a/21617685/2191572 – MonkeyZeus

回答

0

您没有运行查询

添加

$result = mysqli_query($sql); 

(或mysql_query,根据你使用的是什么)

3

你正在做一个职位,但不发送通过该POST的任何数据。你的URL,这实际上是一个GET技术发送数据:

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

不要紧,HTTP动词使用,如果在URL中的查询参数,它们会在$ _GET,所以

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

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

+0

ops yea,我改变了这一点,让这里发布错误的代码。我如何防止SQL注入攻击? – CFleming