Home > OS >  PHP unknown column in field list but table exists
PHP unknown column in field list but table exists

Time:10-21

Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'people.addressId' in 'field list' in A:\xampp\htdocs\brgytoolkit\census2.php:97 Stack trace: #0 A:\xampp\htdocs\brgytoolkit\census2.php(97): PDOStatement->execute() #1 {main} thrown in A:\xampp\htdocs\brgytoolkit\census2.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();

CodePudding user response:

  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();
  • Related