0

我正在尝试将发票中的数据写入 2 个表表 1 是它包含的发票表:

invoice_id account sales purchase_date

然后是第二个表 invoice_items 它包含有关发票中每一行的信息。

id  invoice_id  item    description     quantity    price   amount

基本上现在我正在检查 id 是否存在,如果存在,我检查 line id 是否存在,如果不存在,它会添加发票信息,如果 id 存在,它会检查 line id 是否存在

$checkin = mysql_query("
SELECT `id`
FROM `invoices`
WHERE `invoices`.`id` =$invoice_id
");

if (mysql_num_rows($checkin) == 1) 
{
  $updatememocount ++;

  mysql_query("UPDATE `invoices`  SET `invoices`.`INV_ID` = '".$invid."'WHERE `invoices`.`id` =$invoice_id ") or die("load1 -" . mysql_error());

  // check line id
  $checklineid = mysql_query("SELECT `Line_ID` from `invoice_items` WHERE `Line_ID` = '$lineid'") or die("load1 -" . mysql_error());
  if (mysql_num_rows($checklineid) == 0) 
  {
    $insertlinecount ++;
    mysql_query("INSERT INTO `invoice_items` (invoice_id, item, description, quantity, price, amount, Line_ID) VALUES ('".$invoice_id."', '".mysql_real_escape_string($row['3'])."', '".mysql_real_escape_string($row['4'])."', '".$row['9']."', '".$row['7']."', '".$row['5']."', '".$row['14']."' ) ") or die("load1 -" . mysql_error());
  } 
}
else
{
  // makes new invoice entry and enters the line item information for the first line.
}

数据样本 这是数据的来源

Date    Invoice_Id    Item           Description             Price    Amount   Customer_ID   Line_ID
05/12/12  1234        something     whatever somthing  is     15        15       2255          123
05/12/12  1234        Another_thing  Whatever that is         25        25       2255          124
4

1 回答 1

0

看这个例子,在重复键插入

    <?php
    $sql = "SELECT COUNT(UserID) FROM configuration WHERE UserID='SomeUser'";
    $result = mysqli_query($db,$sql);
    if ($result && mysqli_num_rows($result)>0) {
    $aResult = mysqli_fetch_array($result);
    $iRecordExists = ($aResult[0]>0?1:0);
}

   if ($iRecordExists>0) {
   //make the insert with another ID
    }
    else {
    //do an insert
    $sql = "INSERT INTO configuration SET someField='someValue', UserID='SomeUser'";
    mysqli_query($db,$sql);
    }
    ?>
于 2012-06-11T20:30:29.197 回答