首页 > 解决方案 > 使用php将ajax数据发送到mysql

问题描述

我实现了以下 javascript 函数来收集数据并显示它们,但我需要将这些数据保存到 mysql 数据库中以跟踪数据,我尝试使用下面的 php 代码在本地连接到 mysql 但没有用,我认为问题是我不知道如何收集数据并将它们插入到查询中

这是javascript代码:

function Script2()
{
  var player = GetPlayer();    
$.ajaxSetup({ cache: false });   
 $.ajax({
url: "data.php",
           type: "GET",
           processData: true,
           data: {"entry.939953095" :player.GetVar("username"),
"entry.391274481" :player.GetVar("Slide1"),
"entry.822772676" :player.GetVar("slide2"), 
"entry.934389578" :player.GetVar("slide3"),
"entry.1669272782" :player.GetVar("slide4"),
"entry.1486049396" :player.GetVar("Slide5"), 
"entry.264581670" :player.GetVar("slide6"),

          dataType: "jsonp",
           success: function (data) {
               processData(data);
           }
});

用于连接数据库并存储数据的 php 文件

<?php

if(isset($_REQUEST))
{
$connect_error='sorry we\'re experincing connection error';
@mysql_connect('localhost','root','') or die($connect_error);
mysql_select_db('lo-test') or die($connect_error);

error_reporting(E_ALL && ~E_NOTICE);

$data=$_GET['player.GetVar("Slide1")'];
$sql="INSERT INTO lo-test(text2) VALUES ('$data')";
$result=mysql_query($sql);

}
?>

对我的代码以及如何使用 Get 或 Post 处理变量的任何建议

标签: javascriptphpmysqlajax

解决方案


你会得到这样的数据

$data = $_GET['entry.939953095'];

或者您也可以尝试转储$_GET变量并查看它拥有的所有键


推荐阅读