MyBB Community Forums

Full Version: PHP update query?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can anybody help me with this code!

I am fetching and showing the thread's tags in the edit post but how to update the existing tags on submit since the table alredy contains old tags I juts wanna replece them with new!



function systemtags_insert_tags_thread(&$thread)
{
	global $mybb, $db;
	if($mybb->input['tags'])
	{
		$stags = strip_tags($mybb->input['tags']);
		$tagsht = htmlspecialchars_uni($stags);
		$searchcodes = explode(",", $mybb->input['systemtag_disablecodes']);
		$rtags = str_replace($searchcodes, " ", $tagsht);
		$rtagsp_br = preg_replace("/\r\n|\r/", " ", $rtags);
		$tags = trim($rtagsp_br);
		$explode = explode(",", $tags);
		$array = array();
		foreach($explode as $tag)
		{
			if(strlen($tag) >= 3)
			{	
				$array[] = $tag;
			}
		}
		$insert = implode(", ",$array);
		$insert = $db->escape_string($insert);
		$inserttags = array(
			"tid" => $thread->tid,
			"tags" => $insert
		);	
		$aid = $db->insert_id();
		$db->insert_query("tags", $inserttags);
	}
	
}

function systemtags_validate_tags(&$thread)
{
	global $mybb,$posthandler, $lang;
	$lang->load("tags");
	
     if (!$mybb->input['tags'])
	{
		$posthandler->set_error($lang->notags);
		return false;
	}
	
	if($mybb->input['tags'])
	{
		$stags = strip_tags($mybb->input['tags']);
		$tagsht = htmlspecialchars_uni($stags);
		$searchcodes = explode(",", $mybb->settings['systemtag_disablecodes']);
		$rtags = str_replace($searchcodes, " ", $tagsht);	
		$tags = preg_replace('/\s*,\s*/', ',', $rtags);
		$tags = rtrim($tags, ", \t\n");
		$explode = explode(",", $tags);
		$tagnum = 0;
		foreach($explode as $tag)
		{
			if(strlen($tag) >= 3)
			{
				++$tagnum;
			}
			else
			{
				$posthandler->set_error($lang->tagsfivecaracters);
				return false;
			}
			if (!preg_match('/^[a-zA-Z0-9 .]+$/', $tag))
			
			{
				$posthandler->set_error($lang->numberslettersonly);
				return false;
			}
          }
       }
    }

function systemtags_postbit_tags(&$post)
{
	global $db, $mybb, $templates, $theme,$postcounter;
	if($postcounter == 1)
	{
		$query = $db->simple_select("tags", "tags", "tid=".$post['tid']);
		$fortag = $db->fetch_array($query);
		
		if(!empty($fortag['tags']))
		{
			$explode = explode(", ",$fortag['tags']);
			$tagspostbit = "<div class='systemtag'>";
			$counttag = 0;
			foreach($explode as $tag)
			{
				$tagspostbit .= "<div class=\"systemtag_element\"><a href=\"tags/".$tag.".html\"><span class=\"smalltext\"><b>".$tag."</b></span></a></div>";
				++$counttag;
			}
			$tagspostbit .= "</div>";
			eval("\$post['tags'] = \"".$templates->get("tags_postbit")."\";");
		}
	}
}




function edit_tags()
{
	global $db, $usertags, $tid;
	
	$query = $db->query("
        SELECT * 
        FROM ".TABLE_PREFIX."tags t
		WHERE t.tid = $tid
		");

while($tag = $db->fetch_array($query))
{
	$usertags = $tag['tags'];
}

}
Well, if you want to replace old tags with new ones, the simpliest way is to delete the old one and then insert the new:
$db->delete_query("tags", "tid=".$thread->tid);
$db->insert_query("tags", $inserttags);

I don't think doing an update query is usefull, because you don't know if there is already tags.

Depending on the structure of the database, you can do a replace query (or "insert or update"), but it will be a custom query, not using the MyBB Database methods, so it will be platform-dependant, not a good idea imho.
You are awesome thanks very much genius Smile

I am sorry where do I have to put your fix?
(2015-03-05, 03:35 PM)Crazycat Wrote: [ -> ]Depending on the structure of the database, you can do a replace query (or "insert or update"), but it will be a custom query, not using the MyBB Database methods

There is $db->replace_query():
https://github.com/mybb/mybb/blob/featur...e.php#L352
Could you edit the code cause I am terrible with PHP please? This is the second day I am with this!
Just replace your previous insert_query with replace_query.

$db->replace_query("tags", $inserttags);

If you use my code, this line must be used as a replacement of the 2 queries (delete and insert)