在php中插入查询不能使用ajax

时间:2012-04-11 22:32:21

标签: php ajax insert

我遇到了一些从ajax调用的插入查询问题。 ajax调用成功返回,我可以通过改变的html看到它,如下面成功的代码中所述:function()。我不确定为什么process.php中的insert查询不起作用。 dataString的参数是正确的(dataString的警告显示正确的参数),我在数据库中的字段可以取空值。

js code

  var dataString=$('#testimonials').serialize();

  alert (dataString);

  $.ajax(
  {
    type: "POST",
    url: "process.php",
    data: dataString,
    success:function() {
      $('#testimonials').html("<div id='message'></div>");
      $('#message').html("<h2>Your information has been submitted!</h2>")
      .append("<p>Thank you for your help and support.</p>")
      .hide()
      .fadeIn(1500, function() 
      {
        $('#message').append("<img id='checkmark' src='images/check.png' height='30' width='30'/>");
      });
   });

process.php文件

    $company =mysql_escape_string($_POST('company'));
    $jobfunc = mysql_escape_string($_POST('jobfunc'));
    $location = mysql_escape_string($_POST('location'));
    $overall = mysql_escape_string($_POST('overall'));
    $detail = mysql_escape_string($_POST('detail'));
    $pros = mysql_escape_string($_POST('pros'));
    $cons = mysql_escape_string($_POST('cons'));
    $sr_mgmt = mysql_escape_string($_POST('sr_mgmt'));
    $submitted_by = mysql_escape_string($_POST('submitted_by'));
    $class = mysql_escape_string($_POST('classof'));
    $school = mysql_escape_string($_POST('school'));
    $anonymous = mysql_escape_string($_POST('anonymous'));      

    mysql_select_db($database_connTest, $connTest);

    $query_AddTestimonial = "INSERT into testimonials (company,job_function,location,overall,project_details,pros,cons,sr_mgmt,submitted_by,class,school,anonymous) VALUES ('$company','$jobfunc','$location','$overall','$detail','$pros','$cons','$sr_mgmt','$submitted_by','$class','$school','$anonymous')";
    $result_AddTestimonial = mysql_query($query_AddTestimonial) or die(mysql_error());

2 个答案:

答案 0 :(得分:0)

在创建$ query_AddTestimonial的倒数第二行中,您创建的字符串不会放入php变量,因为您没有告诉它它们是变量。你可以像这样使用php变量:

$query_AddTestimonial = "INSERT into testimonials (company,job_function,location,overall,project_details,pros,cons,sr_mgmt,submitted_by,class,school,anonymous) VALUES ('{$company}','{$jobfunc}','{$location}','{$overall}','{$detail}','{$pros}','{$cons}','{$sr_mgmt}','{$submitted_by}','{$class}','{$school}','{$anonymous}')";

答案 1 :(得分:0)

问题在于我调用变量的方式。应该是$ _POST ['company']而不是$ _POST('company')。完全错过了它(自阵列以来$ _POST的方括号)