我正在尝试创建一个可以容纳大约 1000 支枪的枪支站点。这不是很多数据库条目,但我试图让数据库尽可能轻。我创建了五个表,牢记规范化,并且在一次查询中将数据放入所有五个表时遇到问题。我的数据库结构如下:
+-----------------+ +-----------------+ +-----------------+ +-----------------+
| 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>
添加产品.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');
}
因此,当我使用上面的代码添加一个项目时,一切正常,但是当我使用相同的制造商名称添加另一个项目时,它会复制它。我只是想让它意识到它已经存在而不是复制它。
我正在考虑进行某种类型的检查以查看该数据是否已经存在,如果存在则不要输入数据,而是获取 id 并在需要的其他表中输入它。
我想到的另一件事是为最有可能被复制的数据创建一个下拉列表,并将该值分配为 id。但是,我简单的头脑无法找出最好的方法:(希望所有这些都是有道理的,如果没有,我会尝试详细说明。