Jump to content

Archived

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

witt

quick mysql_insert_id question

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.

Share this post


Link to post
Share on other sites
Never mind, I had it right all along. The problem was the I didn't put single quotes on the valuse I wanted to insert.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.