2

I have two tables: one hostgroup_host and the other one hostgroups. The hostgroups represents the names of all the host groups and has 3 important values (hostgroup_id, name and alias). The hostgroup_host makes the connection between the groups and the hosts and has two values (hostgroup_id and host_id). Example: hostgroup_id = 1 and host_id = 3 it means that host which id is number 3 belongs to hostgroup with id = 1 (with a specific name).

I have this:

$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(); ?>

Why doesn't work?

1
  • what exactly doesn't work? what is the error you are getting? Commented May 12, 2011 at 15:31

3 Answers 3

1

well, just join your insert statements: something like:

INSERT INTO TABLE_A (id,name,alias);
INSERT INTO TABLE_B (id,name);

the ; is seperating commands in my sql, so basicly when you will run the query, both of the insert statements will be run.

Sign up to request clarification or add additional context in comments.

1 Comment

mysql_query will only execute 1 query though (multiple queries are not supported), so you will have to execute every query in a separate call.
0

I'm a bit confused, but just use more than one link resource.

$linka = mysql_connect('server1', 'mysql_user', 'mysql_password');
$linkb = mysql_connect('server2', 'mysql_user', 'mysql_password');

Then use

$result = mysql_query('INSERT...', $linka);

or

$result = mysql_query('INSERT...', $linka);

But I fear I may have misunderstood your question completely...

1 Comment

grim imagine one page to add an host: i have 3 textboxes (name, alias and address) and a dropdown menu which lists all the groups available (the host being added is going to belong to one of the groups). After submit I have another php file which makes all those actions. I just want to know how can I pick the hostgroup_id associated to the name on the dropdown menu (the group name) and insert into hostgroup_host associated to the host id.
0

Am I missing something or wouldn't it just be better to have the hostgroup_id and the host_id as the option values in the dropdowns?

<select name="hosts">
  <option value="1">Host 1</option>
  ...
</select>

<select name="hostgroups">
  <option value="15">Host with 15 as ID</option>
  ...
</select>

then just insert the post values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.