Mysql

市場的數據庫架構posts一種ttributesp這s噸s一種噸噸r一世b在噸和sposts_attributes

  • October 25, 2021

好的,所以我正在製作一個市場類型的網站,例如 olx 等。使用者可以發布不同類型的文章,這意味著欄位會發生變化,所以我製作了一個名為 posts_attributes 的額外表,這些變化的欄位會保存在其中。

我目前的問題是當我想顯示文章屬性時,我想在它旁邊顯示一個標題,例如,假設使用者現在在品牌中發布了一輛待售汽車

$$ input $$他輸入 volvo 的欄位,當我想顯示它時,文本 volvo 將保存在 posts_attributes 表中然後他將進入 2 或 3 等臥室的數量。然後這也將保存在 posts_attributes 中。應再次顯示為 Bedrooms: 3。 我如何能夠使用我目前的數據庫架構或其他類型的架構來做到這一點?

文章如何保存在數據庫中$$ post_ad.php $$

請忽略所有變數,假設它們都是文本,除了 $ad_body 是一個數組

               date_default_timezone_set("Asia/Yangon");
               $date = date("Y-m-d h:i:s");
               $stmt = $conn_posts-> prepare("INSERT INTO `posts`(`ad_title`, `ad_sub_cat`, `ad_price`, `used_new`, `for_r_s`,`ad_location`,`ad_poster`, `ad_des`, `ad_date`) VALUES (?,?,?,?,?,?,?,?,?)"); //query
               $stmt->bind_param("sssssssss", $ad_title, $ad_sub_cat,$ad_price,$ad_used_new,$ad_for_r_s,$ad_loc,$_SESSION['user_ID'],$ad_des, $date); // bind_param / insert info
               $stmt->execute(); // execute
               $stmt->store_result(); // store results
               $last_id = $conn_posts->insert_id; //get id of post
               if($stmt->affected_rows == 1){
                   foreach($ad_body as $data){
                       $stmt_atr = $conn_posts-> prepare("INSERT INTO `posts_attributes`(`post_id`,`post_attribute`) VALUES (?,?)"); //query
                       $stmt_atr->bind_param("ss",$last_id,$data); // bind_param / insert info
                       $stmt_atr->execute(); // execute
                       $stmt_atr->store_result(); // store results
                   }
                   if($stmt_atr->affected_rows >= 1){
                       header('location:ad?id='.$last_id.'');
                       exit();
                   }else{
                       $errors[] = "Sorry, there was an error uploading your ad. D:/paf102";
                   }
               }else{
                   $errors[] = "Sorry, there was an error uploading your ad. D:/paa102";
               }

這是我用 Lucidchart 建構的數據庫模式

在此處輸入圖像描述

我知道有很多 PHP,但我最近才看到這個網站,也許它可能更適合我的數據庫問題。如果這個問題與規則無關,我很抱歉,請詢問或告訴我任何事情,我會立即提供資訊或解決我的問題。

您使用的是什麼數據庫系統?…請相應地更新您的問題標籤。

就架構設計而言,如果您的posts_attributes表是您儲存“Volvo”和“3”值的地方,那麼您應該有一個名為的欄位attribute_value來儲存它並且該post_attribute欄位消失。然後,您也可以添加一個attribute_title欄位,該欄位可以儲存“品牌”和“臥室”等內容。

雖然你應該有一個唯一列表的標準化表,attribute_titles並且只將id標題本身儲存在你的posts_attributes表中。在這種情況下,posts_attributes表中的欄位實際上將被呼叫attribute_title_id

引用自:https://dba.stackexchange.com/questions/301550