約 1000 丁の銃を収容する銃器サイトを作成しようとしています。これは多くのデータベース エントリではありませんが、データベースをできるだけ軽量に保つようにしています。正規化を念頭に置いて 5 つのテーブルを作成しましたが、1 つのクエリで 5 つのテーブルすべてにデータを配置するのに問題があります。私のデータベースは次のように構成されています。
+-----------------+ +-----------------+ +-----------------+ +-----------------+
| 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');
}
したがって、上記のコードを使用してアイテムを追加すると、すべて正常に機能しますが、同じメーカー名を使用して別のアイテムを追加すると、それが複製されます。すでに存在していることを認識し、複製しないようにしたいだけです。
そのデータが既に存在するかどうかを確認するために何らかのチェックを行うことを考えていました。存在する場合はデータを入力せず、ID を取得して、必要に応じて他のテーブルに入力します。
私が考えたもう 1 つのことは、重複する可能性が最も高いデータのドロップダウンを作成し、その値を ID として割り当てることでした。しかし、私の単純な心はそれを行うための最良の方法を理解できません:(うまくいけば、すべてが理にかなっています。そうでない場合は、詳しく説明します.