MyBB Community Forums

Full Version: Help with automatically creating a thread (through a script)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using the following function to create threads on the forum automatically. The problem is that the line "$tid = $db->insert_id();" doesn't return the correct tid. For whatever reason it always returns a number around 10-14 instead of the actual thread id. As a result when I create the post that goes in the thread it has the wrong tid as well. Does anyone know how to fix this? (btw the I got the code from dennistt's report to forum plugin and it seems to work fine there).

function videos_create_thread()
{
	global $mybb, $db, $post, $thread, $pid, $forum, $templates, $cache;

	$message = "message";
	
	$newthread = array(
		"fid" => 36,
		"subject" => addslashes("Testing Video Library Thread Creation"),
		"icon" => -1,
		"uid" => $mybb->user['uid'],
		"username" => addslashes(htmlspecialchars_uni($mybb->user['username'])),
		"dateline" => time(),
		"lastpost" => time(),
		"lastposter" => addslashes(htmlspecialchars_uni($mybb->user['username'])),
		"views" => 0,
		"replies" => 0,
		"visible" => 1
		);
	$db->insert_query("mybb_threads", $newthread);
	$tid = $db->insert_id();  //PROBLEM LINE
	print "<br> tid: $tid <br>";
	$newpost = array(
		"tid" => $tid,
		"fid" => 36,
		"subject" => addslashes("Testing Video Library Thread Creation"),
		"icon" => -1,
		"uid" => $mybb->user['uid'],
		"username" => addslashes(htmlspecialchars_uni($mybb->user['username'])),
		"dateline" => time(),
		"message" => addslashes($message),
		"ipaddress" => getip(),
		"includesig" => "no",
		"smilieoff" => "no",
		"visible" => 1
		);
	print_r($newpost);
	$db->insert_query("mybb_posts", $newpost);
	$pid = $db->insert_id();
	print "<br> pid: $pid";
	$firstpostup = array("firstpost" => $pid);
	$db->update_query("mybb_threads", $firstpostup, "tid='$tid'");
	$parents = explode(",", $forum['parentlist']);
	foreach($parents as $parentfid)
	{
		updateforumcount($parentfid);
	}
	$cache->updatestats();
	updatethreadcount($tid);
}

Thank you for any advice!
I took a good look at the code and I couldn't spot any problems.

Have you confirmed that threads are actually being created? Also, can you give any more details about the value of $tid? Is it always just a random number between 10 and 14?
Just figured it out =) Because my script uses it's own database the problem was that the last used DB link was for my database and not the mybb database. You can fix it by changing
$tid = $db->insert_id();  //PROBLEM LINE
to

$tid = $db->insert_id($db->link);

Thank you for your help. Hope this helps someone out.