Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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和mysql从表中捕获ID_Php_Mysql - Fatal编程技术网

如何使用php和mysql从表中捕获ID

如何使用php和mysql从表中捕获ID,php,mysql,Php,Mysql,我的问题是: 如何使用php和mysql中准备的语句捕获表的ID,以分配通过insert捕获并添加的ID作为下表的外键 基本上,我有两个表'Cliente'和'Direccion',所以我想要Cliente的ID(pk)并将该ID(在第一次查询之后)放入Direccion的ID(fk)中,因为这两个表都是相关的 当我尝试在我的页面中运行表单时,出现以下消息错误: 致命错误:未捕获错误:在 C:\xampp\htdocs\compra\compra\regstro\formregstro_reg.

我的问题是:

如何使用php和mysql中准备的语句捕获表的ID,以分配通过insert捕获并添加的ID作为下表的外键

基本上,我有两个表'Cliente'和'Direccion',所以我想要Cliente的ID(pk)并将该ID(在第一次查询之后)放入Direccion的ID(fk)中,因为这两个表都是相关的

当我尝试在我的页面中运行表单时,出现以下消息错误:

致命错误:未捕获错误:在
C:\xampp\htdocs\compra\compra\regstro\formregstro_reg.php:31堆栈跟踪:#0{main}抛出
第31行的C:\xampp\htdocs\compra\compra\registro\FormRegistro_reg.php

代码如下:

<?php

//Table Cliente
$nom = $_POST["nombre"];
$ape = $_POST["apellido"];
$mail = $_POST["correo"];
$obs = $_POST["observacion"];
$con = $_POST["contrasena"];
$rut = $_POST["rut"];
$fono = $_POST["num_telefono"];

// Table Dirección
$calle = $_POST["calle"];
$num = $_POST["num_calle"];
$com = $_POST["comuna"];
$reg = $_POST["region"];



require('otraconexion.php');


    //Query Table Cliente

    $sql = "INSERT INTO cliente VALUES (NULL,?,?,?,?,?,?,?)";
    $resultado = mysqli_prepare($conexion, $sql);


$ok = mysqli_stmt_bind_param($resultado,"ssssisi",$nom,$ape,$mail,$obs,$con,$rut,$fono);
$ok = mysqli_stmt_execute($resultado);
$fk = $sql->mysqli_insert_id();

//Query Table Dirección

$sql2 = "INSERT INTO direccion VALUES (NULL,?,?,?,?,?)";
$resultado2 = mysqli_prepare($conexion, $sql2);

if(!$resultado2){
echo "Error al insertar.".mysqli_error($conexion);
}

$ok = mysqli_stmt_bind_param($resultado2,"sissi",$calle,$num,$com,$reg,$fk);
$ok = mysqli_stmt_execute($resultado2);



if($ok==false){

    echo"Error,";
}else{

    header("Location: ../Acceso/Indexp.php");

}

mysqli_stmt_close($resultado);


?>


我看不出你的代码中有任何问题。你说得对,我忘记发布问题了,谢谢
$sql
var是一个字符串,而不是一个具有
mysql\u insert\u id
方法的对象。您只需删除
$sql->
部分,就可以正确调用
mysqli\u insert\u id
函数,不要忘记传递连接参数。您需要
$conexion->insert\u id
$ok->insert\u id
,而不是
$sql->mysqli\u insert\u id()
。看到或看到。投票结束是一个打字错误