1

I have 2 mysql tables that are joined, in the database, by the descriptionId field.

dataTable.dataId
dataTable.descriptionId
dataTable.userId
dataTable.value

descriptionLookupTable.descriptionId
descriptionLookupTable.descriptionName

In PHP, I am trying to update the field: dataTable.value, when dataTable.descriptionID = Gender and the dataTable.userId = $_SESSION['userId'].

For example in the descriptionLookupTable: descriptionLookupTable.descriptionId = 1 and descriptionLookupTable.descriptionName = Gender. And in the dataTable: dataTable.descriptionID = 1.

I understand the part that deals with the userID by using:

"WHERE dataTable.userId = '" . $_SESSION['userId'] . "'"; 

But I can't figure out how to use INNER JOIN.

The following query does not work.

$query = "UPDATE dataTable
  INNER JOIN descriptionLookupTable
  ON dataTable.descriptionId = descriptionLookupTable.descriptionId
  AND descriptionLookupTable.descriptionName = Gender
  SET Gender = '$gender' WHERE dataTable.user_id = '" . $_SESSION['user_id'] . "'";

The error message is: Unknown column 'Gender' in 'field list'.

VolkerK
  • 95,432
  • 20
  • 163
  • 226

2 Answers2

1

Gender apparently is not a column in your table. One of the field must be named Gender for you to be able to update its content

what about trying to update a field which is in your table :) :

SET dataTable.value = '$gender' WHERE dataTable.user_id = '" . $_SESSION['user_id'] . "'";
  • Thanks but when I try it I get Error msg: Unknown column 'dataTable.value' in 'field list'. – user2646006 Jan 14 '14 at 16:15
  • hmmm. how come mysql would recognize dataTable.descriptionId for the join but not dataTable.value. are you sure about capitalization and the name of your fields? could you post the structure of your tables? like the output of phpmyadmin when you export the structure only to the screen – Félix Adriyel Gagnon-Grenier Jan 14 '14 at 16:21
  • oh and be sure to put gender between colons 'gender' as VolkerK said. this is effectively a problem which you will run into after you rule out the problem of column names – Félix Adriyel Gagnon-Grenier Jan 14 '14 at 16:25
  • 1
    Thank you to everyone! The combination of ideas from Felix and Volkerk worked! The following mysql statement worked: $query = "UPDATE dataTable INNER JOIN descriptionLookupTable ON dataTable.descriptionId = descriptionLookupTable.descriptionId AND descriptionLookupTable.descriptionName = 'Gender' SET dataTable.value = '$gender' WHERE dataTable.user_id = '" . $_SESSION['user_id'] . "'"; – user2646006 Jan 14 '14 at 16:27
  • Actually there is a typo in my last comment wrt user_id. The following is the fix: $query = "UPDATE dataTable INNER JOIN descriptionLookupTable ON dataTable.descriptionId = descriptionLookupTable.descriptionId AND descriptionLookupTable.descriptionName = 'Gender' SET dataTable.value = '$gender' WHERE dataTable.userId = '" . $_SESSION['userId'] . "'"; – user2646006 Jan 14 '14 at 16:39
0
descriptionLookupTable.descriptionName = Gender

Gender in this case is a string literal, therefore you have to mark it as such

descriptionLookupTable.descriptionName = 'Gender'
VolkerK
  • 95,432
  • 20
  • 163
  • 226