【问题标题】:Ajax update, comparing and insert data to mysqlAjax更新,比较和插入数据到mysql
【发布时间】:2013-05-21 12:03:06
【问题描述】:

美好的一天, 正如主题中提到的,我正在创建一个 ajax 函数,其中 php 将直接更新状态,然后如果状态为 1(或批准),它将在 2 个表(tblcompany 和 tblinternapplication)之间进行比较,如果公司不在列表中。我尝试一一测试它运行良好,但是在合并后,当人员申请批准(或设置为 1)时,即使 tblinternapplication 中的状态已更新,它也不会添加任何新公司。下面是我的代码。

 <?php require_once("../includes/session.php"); ?>
 <?php require_once("sessioncourse.php"); ?>
 <?php confirm_logged_in(); ?>
 <?php require_once("../includes/connection.php") ?>
 <?php require_once("../includes/functions.php") ?>

 <?php

 $id = $_GET['id'];
 $status =$_GET['status'];

 $sql="UPDATE tblinternapplication set status_approval  =     
 ".mysql_real_escape_string($status) ." WHERE id = " .mysql_real_escape_string($id);
 $result = mysql_query($sql);

 $querysel = "SELECT i.company_code, c.company_name as cn, i.company_name as ic, 
               c.company_branch as cb, i.company_branch as ib, FROM tblcompany c, 
               tblinternapplication i WHERE i.id = '$id' ";
 $resultsel = mysql_query($querysel, $connection);
 $queryselc = "SELECT 
    company_name, company_branch,
    company_address, post_code,
    company_city, company_state,
    company_country,
    company_phone, company_fax,
    company_url FROM tblinternapplication WHERE id = '$id' ";
 $resultselc = mysql_query($queryselc, $connection);

 if ($status == 1){
  while($rowsel = mysql_fetch_array($resultsel)){
   if($rowsel['company_code'] == NULL){
    if(($rowsel['cn'] != $rowsel['ic']) OR ($rowsel['ib'] != $rowsel['cb'])){
    while($rowselc = mysql_fetch_array($resultselc)){
      $query = "INSERT INTO tblcompany (
      company_name, company_branch,
      company_address, post_code,
      company_city, company_state, company_country, 
      company_phone, company_fax,
      company_url
  ) VALUES (
  '{$rowselc['company_name']}', '{$rowselc['company_branch']}',
      '{$rowselc['company_address']}','{$rowselc['post_code']}',
      '{$rowselc['company_city']}','{$rowselc['company_state']}',
      '{$rowselc['company_country']}',
      '{$rowselc['company_phone']}','{$rowselc['company_fax']}',
      '{$rowselc['company_url']}'
  )";
  $resultc = mysql_query($query, $connection); 
    }
   }
  }
}
}

?>

【问题讨论】:

  • 那么,Ajax 代码在哪里
  • ajax 代码我没有发布,因为该代码没有任何问题。
  • 您不需要在文件开头打开和关闭 php-tags。只需打开一次。
  • 好的,注意到但我把php-tags开头是为了标准化所有其他页面。

标签: php mysql ajax


【解决方案1】:

只是用我自己的方法分享答案。基本上我删除了 2 级嵌套 while 并使第一个查询行匹配,然后第二个是搜索结果。希望这对其他人有帮助。

<?php

$id = $_GET['id'];
$status = $_GET['status'];

$sql="UPDATE tblinternapplication set status_approval  = 
".mysql_real_escape_string($status) ." WHERE id = " .mysql_real_escape_string($id);
$result = mysql_query($sql);

$querysel = "SELECT i.company_code, i.company_name, i.company_branch, c.company_name,   
c.company_branch FROM tblinternapplication i, tblcompany c WHERE i.company_name = 
c.company_name AND i.company_branch = c.company_branch AND i.id = '$id' ";
$resultsel = mysql_query($querysel, $connection);

$queryselc = "SELECT * FROM tblinternapplication where id = '$id'";
$resultselc = mysql_query($queryselc, $connection);

if ($status == 1){
if(mysql_num_rows($resultsel) == 0){
    while($rowselc = mysql_fetch_array($resultselc)){
$query = "INSERT INTO tblcompany (
    company_name, company_branch,
    company_address, post_code,
    company_city, company_state, company_country, 
    company_phone, company_fax,
    company_url
) VALUES (
'{$rowselc['company_name']}', '{$rowselc['company_branch']}',
    '{$rowselc['company_address']}','{$rowselc['post_code']}',
    '{$rowselc['company_city']}','{$rowselc['company_state']}',
    '{$rowselc['company_country']}',
    '{$rowselc['company_phone']}','{$rowselc['company_fax']}',
    '{$rowselc['company_url']}'
)";
$resultc = mysql_query($query, $connection); 
    }
 }
}


?>

如果有人有推荐,欢迎离开 cmets。 谢谢。

【讨论】:

    猜你喜欢
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2014-11-26
    • 2013-04-06
    • 1970-01-01
    相关资源
    最近更新 更多