Avatar of medievalman
medievalman
 asked on

MySQL - Error 1054 : Unknown column in 'field list' when column actually exists.

I have a simple update query that is failing with the following errror...

Error 1054 : Unknown column 'Display' in 'field list'

The column does exist in the database and I have checked to make sure there are no spaces or anything quirky like that going on. I have tried moving, dropping, and re-creating the columns with no success. I am using MySQL version 5.6.17 The code that produces the error is as follows...

<?php

	$post_title = trim(mysqli_real_escape_string($db, $_POST['Title'])); 
	$post_longdescription = trim(mysqli_real_escape_string($db, $_POST['LongDescription']));
	$post_laborrate = $_POST['LaborRate'];  
	$post_display = (int) $_POST['Display']; 
	$post_id = (int) $_POST['LaborerId']; 
	

	$querystring =  " UPDATE myTable";
	$querystring .= " SET ";
	$querystring .= " Title = '{$post_title}',";
	$querystring .= " Display = $post_display,";
	$querystring .= " LongDescription = '{$post_longdescription}',";
	$querystring .= " LaborRate = $post_laborrate";
	$querystring .= " WHERE LaborerId = " . $post_id;
	
	//spew the query string if set
	if(isset($_GET['showquery']) && $devmode) {
		var_dump($querystring);
	}	
				
	//Query was unsuccessful
	$result = mysqli_query($db, $querystring);
		if(!$result) {
			if($devmode) {
				die("Error " . mysqli_errno($db) . " : " . mysqli_error($db));
			} else {
				die("An error has occured. Please contact the system administrator.");			 	
			}
		}	
		
		if(!mysqli_affected_rows($db)) {
			die("No matching rows found. Please contact system adminstrator");
		}	
?>

Open in new window

MySQL ServerPHP

Avatar of undefined
Last Comment
medievalman

8/22/2022 - Mon
gr8gonzo

1. Double-check to make sure you're connecting to the right database. :) I've seen people forget that they're connecting to a development database, which doesn't have the latest columns, and when they check to see if the column exists, they check a production database or something, so they THINK it's there, but it's really just a different database.

2. Do you use column permissions at all?

3. What happens if you run a query to show the columns?
<?php
$result = mysqli_query($db, "SHOW COLUMNS FROM MyTable");
while ($row = mysqli_fetch_assoc($result)) { print_r($row); }

Open in new window


What's the output there?
gr8gonzo

Also, post your finished query.
medievalman

ASKER
Positive I am using the correct db because it's an include file used for each connection. If I dump $querystring to the screen and then copay and paste into phpAdmin, where it actually shows the field for the table, it still fails with the same error. As for the results of that query you asked me to run...

Array ( [Field] => LaborerId [Type] => int(11) [Null] => NO [Key] => PRI [Default] => [Extra] => )
Array ( [Field] => Title [Type] => varchar(100) [Null] => NO [Key] => [Default] => [Extra] => )
Array ( [Field] => LongDescription [Type] => longtext [Null] => YES [Key] => [Default] => [Extra] => )
Array ( [Field] => LaborRate [Type] => decimal(18,3) [Null] => NO [Key] => [Default] => [Extra] => )
Array ( [Field] => Display [Type] => tinyint(1) [Null] => NO [Key] => [Default] => 1 [Extra] => )

Thanks for your help.
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
medievalman

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gr8gonzo

I don't think so - that error is just what you get back from MySQL.
medievalman

ASKER
Remember to check and see if underlying Triggers could be the cause of an error where it appears your SQL is correct. MySQL did not tell me it was a different table or a trigger causing the error. Just that there was an error.