在php / mysql中一个接一个地执行两个查询

时间:2016-01-28 08:15:51

标签: php mysql

我遇到了疑问。我有两个查询在运行。我的需要是它必须执行并完成第一个查询。完成第一个查询后,它应该执行第二个查询。我该怎么做? php页面有点大,所以我没有发布它。好。现在这是第一个php页面,它用最新的货币数据更新价格数据。脚本如下:

<?php
$tender_id=$_GET['tender_id'];

include('config.php');

?>

<?php
$sql_rateinr = "select * from currency1";

$result_rateinr = mysql_query($sql_rateinr) or die($sql_rateinr."<br/><br/>".mysql_error());
while($row_rate = mysql_fetch_array($result_rateinr))
{

$currency= $row_rate['currency'];
    if ($currency=='INR') {
        $rate_inr1 = $row_rate['rate'];
    } else if ($currency=='USD') {
        $rate_usd = $row_rate['rate'];
    } else if ($currency=='GBP'){
        $rate_gbp = $row_rate['rate'];
    } else if ($currency=='EUR'){
        $rate_eur = $row_rate['rate'];
    } else if ($currency=='SGD'){
        $rate_sgd = $row_rate['rate'];
    } else if ($currency=='AUD'){
        $rate_aud = $row_rate['rate'];
    } else if ($currency=='CAD'){
        $rate_cad = $row_rate['rate'];
    } else if ($currency=='CHF'){
        $rate_chf = $row_rate['rate'];
    } else if ($currency=='JPY'){
        $rate_jpy = $row_rate['rate'];
    } else if ($currency=='MYR'){
        $rate_myr = $row_rate['rate'];
    } else if ($currency=='ZAR'){
        $rate_zar = $row_rate['rate'];
    }
}

//(Total / item rate) * INR rate
$result = mysql_query("SELECT * FROM comparitive_st_sup where tender_id='$tender_id'");
//echo '<table width=100% border=1>';
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$item_name=$row['item_name'];
$currency1 =$row['currency'];
$total =$row['total'];
$total_inr =$row['total_inr'];
$supplier_name =$row['supplier_name'];

if ($currency1=='INR') {
    $convertedtotal = $total;
} else if ($currency1=='USD') {
    $convertedtotal = ($total * $rate_inr1);
} else if ($currency1=='GBP'){
    $convertedtotal = (($rate_inr1 / $rate_gbp) * $total);
} else if ($currency1=='EUR'){
    $convertedtotal = (($rate_inr1 / $rate_eur) * $total);
} else if ($currency1=='SGD'){
    $convertedtotal = (($rate_inr1 / $rate_sgd) * $total);
} else if ($currency1=='AUD'){
    $convertedtotal = (($rate_inr1 / $rate_aud) * $total);
} else if ($currency1=='CAD'){
    $convertedtotal = (($rate_inr1 / $rate_cad) * $total);
} else if ($currency1=='CHF'){
    $convertedtotal = (($rate_inr1 / $rate_chf) * $total);
} else if ($currency1=='JPY'){
    $convertedtotal = (($rate_inr1 / $rate_jpy) * $total);
} else if ($currency1=='MYR'){
    $convertedtotal = (($rate_inr1 / $rate_myr) * $total);
} else if ($currency1=='ZAR'){
    $convertedtotal = (($rate_inr1 / $rate_zar) * $total);
}

$query_update = "UPDATE comparitive_st_sup SET total_inr = '$convertedtotal' WHERE id = '$id' and tender_id='$tender_id' ";
mysql_query($query_update) or die ("Error in query: $query_update");

}
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
?>

这是第二个PHP页面,其中包含更新的货币数据,iam更新了另一个表格数据:

查询如下:

