PHP unknown column in field list but table exists

Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'people.addressId' in 'field list' in A:xampphtdocsbrgytoolkitcensus2.php:97 Stack trace: #0 A:xampphtdocsbrgytoolkitcensus2.php(97): PDOStatement->execute() #1 {main} thrown in A:xampphtdocsbrgytoolkitcensus2.php on line 97

I have this query that takes four inputs from the form and derives the other values from supposed to be existing columns in the rest of the database. However, it does not seem to read people.addressId even if it does exist. The query works fine when executed on MySQL itself. I tried using single quotes around it instead of backticks but the same happens. I also tried using LEFT JOIN but nothing changes.

$incomeMonthly = $_POST['incomeMonthly'];
$isIncomeSourceSalary = $_POST['isIncomeSourceSalary'];
$isIncomeSourceBusiness = $_POST['isIncomeSourceBusiness'];
$isIncomeSourceRemittance = $_POST['isIncomeSourceRemittance'];

$sql = "INSERT INTO `households` (`incomeMonthly`, `isIncomeSourceBusiness`, `isIncomeSourceSalary`,`isIncomeSourceRemittance`, `addressId`, `personId`, `householdName`, `householdHeadName`)
    SELECT (:a,:b,:c,:d, `people.addressId` , `personId` , `lName` ,`fName`)
    FROM `people` INNER JOIN `addresses` ON `people.addressId` = `addresses.addressId`
    WHERE `isHeadOfFamily`=1 AND `barangay`='San Jose'";
$stmt = $db->prepare($sql);
$stmt->bindParam(":a", $incomeMonthly);
$stmt->bindParam(":b", $isIncomeSourceBusiness);
$stmt->bindParam(":c", $isIncomeSourceSalary);
$stmt->bindParam(":d", $isIncomeSourceRemittance);
$stmt->execute();

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

  1. The backticks on `people.addressId` should be `people`.`addressId`.
    Same goes for `addresses`.`addressId`.
  2. There’s no need to add parentheses [()] on SELECT section so SELECT (:a,:b,:c,:d, `people.addressId` , `personId` , `lName` ,`fName`) is suppose to be just SELECT :a,:b,:c,:d, `people`.`addressId` , `personId` , `lName` ,`fName`

So the updated code should be:

$incomeMonthly = $_POST['incomeMonthly'];
$isIncomeSourceSalary = $_POST['isIncomeSourceSalary'];
$isIncomeSourceBusiness = $_POST['isIncomeSourceBusiness'];
$isIncomeSourceRemittance = $_POST['isIncomeSourceRemittance'];

$sql = "INSERT INTO `households` (`incomeMonthly`, `isIncomeSourceBusiness`, `isIncomeSourceSalary`,`isIncomeSourceRemittance`, `addressId`, `personId`, `householdName`, `householdHeadName`)
    SELECT :a,:b,:c,:d, `people`.`addressId` , `personId` , `lName` ,`fName`
    FROM `people` 
      INNER JOIN `addresses` ON `people`.`addressId` = `addresses`.`addressId`
    WHERE `isHeadOfFamily`=1 AND `barangay`='San Jose'";
$stmt = $db->prepare($sql);
$stmt->bindParam(":a", $incomeMonthly);
$stmt->bindParam(":b", $isIncomeSourceBusiness);
$stmt->bindParam(":c", $isIncomeSourceSalary);
$stmt->bindParam(":d", $isIncomeSourceRemittance);
$stmt->execute();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x