Php 如何处理多个表而不获取重复数据?(MySQL/PDO)
我正试图创建一个枪支网站,将容纳约1000支枪支。这并不是很多数据库条目,但我正在尽可能地保持数据库的轻量级。我创建了五个表,记住了规范化,在一个查询中将数据放入所有五个表时遇到了问题。我的数据库结构如下:Php 如何处理多个表而不获取重复数据?(MySQL/PDO),php,mysql,pdo,Php,Mysql,Pdo,我正试图创建一个枪支网站,将容纳约1000支枪支。这并不是很多数据库条目,但我正在尽可能地保持数据库的轻量级。我创建了五个表,记住了规范化,在一个查询中将数据放入所有五个表时遇到了问题。我的数据库结构如下: +-----------------+ +-----------------+ +-----------------+ +-----------------+ | make + | model | | image | |
+-----------------+ +-----------------+ +-----------------+ +-----------------+
| make + | model | | image | | type |
+-----------------+ +-----------------+ +-----------------+ +-----------------+
| PK | make_id | | PK | model_id | | PK | model_id | | PK | type_id |
+-----------------+ +-----------------+ +-----------------+ +-----------------+
| | make_name | | | make_id | | | image_path | | | type_name |
+-----------------+ +-----------------+ +-----------------+ +-----------------+
| | type_id |
+-----------------+ +------------------+
| | caliber_id | | caliber |
+-----------------+ +------------------+
| | model_name | | PK | caliber_id |
+-----------------+ +------------------+
| | cost | | | caliber_name|
+-----------------+ +------------------+
| | description|
+-----------------+
这可能太规范化了,但这正是我正在处理的;)
让我展示一下代码:
表格
<form action="post" method="addProduct.php" enctype="multipart/form-data">
make: <input type="text" name="make" />
model: <input type="text" name="model" />
type: <input type="text" name="type" />
caliber: <input type="text" name="caliber" />
cost: <input type="text" name="cost" />
desc.: <input type="text" name="description" />
Image: <input type="file" name="image" id="image" />
<input type="submit" name="submit" value="Add Item" />
</form>
制作:
型号:
类型:
口径:
费用:
描述:
图片:
addProduct.php
$make = $_POST['make'];
$model = $_POST['model'];
$type = $_POST['type'];
$caliber = $_POST['caliber'];
$cost = $_POST['cost'];
$description = $_POST['description'];
$image = basename($_FILES['image']['name']);
$uploadfile = 'pictures/temp/'.$image;
if(move_uploaded_file($_FILES['image']['tmp_name'],$uploadfile))
{
$makeSQL = "INSERT INTO make (make_id,make_name) VALUES ('',:make_name)";
$typeSQL = "INSERT INTO type (type_id,type_name) VALUES ('',:type_name)";
$modelSQL = "INSERT INTO model (model_id,make_id,type_id,caliber,model_name,cost,description,) VALUES ('',:make_id,:type_id,:caliber,:model_name,:cost,:description)";
$imageSQL = "INSERT INTO image (model_id,image_path) VALUES (:model_id,:image_path)";
try
{
/* db Connector */
$pdo = new PDO("mysql:host=localhost;dbname=gun",'root','');
/* insert make information */
$make = $pdo->prepare($makeSQL);
$make->bindParam(':make_name',$make);
$make->execute();
$make->closeCursor();
$makeLastId = $pdo->lastInsertId();
/* insert type information */
$type = $pdo->prepare($typeSQL);
$type->bindParam(':type_name',$type);
$type->execute();
$type->closeCursor();
$typeLastId = $pdo->lastInsertId();
/* insert model information */
$model = $pdo->prepare($modelSQL);
$model->bindParam(':make_id',$makeLastId);
$model->bindParam(':type_id',$typeLastId);
$model->bindParam(':caliber',$caliber);
$model->bindParam(':model_name',$model);
$model->bindParam(':cost',$cost);
$model->bindParam(':description',$description);
$model->execute();
$model->closeCursor();
$modelLastId = $pdo->lastInsertId();
/* insert image information */
$image = $pdo->prepare($imageSQL);
$image->bindParam(':model_id',$modelLastId);
$image->bindParam(':image_path',$image);
$image->execute();
$image->closeCursor();
print(ucwords($manu));
}
catch(PDOexception $e)
{
$error_message = $e->getMessage();
print("<p>Database Error: $error_message</p>");
exit();
}
}
else
{
print('Error : could not add item to database');
}
$make=$\u POST['make'];
$model=$_POST['model'];
$type=$_POST['type'];
$calible=$_POST['calible'];
$cost=$_POST['cost'];
$description=$_POST['description'];
$image=basename($_文件['image']['name']);
$uploadfile='pictures/temp/'。$image;
如果(移动上传的文件($上传文件['image']['tmp\u name'],$uploadfile))
{
$makeSQL=“插入make(make_id,make_name)值(“”,:make_name)”;
$typeSQL=“插入类型(类型标识,类型名称)值(“”,:类型名称)”;
$modelSQL=“插入模型(模型id、制造id、类型id、口径、模型名称、成本、描述)值(“”,:制造id,:类型id,:口径,:模型名称,:成本,:描述)”;
$imageSQL=“插入图像(模型标识,图像路径)值(:模型标识,图像路径)”;
尝试
{
/*db连接器*/
$pdo=new-pdo(“mysql:host=localhost;dbname=gun”,“根”,“根”);
/*插入制作信息*/
$make=$pdo->prepare($makeSQL);
$make->bindParam(':make_name',$make);
$make->execute();
$make->closeCursor();
$makeLastId=$pdo->lastInsertId();
/*插入类型信息*/
$type=$pdo->prepare($typeSQL);
$type->bindParam(':type_name',$type);
$type->execute();
$type->closeCursor();
$typeLastId=$pdo->lastInsertId();
/*插入型号信息*/
$model=$pdo->prepare($modelSQL);
$model->bindParam(':make_id',$makeLastId);
$model->bindParam(':type_id',$typeLastId);
$model->bindParam(':calible',$calible);
$model->bindParam(':model_name',$model);
$model->bindParam(':cost',$cost);
$model->bindParam(':description',$description);
$model->execute();
$model->closeCursor();
$modelLastId=$pdo->lastInsertId();
/*插入图像信息*/
$image=$pdo->prepare($imageSQL);
$image->bindParam(':model_id',$modelLastId);
$image->bindParam(':image\u path',$image);
$image->execute();
$image->closeCursor();
印刷(文字($manu));
}
捕获(PDO$e)
{
$error_message=$e->getMessage();
打印(“数据库错误:$Error\u message”;
退出();
}
}
其他的
{
打印('错误:无法将项添加到数据库');
}
因此,当我使用上述代码添加一个项目时,一切正常,但当我使用相同的制造商名称添加另一个项目时,它将复制它。我只是想让它意识到它已经存在,而不是复制它
我正在考虑进行某种类型的检查,以查看该数据是否已经存在,如果已经存在,则不输入数据,而是获取id并在需要的其他表中输入该id
我想到的另一件事是为最有可能被复制的数据创建一个下拉列表,并将值指定为id。但是,我简单的头脑无法找到最好的方法:(希望所有这些都有意义,如果没有,我将尝试详细说明。你需要找出什么是唯一的(不重复的)品牌、型号、类型和口径 然后,您需要为那些实施唯一性的表创建唯一索引。请参阅 例如,您可以使用make_id、model_name和calible_id来唯一标识模型
CREATE UNIQUE INDEX UNIQUEMODEL ON MODEL(make_id, caliber_id, model_name)
设置唯一索引。请注意,主键索引可以是唯一索引,但也可以有其他唯一索引
然后,您可以使用INSERT ON DUPLICATE KEY UPDATE
填充表。请参见此处:
为了使所有这些都能正常工作,在您尝试填充每个
模型行之前,您必须确保适当的类型,口径,以及使行存在:您需要前三个表中的ID来填充第四个表。如果您有字段,其中只有一个限制设置为f data(calibre肯定是其中之一,我怀疑manfactorer也会起作用)您可以在数据库中预先填充表,并将其转换为查找字段
在HTML表单上,您可以打印一个选择框,而不是文本输入字段。选择框的值是查找字段中的ID-您不必担心向数据库中的查找字段添加任何内容
当我过去不得不这样做时,我编写了一个函数来执行数据插入;它检查值是否在表中。如果在表中,它将返回字段的索引;否则,它将其作为一个新条目添加,并返回新条目的ID。这不是最优雅的解决方案,但效果很好。谢谢andrewsi,我选择了您的建议。我只是为唯一的数据填充选择字段。我发现的另一件事是,PDO内置了功能,就像Ollie在MySQL中提到的那样,插入数据时,它会注意到重复的条目,并且不会在抛出自定义错误时完成插入。谢谢!