插入多个表格并在两个表格中提供相同的ID

时间:2017-03-13 11:15:51

标签: php mysql

我想在android中使用单一注册表单插入多个表。并且希望它在两个表中插入相同的ID,但是它会使用不同的ID,有人可以告诉我该怎么办?

DbOperations.php



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;
				}
				
			}
		}




1 个答案:

答案 0 :(得分:0)

您不能在一次执行调用中执行两个语句,因此您必须将其拆分为两个语句:

            $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;
            }
            $stmt1 = $this->con->prepare("INSERT INTO `employee_category` (`pid`,`name`, `pin`, `a`, `b`, `c`, `d`) VALUES (?, ?, ?, ?, ?, ?, ?); ");
            $stmt1->bind_Param("issssss",$stmt->insert_id(),$name,$pin,$a,$b,$c,$d);
            if($stmt1->execute()){
                return 1;
            }else{
                return 2;
            }