//create and issue the first query
    $add_topic_sql = "INSERT INTO forum_topics (topic_title, topic_create_time, topic_owner) VALUES ('$_POST[topic_title]',now(),'$_POST[topic_owner]')";
	$add_topic_res = mysql_query($conn, $add_topic_sql)
	or die(mysql_error($conn));
			//get the id of the last query
			$topic_id = mysql_insert_id($mysql);

Recommended Answers

All 4 Replies

What is the problem?

One thing that you could do to make the code better is instead of using the POST array in the actual query, you could clean it up and store it in another variable first, something like:

$topic_owner = mysqli_real_escape_string($conn, trim($_POST['topic_owner']));
//create and issue the first query
    $add_topic_sql = "INSERT INTO forum_topics (topic_title, topic_create_time, topic_owner) VALUES ('$_POST[topic_title]',now(),'$_POST[topic_owner]')";
	$add_topic_res = mysql_query($conn, $add_topic_sql)
	or die(mysql_error($conn));
			//get the id of the last query
			$topic_id = mysql_insert_id($mysql);

You messed up with parameters of the mysql_query(), it should have been -

mysql_query($add_topic_sql, $conn)

From what I can tell, network18's got it.

$topic_id = mysql_insert_id($mysql);

is wrong too.mysql_insert_id() expects the MySQL link parameter.
It should have been like -

$topic_id = mysql_insert_id($conn);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.