LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-12-2011, 10:29 AM   #1
aocferreira
Member
 
Registered: Feb 2011
Posts: 117

Rep: Reputation: 0
sql query


Hello.
I have to 3 tables (hosts, hostgroups and hostgroup_host). I have a php page to add an host where are 3 textboxes and a dropdown menu which lists all the groups available (the host is going to belong to a group).
When I submit the information I have another php responsible for those actions.. What's wrong here?

Code:
$name=$_POST['name'];
$alias=$_POST['alias'];
$address=$_POST['address'];
$hostgroup=$_POST['hostgroup'];


mysql_connect('localhost:/usr/local/groundwork/mysql/tmp/mysql.sock', $username ,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO hosts (host_id, name, alias, address, hosttemplate_id) VALUES ('','$name','$alias','$address', '1');
INSERT INTO hostgroup_host (hostgroup_id, host_id) VALUES ((select hostgroup_id from hostgroups where name='$hostgroup'), (SELECT
host_id from hosts where name = '$name'))";
mysql_query($query);

mysql_close();
 
Old 05-12-2011, 10:55 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Well, for one thing, what you've posted is a critical security vulnerability. You've taken no steps at all to protect yourself against SQL injection attacks. Or any other attack vector involving form input.

This presumably wasn't what you were thinking of, so if you want an answer to your actual question then you should tell us why you think anything is wrong.

Last edited by dugan; 05-12-2011 at 10:59 AM.
 
Old 05-13-2011, 03:58 AM   #3
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
Why would you put everything in 1 php query and not the two queries it actually is? I don't know if mysql actually supports this or the nested select as you did them in your second query. Otherwise you may have to turn this into the four queries;

Below is a slightly altered version where I put in a very basic form of security (add_slashes) which by no means provides "full" security against malicious use of your form.

PHP Code:
$name=$_POST['name'];
$alias=$_POST['alias'];
$address=$_POST['address'];
$hostgroup=$_POST['hostgroup'];


mysql_connect('localhost:/usr/local/groundwork/mysql/tmp/mysql.sock'$username ,$password);
@
mysql_select_db($database) or die( "Unable to select database");

$query=sprintf("INSERT INTO hosts (host_id, name, alias,address, hosttemplate_id) VALUES ('','%s', '%s', '%s', 1)",
add_slashes($name), add_slashes($alias), add_slashes($address));
mysql_query($query);

$query=sprintf("INSERT INTO hostgroup_host (hostgroup_id, host_id) VALUES((SELECT hostgroup_id FROM hostgroups WHERE name='%s'),(SELECT host_id FROM hosts WHERE name='%s'))",
add_slashes($hostgroup),add_slashes($name));
mysql_query($query);

mysql_close(); 
 
  


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 query help struct Programming 2 10-27-2010 12:29 AM
SQL: do math in query Ephracis Programming 4 04-25-2009 02:47 AM
How would I make this SQL query OlRoy Linux - Server 3 03-14-2007 02:51 PM
Massive SQL Query patpawlowski Programming 7 03-05-2004 04:24 PM
SQL Query question oulevon Programming 7 01-16-2004 01:50 AM

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

All times are GMT -5. The time now is 09:59 PM.

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