-1

我有一个 php 脚本,它具有基于用户选择/输入的一系列计算和 FORM RELOADS,只有在表中而不是在 FORM RELOADS 上完成记录添加时,我才需要运行这些,如何在这种情况下运行循环。

我的部分 php 脚本 bill-detail.php: 希望这足够:

<html>
<head>
<title>Cash Bill Detail</title>


<script language="javascript">

function reload(form)
{
document.location.href = 'bill-detail.php'
}



function showecr(str)
{

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","billecr.php?q1=" + str,false);
xmlhttp.send();
 }

</script>


</head>
<body>

<?php
$con = mysql_connect("localhost","svga","a!@#");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("svga", $con);

$user = $_SESSION['user'];

//<!--------------VALIDATIONS ------------------->

echo "<form name='form1'>";
echo "ECR: <input type='text' size='10' maxlength='10' name='ecrn'     onchange=\"showecr(this.value);\"><br>";
echo "Product Type";
echo "<select name='product' onchange=\"reload(this.form);\">>";
echo "<option value='Gas'>Gas</option>";
echo "<option value='Others'>Others</option>";
echo "</select>";
echo "</form>";


//---As per user inputs copying value to variables---------------------
$hdr=mysql_query("SELECT * FROM BILLHDR_draft WHERE usr='$user'");
$hdr2=mysql_fetch_assoc($hdr);
$numrows=mysql_num_rows($hdr);

if ($numrows>0)

{
$cust_code=$hdr2['Ac_code'];
$product=$hdr2['prod_desc'];

//--Searching BILL DETAILS TABLE as per the user inputs from BILL HEADER Table------
$recs=mysql_query("SELECT * FROM BILLDTLS WHERE Ac_code='$cust_code' AND     Prod_desc='$product'");
while ($rec2=mysql_fetch_assoc($recs))
{
mysql_query("INSERT INTO DEMURAGE (DC_No, DC_date, Ac_code, Product_Desc) VALUES ('$rec2[dc]', '$rec2[dcdate]', '$rec2[accode]', '$rec2[product]'");
}

}

mysql_close($con);
?>

4

1 回答 1

0

您可以在循环之前添加一个布尔变量,您可以使用它来跟踪是否已经添加了记录。以下代码主要是概念,因为您自己没有提供太多代码示例,但希望您能理解。

// Set to false initially
$recordAdded = FALSE;

if( $num_rows > 0 && $recordAdded == FALSE )
{
   // Code to execute

   // Set value to to TRUE after initial successful execution
   $recordAdded = TRUE;
}
于 2012-07-12T06:01:18.063 回答