-2

I am trying to have an AND in my statement but everytime I run the code I get this error:

PHP Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'company_name' in where clause is ambiguous

select tblcomplaints.*,user.name as name,category.categoryName as
    catname from tblcomplaints join user on user.id=tblcomplaints.userId join category on 
    category.id=tblcomplaints.category where tblcomplaints.complaintNumber=".$cid." AND company_name=$companyname`
Mureinik
  • 297,002
  • 52
  • 306
  • 350
yobra89
  • 19
  • 6
  • 2
    Edit your post to add the **structure** of your **tables** to better understand your error – Atika Sep 13 '21 at 14:13

1 Answers1

4

According to the error, two or more of the tables in the query have a company_name column. You can fix the error by full qualifying the column with a table name. E.g., if it's in the user table:

select tblcomplaints.*, user.name as name, category.categoryName as catname
from   tblcomplaints 
join   user on user.id = tblcomplaints.userId
join   category on category.id = tblcomplaints.category
where  tblcomplaints.complaintNumber = ".$cid." AND 
       user.company_name = $companyname
-- Here^

Side note:
Concatenating strings into SQL statements like this may make your application vulnerable to SQL Injection attacks. I strongly recommend you look into prepared statements as a safer approach.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    yes table content has and user have both collumn company_name im now getting another error PHP Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column – yobra89 Sep 13 '21 at 14:10
  • 2
    That error goes on to say WHICH COLUMN is unknown! So which column is it – RiggsFolly Sep 13 '21 at 14:14
  • silly me i had not declared that column in the user table .. thank you so much for your time – yobra89 Sep 21 '21 at 13:38