LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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
 
LinkBack Search this Thread
Old 03-02-2006, 12:12 PM   #1
ckoniecny
Member
 
Registered: Oct 2005
Location: Michigan
Distribution: Slackware
Posts: 162

Rep: Reputation: 30
POST data to PHP code


I have this HTML code:

Code:
html>
<body>
<form action="test.php" method="POST">
Search Department(s): <input type="text" name="Department" /><br>
<input type="submit" />
</form>
</body>
</html>
I'm trying to use the POST data in a SQL Query like the following:

Code:
$QUERY = 'SELECT * FROM CommExchange.Personnel_Information WHERE Department='$_POST["Department"]'';
But its not working. Is there something I'm missing?
 
Old 03-02-2006, 01:10 PM   #2
pnellesen
Member
 
Registered: Oct 2004
Location: Missouri, USA
Distribution: Slackware 12.2, Xubuntu 9.10
Posts: 371

Rep: Reputation: 31
I think for POST variables, you need single quotes for the index instead of double quotes, so maybe try '$_POST['Department']'?
 
Old 03-02-2006, 01:18 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Code:
$QUERY = 'SELECT * FROM CommExchange.Personnel_Information WHERE Department='$_POST["Department"]'';
use double quotes for the query string.. otherwise $_POST[..] is a literal value and not a variable..
so do this..
Code:
$QUERY = "SELECT * FROM table WHERE field='$_POST[department]'";
i believe that should work but cant test right now.. notice how the array id word Department does not need to be quoted when used in this manner..
 
Old 03-02-2006, 01:30 PM   #4
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
Firstly, pnellesen is incorrect; you can use single or double quotes in $_POST[] vars, just like any other array.

xhi is correct, in that you must use double-quotes around your string if you wish to include variable names in it to be translated.

However, that isn't the whole story. If you are embedding an array variable into a string like this, you must wrap it in curly braces, otherwise the PHP interpreter gets confused by the array variable syntax.

So your code would need to look like this:
PHP Code:
$query "SELECT * FROM table WHERE field='{$_POST[department]}'"
One other thing I would add: Please also be very careful when putting a post variable directly into a SQL query -- you may be opening yourself up to a potential security problem, called an "SQL injection attack", where malicious posters could manipulate your SQL string and hack your database.

Hope you get everything working

All the best.
 
Old 03-02-2006, 01:31 PM   #5
msound
Member
 
Registered: Jun 2003
Location: SoCal
Distribution: CentOS
Posts: 465

Rep: Reputation: 30
I would use:
PHP Code:
$QUERY 'SELECT * FROM CommExchange.Personnel_Information WHERE Department=\'' $_POST[Department] . '\''
 
Old 03-02-2006, 09:23 PM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
However, that isn't the whole story. If you are embedding an array variable into a string like this, you must wrap it in curly braces, otherwise the PHP interpreter gets confused by the array variable syntax.
i have never ran into having to use the curly braces. is that somthing new? or maybe i have just never ran into it because i dont usually assign the post data to another variable..
ex..
Code:
$forumQ = mysql_query("SELECT * FROM forums WHERE id=$_POST[fid]");
 
Old 03-02-2006, 10:48 PM   #7
mfrick
Member
 
Registered: Sep 2004
Location: Perth
Posts: 51

Rep: Reputation: 15
Quote:
Originally Posted by Spudley
Firstly, pnellesen is incorrect; you can use single or double quotes in $_POST[] vars, just like any other array.

xhi is correct, in that you must use double-quotes around your string if you wish to include variable names in it to be translated.

However, that isn't the whole story. If you are embedding an array variable into a string like this, you must wrap it in curly braces, otherwise the PHP interpreter gets confused by the array variable syntax.

Cheers for that (although I wasn't asking the question) as I had always broken the string up like the post below yours eg "start string".theArray[element]."end string" but I do very much like the {} solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
merge ASP code with PHP code.. possible ?? ALInux Programming 7 12-30-2005 08:40 AM
PHP Question about POST data BrianK Programming 7 11-26-2005 01:33 PM
php, apache, POST/GET data jhaiduce Linux - Software 0 12-17-2003 04:02 PM
Trouble recognising 'post' data in a php file davee Programming 4 10-20-2003 12:29 PM
php post data sql queries... bulliver Programming 2 01-03-2003 02:18 AM


All times are GMT -5. The time now is 06:49 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration