Jump to content

[SOLVED] Updating a non-auto-increment column without a $_GET value


Goldeneye

Recommended Posts

So I'm writing a forum and in the `messages` table there's the `topicid` field which is not auto-incremented which is used by create.php?node=topic&board=*. So how would get the auto-incremented `topicid` from the `topics` table to the `messages table? Is there anyway to do it without a SELECT query?

 

<?php
if($_GET['node'] == 'topic'){
	if($_GET['board'] == $boardnum){
		gHeader('Create a Topic');
		if (isset($_POST['createtopic'])) {
			if(empty($_POST['message'])) {
				echo 'You missed a field..<br><br>';
			} else {
				mysql_query("INSERT INTO `boardmessages` (`topicid`, `message`, `from`, `userid`, `deleted`, `ip`, `time`) VALUES ('`topicid`+1', '" . $_POST['message'] . "', '" . $namerow['username'] . "', '" . $_SESSION['user_id'] . "', 0, '" . $_SERVER['REMOTE_ADDR'] . "', '" . time() . "')");
				mysql_query("INSERT INTO `boardtopics` (`topicid`, `title`, `creator`, `lastpost`, `created`, `userid`, `board`, `pinned`, `closed`, `archived`, `favorites`, `messages`) VALUES ('`topicid`+1', '" . $_POST['title'] . "', '" . $namerow['username'] . "', '" . time() . "', '" . time() . "', '" . $_SESSION['user_id'] . "', '" . $boardnum . "', 0, 0, 0, 0, 1)") || die(mysql_error());
				mysql_query("UPDATE `boards` SET `messages`=`messages`+1, `topics`=`topics`+1, `lastpost`=" . time() . " WHERE `boardnum`=" . $boardnum . "");
				echo 'THanks for posting! <a href="topiclist.php?board=' . $boardnum . '">Return</a> to the board.<br><br>';
			}
		}

		if (!isset($_POST['createtopic'])){
			echo '<ul class="subnavul"><li class="subnavli"><a href="boardlist.php">Board List</a></li>';
			echo '<li class="subnavli"><a href="topiclist.php?board=' . $boardnum . '" target="_blank"><b>Topic List</b></a></li>';
			echo '</ul>';
			echo '<form action="?node=topic&board=' . $boardnum . '" method="POST">';
			echo 'Title:<br><input type="type" name="title" class="textfieldlong"><br><br>';
			echo 'Message:<br><textarea rows="5" name="message" class="inputbox"></textarea><br>';
			echo '<input type="hidden" name="topid">';
			echo '<input type="submit" value="Create Topic" name="createtopic"  class="button">';
			echo '</form>';
		}
		gFooter();
	}
}
?>

 

A preemptive thanks...

Thanks to you DarkWater, it worked!

I was looking at another way of doing it on someone else's script, but it was much more complex. This will work just fine.

 

To BlueSky:

The `topicid` in `boardtopics` is auto-incremented but the `topicid` in `boardmessages` isn't; and I was looking to update the `topicid` in `boardmessages` with the same `topicid` that was inserted into `boardtopics` via an INSERT query.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.