mysqli_query($con,"INSERT INTO quotation_terms (tender_id, export_charges, export_charges_inr, iata_charges, iata_charges_inr, coc_charges, coc_charges_inr, calibration_charges, calibration_charges_inr, tp_inspe_charges_inr, tp_inspe_charges, ultrason_inspe_charges_inr, ultrason_inspe_charges, opt_access_cost, opt_access_cost_inr, ext_warranty_cost_inr, ext_warranty_cost, inst_training_cost_inr, inst_training_cost, approx_freight_inr, approx_freight, other_charges_inr, other_charges, currency1)

VALUES ('$tender_id','$export_charges','$exportinr','$iata_charges','$iatainr','$coc_charges','$cocinr','$calibration_charges','$calibrationinr','$tpinr','$tp_inspe_charges', '$ultrasoninr', '$ultrason_inspe_charges', '$opt_access_cost', '$optinr', '$extinr', '$ext_warranty_cost', '$instinr', '$inst_training_cost', '$freightinr', '$approx_freight', '$otherinr', '$other_charges', '$currency')");
echo "<br>Checkbox Values submitted to Terms DB";

运行第一个php查询后,它应该运行第二个查询。任何提示..?

2 个答案:

答案 0 :(得分:0)

我假设第二个查询依赖于放在上一个查询中的内容。如果是这样,您可能需要查看“mysqli_insert_id”,它将获得在以前的查询中生成的ID。

更多信息:http://php.net/manual/en/mysqli.insert-id.php

答案 1 :(得分:0)

我只想简单一点。我为查询逻辑创建函数。请试试。

function multiQueryExec($type = 'SELECT', $arrParam = array()){

    $returnVal = array();

    if($type == 'SELECT'){

        $tender_id = $arrParam['tender_id'];

        //(Total / item rate) * INR rate
        $result = mysql_query("SELECT * FROM comparitive_st_sup where tender_id='$tender_id'");

        while($row = mysql_fetch_array($result)){
            $id=$row['id'];
            $item_name=$row['item_name'];
            $currency1 =$row['currency'];
            $total =$row['total'];
            $total_inr =$row['total_inr'];
            $supplier_name =$row['supplier_name'];

            if ($currency1=='INR') {  $convertedtotal = $total;
            } else if ($currency1=='USD') { $convertedtotal = ($total * $rate_inr1);
            } else if ($currency1=='GBP'){ $convertedtotal = (($rate_inr1 / $rate_gbp) * $total);
            } else if ($currency1=='EUR'){ $convertedtotal = (($rate_inr1 / $rate_eur) * $total);
            } else if ($currency1=='SGD'){  $convertedtotal = (($rate_inr1 / $rate_sgd) * $total);
            } else if ($currency1=='AUD'){ $convertedtotal = (($rate_inr1 / $rate_aud) * $total);
            } else if ($currency1=='CAD'){ $convertedtotal = (($rate_inr1 / $rate_cad) * $total);
            } else if ($currency1=='CHF'){ $convertedtotal = (($rate_inr1 / $rate_chf) * $total);
            } else if ($currency1=='JPY'){ $convertedtotal = (($rate_inr1 / $rate_jpy) * $total);
            } else if ($currency1=='MYR'){ $convertedtotal = (($rate_inr1 / $rate_myr) * $total);
            } else if ($currency1=='ZAR'){ $convertedtotal = (($rate_inr1 / $rate_zar) * $total);
            }
        }

        $returnVal['currency1'] = $currency1;
        $returnVal['convertedtotal'] = $convertedtotal;
    }
    else {
        $id = $arrParam['id'];
        $convertedtotal = $arrParam['convertedtotal'];
        $tender_id = $arrParam['tender_id'];

        $query_update = "UPDATE comparitive_st_sup SET total_inr = '$convertedtotal' WHERE id = '$id' and tender_id='$tender_id' ";
        mysql_query($query_update) or die ("Error in query: $query_update");
        $returnVal['tender_id'] = $tender_id;
    }
    return $returnVal;

//Call for first select query after currency query
$arrParam = array();
$arrParam['tender_id'] = $tender_id;
$getResult = multiQueryExec('SELECT', $arrParam);

//Call for update second query
$arrParam = array();
$arrParam['tender_id'] = $tender_id;
$arrParam['convertedtotal'] = $convertedtotal;
$arrParam['id'] = $id;
$getResult = multiQueryExec('SELECT', $arrParam);

现在,当您从第一页到第二页旅行时,将第二页上的每个数据传递给第二页并在第二页上检查相同数据,如果数据可用则只调用quotation_terms期限查询,否则再次在第一页上抛出。