gpt4 book ai didi

php - 将值复制到另一个表

转载 作者:行者123 更新时间:2023-11-29 07:54:10 25 4
gpt4 key购买 nike

我的代码有问题。我有一个名为“users”的表,其中有一个“id”字段。我想将 id 值复制到另一个名为“aircondition”的表。这是将值插入到空调表中的代码。问题是,当我使用此代码时,我在新的 id 字段中得到 0 而不是 user.id

 <?php
$con=mysqli_connect("localhost","george","george123","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$acname = mysqli_real_escape_string($con, $_POST['ACName']);
$btu = mysqli_real_escape_string($con, $_POST['BTU']);
$space = mysqli_real_escape_string($con, $_POST['Space']);
$energyclass = mysqli_real_escape_string($con, $_POST['EnergyClass']);



$sql="INSERT INTO aircondition (id, ACName, BTU, Space, EnergyClass)
VALUES ('SELECT id
FROM users', '$acname', '$btu', '$space', '$energyclass')";

if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
header('location:aircondition.php');


mysqli_close($con);
?>

最佳答案

使用此查询

INSERT INTO aircondition (id, ACName, BTU, Space,  EnergyClass)
SELECT id, '$acname', '$btu', '$space', '$energyclass'
FROM users

关于php - 将值复制到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25592817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com