将值复制到另一个表

时间:2014-08-31 14:15:24

标签: php mysql

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

 <?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);
?>

1 个答案:

答案 0 :(得分:2)

使用此查询

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