数据通过ajax请求

时间:2016-07-21 10:04:08

标签: php jquery ajax

我试图通过ajax请求发送我的表单数据,但是当插入代码运行时,它没有存储在数据库中。

 var form_data = $('#form_company').serialize();
  var request   = $.ajax({
    url:          'data.php',
    cache:        false,
    data:        {job : "add_company", form_data:form_data},
    dataType:     'json',
    contentType:  'application/json; charset=utf-8',
    type:         'get'
  });
  request.done(function(output){
    if (output.result == 'success'){
      // Reload datable
      table_companies.api().ajax.reload(function(){
        hide_loading_message();
        var company_name = $('#company_name').val();
        show_message("Company '" + company_name + "' added successfully.", 'success');
      }, true);
    } else {
      hide_loading_message();
      //alert ("sucks");
      show_message('Add request failed', 'error');
    }
  });

在其他php页面上有用于将其插入数据库的代码 的 data.php

<?php
 // Database details
  $db_server   = 'localhost';
  $db_username = 'root';
  $db_password = '';
$db_name     = 'ocean';

 // Get job (and id)
  $job = '';
$id  = '';
if (isset($_GET['job'])){
 $job = $_GET['job'];
 if ($job == 'get_companies' ||
  $job == 'get_company'   ||
  $job == 'add_company'   ||
  $job == 'edit_company'  ||
  $job == 'delete_company'){
  if (isset($_GET['id'])){
  $id = $_GET['id'];
  if (!is_numeric($id)){
    $id = '';
    }
  }
 } else {
  $job = '';
 }
 }
if ($job == 'add_company'){

// Add company
$query = "INSERT INTO it_companies SET ";
if (isset($_GET['rank']))         { $query .= "rank         = '" . mysqli_real_escape_string($db_connection, $_GET['rank'])         . "', "; }
if (isset($_GET['company_name'])) { $query .= "company_name = '" . mysqli_real_escape_string($db_connection, $_GET['company_name']) . "', "; }
if (isset($_GET['industries']))   { $query .= "industries   = '" . mysqli_real_escape_string($db_connection, $_GET['industries'])   . "', "; }
if (isset($_GET['revenue']))      { $query .= "revenue      = '" . mysqli_real_escape_string($db_connection, $_GET['revenue'])      . "', "; }
if (isset($_GET['fiscal_year']))  { $query .= "fiscal_year  = '" . mysqli_real_escape_string($db_connection, $_GET['fiscal_year'])  . "', "; }
if (isset($_GET['employees']))    { $query .= "employees    = '" . mysqli_real_escape_string($db_connection, $_GET['employees'])    . "', "; }
if (isset($_GET['market_cap']))   { $query .= "market_cap   = '" . mysqli_real_escape_string($db_connection, $_GET['market_cap'])   . "', "; }
if (isset($_GET['headquarters'])) { $query .= "headquarters = '" . mysqli_real_escape_string($db_connection, $_GET['headquarters']) . "'";   }
  //var_dump($query);
   $query = mysqli_query($db_connection, $query);
    if (!$query){
     $result  = 'error';
     $message = 'query error';
  } else {
  $result  = 'success';
    $message = 'query success';
   }

    }

代码有什么问题?我怎么能得到我想要的结果。

2 个答案:

答案 0 :(得分:1)

从ajax调用中返回一些内容

$query = mysqli_query($db_connection, $query);
    if (!$query){
     $result  = 'error';
     $message = 'error';
  } else {
  $result  = 'success';
    $message = 'success';
   }

    }
    header('Content-Type: application/json');
    echo json_encode(['result'=>$message]);

答案 1 :(得分:1)

似乎您的字段名为rankcompany_nameindustries,这些字段是保留字。像这样使用backquotes

  

'rank' 'company_name' 'industries'