3

美好的一天,正如主题中提到的,我正在创建一个 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); 
    }
   }
  }
}
}

?>
4

1 回答 1

0

只是用我自己的方法分享答案。基本上我删除了 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); 
    }
 }
}


?>

如果有人有建议欢迎发表评论。谢谢你。

于 2013-05-26T19:07:51.700 回答