我正在构建一个相当简单的网站来跟踪一些工作销售情况。它涉及一个mysql数据库,每个条目有多个表。大多数情况下,这种关系是被割断和干燥的。不过,我有一个评论表,它将存储多个评论以供同一个销售。我在注释表中有一个外键与主销售表的ID绑定。我在总表和销售表之间有一个类似的安排,因为它存储了同一个销售ID的多个总额。将这些插入数据库的最佳方法是什么?目前,我正在插入注释并获取该行的ID,然后插入总量并获取ID,然后使用注释ID和总体ID将插入插入到销售表中。有没有更有效的方法,而不是做5个查询?PHP MYSQL数据库结构,一个条目的多个查询
- 编辑:这是当前的代码。我对此很新,所以我不得不查找程序来查看你的意思。这似乎基本上是我在做什么,而是为它创建一种方法,一旦我找出最有效的方法,我将会这样做。
//Prepare the Queries
//Insert Gross Amounts Query
$grossQ = "INSERT INTO gross (commGross, storeGross, manGross, fiGross, flatGross) VALUES ('$commGross', '$storeGross', '$manGross', '$fiGross')";
//Insert Comments Query
$commentsQ = "INSERT INTO comments (comment, dealcomment, grosscomment) VALUES ('$otherComments', '$dealComments', $grossComments')";
//Insert Deal Query
$q = "INSERT INTO sales (make, model, location, vehtype, saletype, manager, county, flat, unitcount, armoramount, controlnumber, salesman, stock) VALUES ('$make', '$model', '$location', '$vehType', '$saleType', '$managerName', '$county', '$flat', '$unitCount', '$armorAmount', '$controlNumber', '$salesmanName1', '$stock')";
//Make database connection
$db = new db('palm_sales');
//Execute gross query and get the id of the row
$db->execute($grossQ);
$grossID = $db->getLastID();
//Execute comments query and get the id of the row
$db->execute($commentsQ);
$commentsID = $db->getLastID();
//Build Main Query
$q = "INSERT INTO sales (make, model, location, vehtype, saletype, manager, gross, comments, county, flat, unitcount, armoramount, controlnumber, salesman, stock) VALUES ('$make', '$model', '$location', '$vehType', '$saleType', '$managerName', '$grossID', '$commentsID', '$county', '$flat', '$unitCount', '$armorAmount', '$controlNumber', '$salesmanName1', '$stock')";
//Execute Main Query
$db->execute($q);
if(mysql_affected_rows() > 0)
{
echo "Success!";
}
else echo "Error: ".mysql_error();
你能写一个存储过程吗? –
你可以发布你的代码吗?最有效的方法可能是将您的插入在预先触发器和触发器之间进行拆分 –
您应该编写一个存储过程,或者直接使用sql事务来确保一切正常或nok。 –