ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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();
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.
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.
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 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);
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.