george123
george123

Reputation: 33

Copy value to another table

I have a problem with my code. I have a table called "users" with an "id" field.I want to copy the id value to another table called "aircondition" . This is the code that inserts values into the aircondition table .The problem is that when I use this code I get 0 in the new id field instead of the 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);
?>

Upvotes: 0

Views: 104

Answers (1)

juergen d
juergen d

Reputation: 204746

Use this query

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

Upvotes: 2

Related Questions