inserting to multiple tables and giving same id's in both table? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

inserting to multiple tables and giving same id's in both table?

i am trying to insert to multiple table using single registration form in android. and want it to insert same id's to both tables but it's taking different id's, can someone tell me what should i do for this? My code ---public function createUser($name, $surname, $username, $user_pass, $address, $pin, $mail, $phone, $a, $b, $c, $d){ if($this->isUserExist($username,$mail,$phone)){ return 0; }else{ $password = md5($user_pass); $stmt = $this->con->prepare("INSERT INTO `user_data` (`name`, `surname`, `username`, `password`, `address`, `pin`, `mail`, `phone`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"); $stmt->bind_Param("ssssssss",$name,$surname,$username,$password,$address,$pin,$mail,$phone); if(!$stmt->execute()){ return 2; } $stmtcate = $this->con->prepare("INSERT INTO `employee_category` (`name`, `pin`, `a`, `b`, `c`, `d`) VALUES (?, ?, ?, ?, ?, ?);"); $stmtcate->bind_Param("ssssss",$name,$pin,$a,$b,$c,$d); if ($stmtcate->execute()){ return 1; }else{ return 2; } } }-----

20th Mar 2017, 1:51 PM
SAMEER RAJPAL
SAMEER RAJPAL - avatar
2 Answers
+ 1
@Devender Mahajan... it didn't work for me... anyways i have found a solution for it yesterday and i'd like to share it. My Code "pid" is the column where i am inserting my last id. ---public function testReg($name, $pin, $a, $b, $ho, $ll, $c, $d){ $stmt = $this->con->prepare("INSERT INTO `test` (`name`, `pin`) VALUES (?, ?);"); $stmt->bind_Param("ss",$name,$pin); if(!$stmt->execute()){ return 2; }else{ $lastid=mysqli_insert_id($this->con); $stmttst = $this->con->prepare("INSERT INTO `test_category` (`pid`, `name`, `a`, `b`, `ho`, `ll`, `c`, `d`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"); $stmttst->bind_Param("isssssss",$lastid,$name,$a,$b,$ho,$ll,$c,$d); if ($stmttst->execute()){ return 1; }else{ return 2; } } }---
23rd Mar 2017, 6:25 AM
SAMEER RAJPAL
SAMEER RAJPAL - avatar
- 1
As I could understand your problem. 'id' columns in both tables are auto incremental. In order to solve this situation, do following things 1. Make 'id' column non incremental for. employee_category 2. After insert operation on users table, capture last insert id. $uid = $stmt->insert_id 3. Now pass that value in insert command on employee_category prepare("INSERT INTO `employee_category` (`id`,`name`, `pin`, `a`, `b`, `c`, `d`) VALUES (?,?, ?, ?, ?, ?, ?);");
22nd Mar 2017, 3:22 PM
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender)
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender) - avatar