开发者

PHP: use id to another column

Ok, so i have a normal query that inserts to the database.

mysql_query("INSERT INTO users_pm_in (uID, msg) VALUES ('$uID', '$msg')");

Now this table has also a 开发者_如何学编程column called "id" with auto_increment & primary key.

When it inserts it auto makes number for the column in the row. Now I want this number, and put it in column dialog, in the same row. So the inserted row have the same number/id in "id" and "dialog". How can i do that?


Not sure if this can be done in one query (or why you even want to do this), but you can use this:

mysql_query("INSERT INTO users_pm_in (uID, msg) VALUES ('$uID', '$msg')");
mysql_query("UPDATE users_pm_in SET dialog = id WHERE id = '".mysql_insert_id()."');

Be sure to escape the variables properly also.


I think it would be easier to remove the autoincrement and add the id+dialog value yourself.


Check out mysql_insert_id()


You can do this, altough it's not very efficient...

Supose you have this table:

CREATE TABLE `test` (
    `id` INT(10) NOT NULL AUTO_INCREMENT,
    `a` INT(10) NULL DEFAULT '0',
    `b` INT(10) NULL DEFAULT '0',
    PRIMARY KEY (`id`)
)
ENGINE=MyISAM
ROW_FORMAT=DEFAULT

You can perform the following query:

INSERT INTO test (a, b) SElECT IFNULL((MAX(id) +1),1), 200 FROM test;

Notice that "200" is some random value that will be inserted on "b" column.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