LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2011, 10:29 AM   #1
puppymagic
Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 66

Rep: Reputation: 4
Post What's wrong with this SQL statement?


It is under PHP/MySQL

$queryresult = $conn->query("INSERT INTO normalrequests VALUES($finalkey, 1234, 5678, FALSE)");

thanks....

it is connected to the database successfully but I feel there is something wrong with the statement up there~~
 
Old 01-29-2011, 10:52 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
As far as I'm aware (and without having tried it) the format is correct, as long as you have 4 fields and 4 fields only, which appear in the order listed above. If you had, say, 5 fields then you would need to tell it into which columns you wanted to enter data:

Code:
INSERT INTO normalrequests (key, numberone, numbertwo, aboolean) VALUES ($finalkey, 1234, 5678, FALSE)
where key, numberone, etc. were the names of your columns.
 
Old 01-29-2011, 11:16 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

STRONG SUGGESTION:
Cut/paste the exact statement into mysql.

This will give you an error message; you should be able to identify and resolve the problem almost immediately.

If you have any questions about what mySql is doing, then cut/paste your input and the mysql response into this thread.

My guess is that you probably need to quote one or more of your values.

'Hope that helps!
 
Old 01-29-2011, 11:28 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
Originally Posted by puppymagic View Post
$queryresult = $conn->query("INSERT INTO normalrequests VALUES($finalkey, 1234, 5678, FALSE)");
Isn't this a security vulnerability?

If finalKey were set to:

Code:
$finalKey = '1, 1, 1, FALSE); DROP TABLE normalRequest; INSERT INTO normalRequest VALUES (1'
Then the line would be:

Code:
$queryResult = $conn->query("INSERT INTO normalRequests VALUES(1, 1, 1, FALSE); DROP TABLE normalRequest; INSERT INTO normalRequest VALUES (1, 1234, 5678, FALSE)");
As a rule, every database query that involves a parameter should be done with prepared statements and parameter binding.

Last edited by dugan; 01-29-2011 at 12:17 PM.
 
Old 01-29-2011, 11:35 AM   #5
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
It depends on what is in the variable "$finalkey". If this is always an integer I suggest to make
Code:
$finalkey = intval($finalkey);
before $conn->query().
 
Old 01-30-2011, 11:40 AM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi again, puppymagic -

Let me repeat - if you think a SQL statement might be having a problem, just try copying/pasting the statement into mysql.

You can add a PHP "echo" to your code in order to get something you can copy/paste from.

It's probably the fastest/easiest way to troubleshoot.

And please let us know how things turn out!
 
Old 02-12-2011, 02:57 AM   #7
puppymagic
Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 66

Original Poster
Rep: Reputation: 4
oh the problem has been solved by going '$finalkey' instead of $finalkey thanks for all your inputs, guys and girls from Linux Questions

and the value of the $finalkey is randomly generated within the php script. it is not something the visitor to the website can place in there instead.

Last edited by puppymagic; 02-12-2011 at 03:00 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL update statement to SQL SERVER 2005 scheidel21 Programming 2 11-05-2009 06:30 PM
sql statement question ShaqDiesel Programming 4 10-17-2009 07:52 AM
Complex SQL Statement 0.o Programming 7 05-21-2008 06:57 AM
help optimizing this SQL statement hedpe Programming 1 05-30-2007 07:06 AM
SQL statement glj Programming 1 10-12-2001 09:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration