Jump to content

quick mysql_insert_id question


witt

Recommended Posts

[code]

$query = "INSERT INTO table_one (data) VALUES ('$data')";
$result = mysql_query ($query);

$counter = 3;

    for ($i = 0; $i < $counter; $i++) {
                
        $query = "INSERT INTO table_two (hello, data_id) VALUES ('$hello', '??????')";
        $result = mysql_query ($query);
        
    }

[/code]

So basically the first query would insert $data into table_one under the data coulmn, and insert an incrementing number under the data_id column.

What I want the second query to do is insert $hello into table_two under the hello column, and also insert the data_id generated by the first query into a column named data_id.

I'm not quite sure how to use mysql_insert_id in this case but it should be simple.
Link to comment
https://forums.phpfreaks.com/topic/10661-quick-mysql_insert_id-question/
Share on other sites

The quotes are only mandatory with string values.

[code]$query = "INSERT INTO table_one (data) VALUES ('$data')";
$result = mysql_query ($query);

$id = mysql_insert_id();

$counter = 3;

    for ($i = 0; $i < $counter; $i++) {
                
        $query = "INSERT INTO table_two (hello, data_id) VALUES ('$hello', $id)";
        $result = mysql_query ($query);
        
    }[/code]

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.