php 如何将数据从SELECT发送到INSERT

x759pob2  于 2022-12-02  发布在  PHP
关注(0)|答案(1)|浏览(160)

我不知道如何在INSERT查询中放置SELECT $device_dep。

$fprint_id = (int)$_GET['fprint_id'];
    $device_uid = $_GET ['device_uid'];
    $serialnumber = (int)$_GET['serialnumber'];

    $stmtdevice_dep = $conn->prepare("SELECT device_dep FROM devices WHERE device_uid = '$device_uid' LIMIT 1");
    //*PHP magic*
    $sqlINSERTquery = "INSERT INTO users (fprint_id, serialnumber, user_date, device_uid, device_dep, del_f, add_f) 
        VALUES ($fprint_id, $serialnumber, CURDATE(), $device_uid, $device_dep, 0, 1 LIMIT 1)";


    $db->query($sqlINSERTquery);

MySQL结构

CREATE TABLE `devices` (
  `id` int(11) NOT NULL,
  `device_name` varchar(50) CHARACTER SET latin1 NOT NULL,
  `device_dep` varchar(20) CHARACTER SET latin1 NOT NULL,
  `device_uid` text CHARACTER SET latin1 NOT NULL,
  `device_date` date NOT NULL,
  `device_mode` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

我试过这个:

$stmtdevice_dep  = $conn->prepare("SELECT device_dep FROM devices WHERE device_uid = '$device_uid'");
    $stmtdevice_dep->execute();
    $resultdevice_dep = $stmtdevice_dep->get_result()->fetch_all();

    print_r($resultdevice_dep);

返回错误-致命错误:未捕获的错误:对中的bool调用成员函数execute()

**打印_r($stmtdevice_dep);**退回:

mysqli_stmt Object
(
    [affected_rows] => 0
    [insert_id] => 0
    [num_rows] => 0
    [param_count] => 0
    [field_count] => 1
    [errno] => 0
    [error] => 
    [error_list] => Array
        (
        )

    [sqlstate] => 00000
    [id] => 1
)
bd1hkmkf

bd1hkmkf1#

您可以使用$stmtdevice_dep['device_dep']从sql结果中获取值device_dep**(如果sql查询以对象格式返回响应)**,也可以在插入sql查询中直接使用

相关问题