使用jQ ajax和php返回为空的数据库表值


Database table values returning as null using jQ ajax and php

我试图传递javascript变量(名称,标题,描述,类型),其中包含存储在其中的文本值,使用AJAX的PHP脚本。然后PHP脚本将这些变量文本值插入到相应的表列中。

然而,在phpMyAdmin中,每当我的$。我试过了。post以及)函数被调用,一个新的行被创建,但它只是具有空值。下面的代码有语法错误吗?有可能传递变量作为对象,就像我下面做的,对吧?因为在我看过的所有例子中,我只看到name: 'smith'type: 1类似的东西。

我知道这可能是坏的做法命名我的变量相同的表列标题,但理论上它应该工作,对吧?

jQuery AJAX

var datavar = {
            name: name,
            title: title,
            description: description,
            longitude: longitude,
            latitude: latitude,
            type: type,
        };
        $.get('school2.php', datavar, function(data) {
                $("#output").html(data);
             });
PHP

$sql="INSERT INTO markers (name, title, description, type)
VALUES
('$_GET[name]', '$_GET[title]', '$_GET[description]', '$_GET[type]')";

如果我像这样写对象字面值,在对象名称上加上数组括号和撇号:

var datavar = [{
            'name': name,
            'title': title,
            'description': description,
            'longitude': longitude,
            'latitude': latitude,
            'type': type,
            }];

我看到,在我的数据库表中,我得到只是得到默认的文本值'name', 'title', 'description'等,而不是null。

$_GET[x]不正确,应该是$_GET['x']。试试这个:

$sql="INSERT INTO markers (name, title, description, type)
VALUES
('".$_GET['name']."', '".$_GET['title']."', '".$_GET['description']."', '".$_GET['type']."')";

另外,作为旁注,您可能想要对数据库输入进行消毒,以防止SQL注入。