我正在尝试创建一个标记系统,不知怎的,我创建了它,但我认为有更好的方法来做到这一点!
我跟随Yaakov Ellis的建议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;
?>
正如你所看到的那样,数组中的元素数量越大(这里是$ bulb),那么我的代码将会运行很多查询。
答案 0 :(得分: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
答案 1 :(得分: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);
}
}
}
?>