元素未添加到数据库中

时间:2013-10-25 17:34:08

标签: php jquery html mysql ajax

我正在创建一个项目规划器,其工作方式非常类似于购物清单,一切都在客户端正常工作;但是,当我尝试将新的附加元素添加到数据库时,它不起作用。其他一切都被添加到数据库中,如id和日期,但不是创建的新元素,我将其保存为文本。 php给我的错误是$ _POST [elem]没有价值。

Jquery / Ajax

$(document).ready(function(){
//  GLOABL VARIABLES
var project     = "";
var div         = "";
var categoryDiv = "";
var category    = "";

// Adding a project
$('.project-btn').click(function(e){
    e.preventDefault();

    //grab the user input for the new project
    var project = $('.project-val').val();

    //Add the project to the list
    $('<li></li>').addClass(project).appendTo('.project-list');
    $('<a></a>').attr("href",project).text(project).appendTo('li.'+project);

    // create the div where the categories will go
    $('<div></div>').attr("id",project).appendTo('.category-wrapper');
    // hide the div untill it is called upon
    $('div#'+project).fadeOut(function(){
        $('<h1></h1>').text(project).css("text-align","center").appendTo('div#'+project);
        // add the input for the category div
        $('<input>').attr("type","text").addClass('category-val').appendTo('div#'+project);
        $('<input>').attr("type","submit").attr("value","Add Category").addClass("category-btn").appendTo('div#'+project);
        // add the back button
        $('<p></p>').text("back").addClass('category-back').css("cursor","pointer").appendTo('div#'+project);
        // add the ul
        $('<ul></ul>').attr("class","category-list").appendTo('div#'+project);
    });

    // clear the input search
    $('.project-val').val('');

    // add to the database
    var elem={};
    elem ='<li class="'+project+'">'+project+'</li>';

    $.ajax({

     url: 'project_list.php',
     data: elem,
     type: 'POST',
     success: function(response)
     {
       alert("success");
     }

     });//end of ajax call
}); 
});

PHP:

<?php 
include 'inc/connect.php'; 

$string = $_POST['elem'];


mysqli_query($con, "INSERT INTO `project_list` (string) VALUES ('$string')");

mysqli_close($con);

?>

1 个答案:

答案 0 :(得分:2)

数据必须是'“elem =”+ elem',here is an example

您的代码也容易受到SQL注入攻击,请检查以防止它们出现:How can I prevent SQL injection in PHP?