LookAtMeNow
LookAtMeNow

Reputation: 261

Php Mysql - Get value auto increment before and insert it to another column

I have a table like

CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(60) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 ;

I want insert some value to name column like

id|name
1 |1_myvalue
2 |2_myvalue
...

This table has multi users can insert to that. What can I do to make that thanks

$r = mysqli_query($conn, "INSERT INTO `test`(`name`) VALUES ('_myvalue')");

Upvotes: 2

Views: 6148

Answers (1)

Hanky Panky
Hanky Panky

Reputation: 46900

Simple workaround:
Save the record, get the inserted record's id, update it to the proper value

$r = mysqli_query($conn, "INSERT INTO `test`(`name`) VALUES ('myvalue')");
//1. save the record

$id=mysqli_insert_id($conn));
//2.  get it's id

mysqli_query($conn, "UPDATE `test` set `name` = $id"."'_myvalue' where id=$id limit 1");
//3. update the record appending correct id to the value as desired

Upvotes: 4

Related Questions