Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 一次将数据插入两个表中_Php_Mysql_Phpmyadmin - Fatal编程技术网

Php 一次将数据插入两个表中

Php 一次将数据插入两个表中,php,mysql,phpmyadmin,Php,Mysql,Phpmyadmin,我想知道如何将从HTML表单检索到的值插入到两个表loginDetails和memberDetails中 登录详细信息(代码中显示了表1) loginID(PK)您可以将查询放入数组中。在数组中循环。如果发生错误,请退出脚本 $myQueries = array(`"INSERT INTO $table_1 VALUES (NULL, '".$formValue['username'].", '".$formValue['password']."')", "INSERT INTO $tabl

我想知道如何将从HTML表单检索到的值插入到两个表loginDetails和memberDetails中

登录详细信息(代码中显示了表1)


loginID(PK)您可以将查询放入数组中。在数组中循环。如果发生错误,请退出脚本

$myQueries = array(`"INSERT INTO $table_1 VALUES (NULL,
'".$formValue['username'].",
'".$formValue['password']."')",
  "INSERT INTO $table_2 VALUES (NULL,
    '".$lastID."')"
  )`;

 for($i = 0; $i < count($myQueries); $i++){
     if (mysqli_query($Link, $myQueries[$i])) {
           $lastID = mysql_insert_id();
            $message = "You've sucessfully created the account!";
            echo json_encode(array('success'=>'true', 
             'action'=>'login',
              'html'=>$message, 
              'console.log'=>$Query));
    }
    else {
        $message = "Error occur in query[$i]";
        echo json_encode(array('action'=>'error',
        'html'=>$message,                   
       'console.log'=>$Query));
        exit; // stops the next query
     }
    }
 }
$myquerys=array(`)插入$table_1值(NULL,
“$formValue['username']”,
“$formValue['password']。”)”,
“插入$table_2值(空,
“$lastID。”)”
)`;
对于($i=0;$i'true',
'操作'=>'登录',
'html'=>$message,
'console.log'=>$Query));
}
否则{
$message=“查询中出现错误[$i]”;
echo json_编码(数组('action'=>'error',
'html'=>$message,
'console.log'=>$Query));
exit;//停止下一个查询
}
}
}

使用
mysqli\u insert\u id($link)
而不是
mysql\u insert\u id
@MuntashirAkon我已经尝试过这个方法,但不起作用:(@RyanVincent是的,因为稍后我将插入另一个表,即'staffDetails',该表还包含loginID,它是与loginDetails关联的外键table@RyanVincentloginDetails是父对象,而成员和员工是子对象loginDetails@RyanVincent对此有什么建议吗?
$myQueries = array(`"INSERT INTO $table_1 VALUES (NULL,
'".$formValue['username'].",
'".$formValue['password']."')",
  "INSERT INTO $table_2 VALUES (NULL,
    '".$lastID."')"
  )`;

 for($i = 0; $i < count($myQueries); $i++){
     if (mysqli_query($Link, $myQueries[$i])) {
           $lastID = mysql_insert_id();
            $message = "You've sucessfully created the account!";
            echo json_encode(array('success'=>'true', 
             'action'=>'login',
              'html'=>$message, 
              'console.log'=>$Query));
    }
    else {
        $message = "Error occur in query[$i]";
        echo json_encode(array('action'=>'error',
        'html'=>$message,                   
       'console.log'=>$Query));
        exit; // stops the next query
     }
    }
 }