0

EDIT:

Im trying to submit a form with a title and body but i want the title to go to one table and body to go to another table, this in itself i can do but i need the ID generated from the title being inserted into its table to then be inserted into a field in the table the body is inserted so as to keep them linked.

What i have so far: I know its not pretty and its not safe, i will be reworking them once i learn how to do it properly.

if (@$_POST['post'])
{

$body = @$_POST['body'];     
$title = @$_POST['title'];
$BoardID = @$_POST['BoardID'];
$MemberID = @$_POST['MemberID'];

$date = date("Y-m-d H:i:s");     

include ('connect.php');    

$insert =  mysql_query("INSERT INTO threads VALUES ('','$BoardID','$title','$date','$MemberID','','')");


if($insert) {
    header("location: ?p=posts&thread=$Thread_ID");
    exit();
}

}

I need to somehow get $Thread_ID which has been generated in the insert and add that to a second insert for adding body to the post table, if that makes sense.

I tried getting the latest $Thread_ID and adding +1 but if multiple threads are posted at once they might get crossed over.

How would i go about fixing this?

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • 3
    _"How would i go about fixing this?"_ start by not using `@` to suppress warnings/errors that _Are there to help_, like the `E_DEPRECATED` notice when using `mysql_*` functions. _DEPRECATED_ isn't meaningless, you know... – Elias Van Ootegem Jul 08 '13 at 08:54
  • 2
    Although deprecated, you can look at the [mysql_insert_id](http://php.net/manual/en/function.mysql-insert-id.php)-function – Marty McVry Jul 08 '13 at 08:54
  • 3
    why suppressing warning for `$_POST` ? also, stop using deprecated `mysql_*` functions. use MySQLi or PDO instead. – Raptor Jul 08 '13 at 08:55
  • Bobby Tables strikes again! http://xkcd.com/327/ – GordonM Jul 08 '13 at 08:55
  • 1
    You have 2 queries, but you execute the last one only. Logic issues. – Raptor Jul 08 '13 at 08:55
  • 2
    possible duplicate of [PHP: how to get last inserted ID of a table?](http://stackoverflow.com/questions/1685860/php-how-to-get-last-inserted-id-of-a-table) – Elias Van Ootegem Jul 08 '13 at 08:56
  • Possible duplication of http://stackoverflow.com/questions/13905381/2-table-interaction-insert-get-result-insert?rq=1 – Kaffee Jul 08 '13 at 08:56
  • i dont know why i had the @ there i was getting no errors – user2558771 Jul 08 '13 at 09:06
  • _" I know its not pretty and its not safe, i will be reworking them once i learn how to do it properly"_, if you know the code is no good, why not learn how to do it properly, before writing something bad? You're basically saying: _"I know my leg is broken, but I'll use a band-aid until I know where to find a cast"_ – Elias Van Ootegem Jul 08 '13 at 09:19
  • You should not rely on LatestID+1 as it might end up with cross over. Insert thread and use insert_id to put data in post table. – Amir Bilal Jul 08 '13 at 09:56

2 Answers2

1

The PHP manual tell us:

This extension Mysql is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. (see ref.)

You must use mysqli or PDO, to make a connection between PHP and a MySQL database.

mysqli

If you want the id of the inserted row, you can use $mysqli->insert_id (ref)

Example:

$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);

printf ("New Record has id %d.\n", $mysqli->insert_id);

PDO

If you want the id of the inserted row, you can use $dbh->lastInsertId(); (ref)

And don't forget to sanatize all your inputs.

Community
  • 1
  • 1
Lan
  • 709
  • 1
  • 8
  • 16
-2

You need to execute both insert queries separately.

$insert = "INSERT INTO threads VALUES ('','$BoardID','$title','$date','$MemberID','','')";

$result = @mysql_query($insert);  
$Thread_ID=@mysql_insert_id();

$insert = "INSERT INTO posts VALUES ('','$BoardID',$Thread_ID','$body','$date','$MemberID')";   

$result = @mysql_query($insert);  

Thanks,