Getting error while inserting data.. Any php expert out here ?

Discussion in 'PHP' started by Rinki12, Jun 28, 2015.

  1. #1
    I have a php based portal where i am adding question and getting error like this :

    A Database Error Occurred
    Error Number: 1054

    Unknown column 'tblquestion_category.question_category_id' in 'where clause'

    SELECT `question_category`.`question_category_id`, `question_category`.`category_title`, `parent_category`.`category_title` as parent_category_title FROM (`tbl_question_category` AS question_category) LEFT JOIN `tbl_question_category` AS parent_category ON `tblquestion_category`.`parent_category_id`=`parent_category`.`question_category_id` WHERE `tblquestion_category`.`question_category_id` = '3'

    Filename: /home/enkatech/public_html/refitsmart.com/register/models/admin/configuration_model.php

    Line Number: 524
     
    Rinki12, Jun 28, 2015 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #2
    Have you made sure that the column question_category_id exists in tblquestion_category? Perhaps you made a small typo?
     
    Anveto, Jun 28, 2015 IP
  3. Rinki12

    Rinki12 Banned

    Messages:
    120
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    53
    #3
    tblquestion_category is not there in the code
     
    Rinki12, Jun 28, 2015 IP
  4. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #4
    Well then you pasted the wrong code I guess because towards the end you clearly have

    WHERE `tblquestion_category`.`question_category_id` = '3'
    Code (markup):
     
    Anveto, Jun 28, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    You're redeclaring names all over, and you have not declared tblquestion_category anywere - you HAVE declared question_category as an alias for tbl_question_category and parent_category as an alias for the same... Also, there shouldn't be backticks ` around table names, just column names (and strictly speaking they're not really needed around column names either, they're there to make sure that you, if you use restricted words for column names (just don't), then it will still work).
     
    PoPSiCLe, Jun 28, 2015 IP
    Rinki12 likes this.
  6. Rinki12

    Rinki12 Banned

    Messages:
    120
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    53
    #6
    I haven't used this word tblquestion_category anywhere in the code even i checked it doesn't seems to be there.
     
    Rinki12, Jun 28, 2015 IP
  7. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #7
    According to the SQL you posted above, tblquestion_category seems to be a table in your database. The error you are seeing is saying that the column question_category_id does not exist in the table tblquestion_category. The error is related to your SQL query and is not in your php code.

    Also as @PoPSiCLe said, the statement is really messy and looks weird although it should work if the column exists.
     
    Anveto, Jun 28, 2015 IP
    Rinki12 likes this.
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    If you tell us the name of your table(s) and the name of your columns, we could probably create a correct query for you, if you explain what it is you're trying to do - from the query it seems you're joining the same table to get... something. But the query is a bit confusing, so it's a bit hard to fathom what that "something" is.
     
    PoPSiCLe, Jun 28, 2015 IP
  9. Rinki12

    Rinki12 Banned

    Messages:
    120
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    53
    #9
    Had issues while defining the table name. Thanks everyone..
     
    Rinki12, Jun 28, 2015 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #10
    This really looks like a case where formatting would have saved you a lot of headaches, since it looks like all that was really wrong was you forgot two underscores. tblquestion instead of tbl_question if the rest of your table names are to be believed.

    It REALLY gets clearer if you keep the SQL commands in caps and throw a few carriage returns in there... and strip out the 'quotes for nothing' since the odds of ANY of those names raising a namespace conflict are effectively zero.

    SELECT 
    	question_category.question_category_id,
    	question_category.category_title,
    	parent_category.category_title AS parent_category_title
    FROM
    	tbl_question_category AS question_category
    LEFT JOIN
    	tbl_question_category AS parent_category
    ON
    	tblquestion_category.parent_category_id = parent_category.question_category_id
    WHERE
    	tblquestion_category.question_category_id = 3
    Code (markup):
    WAY clearer to read... and those two typos stand out far, far more than they do in the endless wordwrapping line.

    Which of course corrected would read:
    SELECT 
    	question_category.question_category_id,
    	question_category.category_title,
    	parent_category.category_title AS parent_category_title
    FROM
    	tbl_question_category AS question_category
    LEFT JOIN
    	tbl_question_category AS parent_category
    ON
    	tbl_question_category.parent_category_id = parent_category.question_category_id
    WHERE
    	tbl_question_category.question_category_id = 3
    Code (markup):
     
    deathshadow, Jun 28, 2015 IP