2014-05-10 35 views
0

我想創建一個創建標記系統,並以某種方式創建它,但我認爲有更好的方法來做到這一點!
進出口以下雅各布·埃利斯建議這裏給出Recommended SQL database design for tags or tagging更好的查詢添加或實現標籤或標記

我的PHP代碼

<?php 
    //checks to see if form is submitted or not 
    if(isset($_POST['submit']) && isset($_POST['title']) && isset($_POST['short']) && isset($_POST['detail']) && isset($_FILES['cover']) && isset($_POST['secret']) && isset($_POST['bulb'])) : 
     //checks the required field 
     if (!empty($_POST['submit']) && !empty($_POST['title']) && !empty($_POST['short']) && !empty($_POST['detail']) && !empty($_FILES['cover']['name']) && ($_POST['secret'] === '****secret****') && is_array($_POST['bulb'])) { 
       $title = $_POST['title']; 
       $short = $_POST['short']; 
       $detail = $_POST['detail']; 
       $bulb = $_POST['bulb']; //Array Checkbox -> Its a checkbox 

      //Time Processing 
        date_default_timezone_set("Asia/Calcutta"); //Time Zone 
        $timestamp = time(); //Current Time in seconds 
       $date = strftime("%Y-%m-%d", $timestamp); //Current time 
       $time = strftime("%H:%M:%S", $timestamp); //Current date 
      //Time Processing Ends 

      //Image Processing 
        $cover = $_FILES['cover']['name']; 
        $cover_tmp_name = $_FILES['cover']['tmp_name']; 
        $cover_format = substr($cover , -4); 
       $cover_name = md5($cover).'.'.$cover_format; 
       $cover_img_path = '../images/cover/'; 
      //Image Processing Ends 

       if (($cover_type == 'image/jpeg' || $cover_type == 'image/gif' || $cover_type == 'image/png')) { 

        $query_for_news_insert = "INSERT INTO `news` (
             `id` , 
             `title` , 
             `short_line` , 
             `detail` , 
             `cover` , 
             `date` , 
             `time` 
             ) 
             VALUES (
             NULL , 
             '".mysql_real_escape_string($title)."', 
             '".mysql_real_escape_string($short)."', 
             '".mysql_real_escape_string($detail)."', 
             '".mysql_real_escape_string($cover_name)."', 
             '".mysql_real_escape_string($date)."', 
             '".mysql_real_escape_string($time)."' 
             ); 
             "; 

        $run_query_for_news_insert = mysql_query($query_for_news_insert); 

    /************** Step 1 of 3 for inserting tags **************************/ 
        $query_to_get_id_of_news_inserted = "SELECT `id` 
                 FROM `news` 
                 WHERE `date` = '".mysql_real_escape_string($date)."' 
                 AND `time` = '".mysql_real_escape_string($time)."'"; 

        $run_query_to_get_id_of_news_inserted = mysql_query($query_to_get_id_of_news_inserted); 
        $returned_id = mysql_fetch_assoc($run_query_to_get_id_of_news_inserted); 
       $news_id = array_shift($returned_id); //News id which will be used in step 3 


    /************** Step 2 of 3 for inserting tags *************************/ 
        foreach ($bulb as $select => $value) : 

         $query_to_get_tag_id = "SELECT `tag_id` 
              FROM `newstag` 
              where `tag_cat` = '".mysql_real_escape_string($value)."'"; 

         $run_query_to_get_tag_id = mysql_query($query_to_get_tag_id); 
         $rows_returned_from_query_to_get_tag_id = mysql_num_rows($run_query_to_get_tag_id); 
         if ($rows_returned_from_query_to_get_tag_id == '1') { 

          $returned_tag_id = mysql_fetch_assoc($run_query_to_get_tag_id); 
       $tag_id = array_shift($returned_tag_id); //Tag id which will be used in step 3 


    /***************   Step 3 of 3 for inserting tags ******************/ 
          $query_to_insert_itemTag = "INSERT INTO `newsitemtag` (
                 `id` , 
                 `tag_id` , 
                 `news_id` 
                 ) 
                 VALUES (
                 NULL , '".mysql_real_escape_string($tag_id)."', '".mysql_real_escape_string($news_id)."' 
                 )"; 
          $run_query_to_insert_itemTag = mysql_query($query_to_insert_itemTag); 

         } 
        endforeach; 

        move_uploaded_file($cover_tmp_name,$cover_img_path.$cover_name); //Transfer file from temp location to permanent location 

        echo 'Success'; 

       } // End of if statement which checks the image type 
     } else { 
      echo 'Failed'; 
     } 

    endif; 
    ?> 

正如你可以看到元素的數組數越多(這裏$燈泡),那麼我的代碼將運行地獄很多關查詢。

+0

您應該更好地檢查上傳文件的文件擴展名,否則可以上傳'.php'文件。 – Gumbo

+0

對於創建日期更好地使用[TIMESTAMP字段](https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html) – lastbyte

+0

@Gumbo瞭解它:) – Sachin

回答

0

加快這你應該嘗試插入選擇

" INSERT INTO `newsitemtag` (`id` , `tag_id` , `news_id`) 
    SELECT NULL , n.id ,".$news_id." 
    FROM `newstag` n where n.tag_cat in ('".join("','",$bulb)."') "; 

當然,你用你的例子做了你應該做衛生

這樣做,你應該看看http://php.net/array_walk

http://php.net/manual/de/function.join.php

0

1)獲取最後一個插入字段的ID使用mysql_insert_id

2)然後「代碼添加標籤」將是這樣的;

<?php 
    /* code */ 
    if (mysql_query($query_for_news_insert)) { 
    $nid = mysql_insert_id(); 
    $tags = $_POST['bulb']; 
    if (!empty($tags)) { 
     # get all avalible tags 
     # Don't use subqueries bcz all tags can be fake 
     $query = 'SELECT `tag_id` WHERE `tag_cat` IN ('. implode(',', $tags) .')'; 
     $res = mysql_query($query); 
     $values = array(); 
     while ($row = mysql_fetch_array($res, MYSQL_NUM)) { 
     $values[] = '('. $row[0] .', '. $nid .')'; 
     } 

     if (!empty($values)) { 
     # don't use NULL and `id` if `id` is auto-increment field 
     $query = 'INSERT INTO `newsitemtag` (`tag_id`, `news_id`) VALUES ('. implode(',', $values) .')'; 
     mysql_query($query); 
     } 
    } 
    } 
?> 
+0

你不覺得添加在HTML端的標籤ID會有點不安全? – Sachin

+0

您對複選框使用複選框。如果您使用複選框,單選按鈕或選擇,爲什麼不使用真實ID?這是正確的。 – lastbyte

+0

它不是關於使用真實身份。它關於將這些值傳遞給db而不驗證標記id實際存在。 – Sachin