PHP/MYSQL:使用相同标识号的多个值

PHP/MYSQL:使用相同标识号的多个值,php,mysql,Php,Mysql,我试图将发票中的数据写入两个表中 表1是发票表 它包括: 发票\u id账户销售采购\u日期 然后,第二个表invoice\u items包含有关发票中每一行的信息 id invoice_id item description quantity price amount 基本上,现在我检查id是否存在,如果存在,我检查行id是否存在,如果不存在,它会为发票添加信息如果id存在,它会检查行id是否存在 $checkin = mysql_query(" SELECT `

我试图将发票中的数据写入两个表中 表1是发票表 它包括:

发票\u id账户销售采购\u日期

然后,第二个表invoice\u items包含有关发票中每一行的信息

id  invoice_id  item    description     quantity    price   amount
基本上,现在我检查id是否存在,如果存在,我检查行id是否存在,如果不存在,它会为发票添加信息如果id存在,它会检查行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
看看这个例子


为什么不为发票id使用自动递增值呢?发票id随数据而来。我暂时认为我可以计算所有的实例,然后循环并写入,直到计数器=0,但我没有;我不知道怎么写。我不知道这是否只是我自己,但我有点困惑。我正在检查id是否存在,如果存在,我检查行id是否存在,如果不存在,它将添加发票信息如果id存在,它将检查行id是否存在???更准确一点,不要担心大问题,信息越多,答案就越好。我想我明白了。我会分开处理。我将检查它是否存在于发票表中,如果不存在,我将插入。然后我将检查它是否在行项目表中,如果不在,我将插入。听起来很简单。我试图通过发票表来验证整件事。这与我的想法非常接近。如何计算数组中的实例数。我正在从csv文件中读取此信息。
    <?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);
    }
    ?>