-2

我在使用从 ajax 调用的插入查询时遇到了一些问题。ajax 调用成功返回,我可以看到它与更改后的 html 一样,如下面代码中的success:function() 所示。我不确定为什么 process.php 中的插入查询不起作用。dataString 的参数正确(dataString 的警报显示正确的参数)并且我在数据库中的字段可以采用空值。

js代码

  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'/>");
      });
   });

进程.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());
4

2 回答 2

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}')";
于 2012-04-11T22:56:15.603 回答
0

问题在于我调用变量的方式。它应该是 $_POST['company'] 而不是 $_POST('company')。完全错过了它($_POST 的方括号,因为它是一个数组)

于 2012-04-12T17:35:00.540 回答