我正在嘗試創建一個可容納大約1000支槍的槍支網站。這不是很多數據庫條目,但我試圖儘可能保持數據庫的輕量級。我創建了五個表格,記住了標準化,並且我在將數據放入一個查詢中的所有五個表格時遇到了問題。我的數據庫的結構,像這樣:如何使用多個表並且不會獲取重複的數據? (MySQL/PDO)
+-----------------+ +-----------------+ +-----------------+ +-----------------+
| 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並在需要的地方輸入其他表。
我想到的另一件事是爲最有可能被複制的數據創建一個下拉列表並將該值指定爲id。但是,我的簡單思維無法弄清楚最好的方法:(希望一切都有道理,如果不是的話,我會盡力詳細說明)
謝謝安德魯西,我隨你的建議去了。我只是填充唯一數據的選擇字段。我發現的另一件事是,PDO具有內置的功能,就像Ollie爲MySQL提到的那樣,當插入數據時,它會注意到重複條目,並且在拋出定製錯誤時不會完成插入操作。謝謝! – Mike