Why is MySQL looking for colum with name of value inserting - mysql

When doing a cfquery to MySQL, MYSQL gives me the error Unknown column 'Question' in 'field list' on the following code:
<cfquery name="qUpdateTheQuestion" datasource="testmaster">
INSERT INTO ETrueFalseQuestions
VALUES (<cfoutput>#newTypeID#</cfoutput>, `<cfoutput>#FORM.Question#</cfoutput>`, <cfoutput>#FORM.truefalse#</cfoutput>
I have tried everything I could think of to try to solve this issue, as well as some suggestions I found when trying to search for a solution, such as using backticks instead of single quotes.
I also don't understand why it is searching for a column with the name Question, as that is the value of FORM.Question, and changes depending on what is typed into the form. Any suggestions would be greatly appreciated.

that query makes no sense to me
normally it should look like
INSERT INTO ETrueFalseQuestions (IdColumnName, questionColumnName, questionTrueFalse)
VALUES ( #newTypeID#, '#form.Question#', #form.truefalse# )
So you need to
list the columns
not use <cfoutput> within a <cfquery>
put single quotes around string values or use <cfqueryparam>
PS: the column names I used are made up you need to fix them to match yours.

Related

Assistance with MySQL 'LIKE' Statement

Hello all you beautiful people,
To preface my question, we have a database that stores the entire HTML code of a web form (don't ask, I didn't create this) and some of those HTML values are populated from a different table. This table was discovered to have some double quotes (") in them, this is problem because if you have:
<input type="text" value="Hello "John" Smith">
It will break all of the tags below it.
We've gone ahead and fixed this, and I've implemented a solution to catch these from today onward. But now we have an unknown amount of forms that are broken.
So what I am trying to do is run an SQL statement to find all of the instances that this occurred.
Here is what I've got:
SELECT * FROM tableName WHERE data LIKE '%value="%"%"'
But this statement hasn't yielded any results.
Any help would be greatly appreciated!
Thank you,
DM
Your tags probably don't end with a quote, so let's add a wildcard at the end:
SELECT * FROM tableName WHERE data LIKE '%value="%"%"%'
If the value attribute is the last attribute in an element you could check if the value of value if enclosed in double quotes in this way:
SELECT * FROM tableName WHERE data
LIKE '%value="%"_%>'
The condition checks if there is double quote, before attribute value ends meaning incorrect value.
_% means one or more of any character.

How to assign aliases to an array of column names with Joomla's quoteName()?

I want to use the AS statement for Aliases in a query.
I use this piece of code:
$query->select($db->quoteName(array('NameInQ as nin', 'Name')));
Anyway I get this error:
'Unknown column 'NameInQ as nin' in 'field list'
NameInQ does exist as a column name in the table. nin should be the alias.
What am I doing wrong?
When you tell Joomla:
$query->select($db->quoteName(array('NameInQ as nin', 'Name')));
echo $query->dump(); will tell you:
SELECT `NameInQ as nin`,`Name`
See how it doesn't know how to differentiate an aliased column name from a string with spaces in it?
The Docs: https://api.joomla.org/cms-3/classes/JDatabaseQuery.html#method_quoteName
If you want to assign aliases to column names in Joomla from within the qn() / quoteName() method, you will need to nominate corresponding aliases for all columns.
$query->select($db->quoteName(array('NameInQ', 'Name'), array('nin', 'Name')));
Renders as:
SELECT `NameInQ` AS `nin`,`Name` AS `Name`
// ^-------^----^---^-^----^----^----^-- everything aliased, everything backtick wrapped
Or, of course you could individualize the quoteName() calls, you can avoid aliasing every column.
$query->select(array($db->quoteName('NameInQ', 'nin'), $db->quoteName('Name')));
Renders as:
SELECT `NameInQ` AS `nin`,`Name`
Finally, the truth is: You don't even need to quote any of your sample column names because the query will be stable/secure without the extra method call(s). *I recommend leaving them out to minimize query bloat and developer eye-strain.
$query->select(array('NameInQ AS nin', 'Name'));
or even in raw form:
$query->select('NameInQ AS nin, Name');
For the record, Name (MYSQL is case-insensitive) IS a KEYWORD, but it is not a RESERVED KEYWORD.
See the MySQL Doc: https://dev.mysql.com/doc/refman/5.5/en/keywords.html#keywords-5-5-detailed-N (there is no "(R)" beside Name

How do I capitalize all text in a column of data in an Access Query while keeping the name of the field the same?

How do I capitalize all text in a column of data in an Access Query while keeping the name of the field the same?
I tried entering "SPEC: StrConv([SPEC],3), but I get an error that I have a circular argument (which, isn't too surprising). So how do I get around this?
Is there a totally different approach to capitalizing in queries?
Given: we have a field named [SPEC].
Problem: need query to grab [SPEC] and convert it to all caps, but
with the same field name
Added: We will call the table that holds the field [SPEC],
[tblTable]
Solution:
What we need to put in the query builder is the following:
SPEC: UCase([tblTable].[SPEC])
That way the machine can figure out that Query.SPEC isn't the same identifier as tblTable.SPEC
Equivalently:
SELECT UCase([tblNames].[FirstName]) AS FirstName
FROM tblNames;
How about using the Ucase function
Ucase(String)

How can I order a mySQL column by a number imbedded in a string?

I am parsing genomic positions from a MySQL field. The field is called "change" and the entries are of the form:
g.100214985T>C
g.100249769C>A
g.10185G>T
I am trying to order the field by the numerical portion of the string. I am trying to figure out what mySQL query I can use to accomplish this. I have tried using REGEXPs and SUBSTRING_INDEX but am still running into issues. Any help would be much appreciated!
Assuming you have always 2 characters in front of and 3 at the end you need to have removed:
SELECT CAST(SUBSTR(col from 3) AS UNSIGNED) AS value
FROM `my_table`
ORDER BY value
Watch this sql fiddle also: http://sqlfiddle.com/#!2/7bc0e/67
Thank you #MarcusAdams and #amoudhgz! The following code works:
CAST(SUBSTR(field, 3) AS UNSIGNED).
MySQL already stops the conversion at the first non-numerical character.

SQL REPLACE INTO not working

So I have a form that can get data from a database by its ID (auto-incremented column(Primary Key)) and display all fields in the <input> tags via value properties. And when I submit the form I want it to either INSERT a new row if the ID from the ID column doesn't already exist and if it does I want to UPDATE the rest of the data in the row with the same ID.
I have been trying to research this, but no one seems to be doing the same thing I am trying to do, its always slightly different. I found a REPLACE INTO and created it like below:
$sqlString = 'REPLACE INTO coursework
SET cwID=`'. $cwID .'`,
cwTitle=`'. $cwTitle .'`,
cwContent=`'. $cwContent .'`,
cwProgress=`'. $cwProgress .'`,
cwDue=`'. $cwDue .'`;
All the $cw[] variables being content received from $_POST method.
I keep getting a Error code: 1054-Unknown column '6' in 'field list' -
the "Unknown column '6'" is mysql trying to call $cwID (value of $_POST['cwID']) instead of the cwID column(which is the Primary Key for my table). I feel like there is something simple and stupid I am missing but I have never used this REPLACE INTO method before.
I saw a post about INSERT IGNORE INTO and INSERT ... ON DUPLICATE KEY UPDATE but both of those sound more destructive than what I am looking for.
I just want to make sure that the table is updated if the cwID exists and the auto-increment is kept in tact, or a new row is added if there is no ID. Should I just run a SELECT query to see if it exists and INSERT/ UPDATE appropriately?
Remove Replace the back-ticks (`) surrounding the strings with single quotes (').
MySql is trying to find a column named by the strings you are using as values.
See http://dev.mysql.com/doc/refman/5.5/en/identifiers.html
Replace the backticks with single quotes:
$sqlString =
"REPLACE INTO coursework SET cwID='$cwID', cwTitle='$cwTitle', cwContent='$cwContent', cwProgress='$cwProgress', cwDue='$cwDue'";
Also, note that " will interpolate your variables.