mysql - How to include a variable inside of a post variable within a query string? -php

944

I have a table with a lot of checkboxes. The checkboxes are being produced via a loop, and are given a unique id.

$get_depts = mysql_query("select dept_id, dept_name from depts where bus_id = '{$business['bus_id']}'");
while(($department = mysql_fetch_assoc($get_depts)))
{
    echo '<tr><td>'.$department['dept_name'].'</td>';
    $req_fields = mysql_fetch_assoc(mysql_query("SELECT employees, department, customer, salespeople, acct_nbrs, categories, subcategories, case_ids, login_ids, phone, phext, cust_email, problem, resolution, source, priority FROM required_fields WHERE dept_id = '{$department['dept_id']}'"));
    if($req_fields['employees'] == 'YES'){echo '<td><input type="checkbox" id="employees[]" name="employees[]" value="YES" checked></td>';}
    else echo'<td><input type="checkbox" id="employees[]" name="employees[]" value="YES"></td>';
}

As you can see, one such example of the id and name of a checkbox is employees1, employees2, etc.

After submit, I try and loop back through my departments to pick up the unique id numbers again. Then I try and update my table.

$get_depts = mysql_query("select dept_id, dept_name from depts where bus_id = '{$business['bus_id']}'");
while(($department = mysql_fetch_assoc($get_depts)))
{
    mysql_query("Update required_fields set employees = '{$_POST['employees'.$department['dept_id']]}' where dept_id = '{$department['dept_id']");
}

I obviously am not using the correct syntax for the post variable. How can I correctly use $_POST['employees'] + $department['dept_id']?

EDIT

The FINAL result would look like this. If $department['dept_id'] = 10 for example, then the name of the post variable would be $_POST['employees10']

198

Answer

Solution:

If you're happy with having them concatenated straight across, you can simply use:

$_POST['employees'] . $department['dept_id']

The. is used for concatenation in PHP.

For example:

$get_depts = mysql_query("select dept_id, dept_name from depts where bus_id = '{$business['bus_id']}'");
while(($department = mysql_fetch_assoc($get_depts)))
{
 //
 // I prefer to do my concat outside of the query!
 //
 $employees = $_POST['employees'] . $department['dept_id'];

 //I assume this query isn't really what you're running.  Pseudo-code, no?
    mysql_query("Update required_fields set employees = '$employees' where dept_id = '{$department['dept_id']}");
}
355

Answer

Solution:

In your first code you need to specify a uniqe name for the input field. You are using an array but it will only hold some auto generated number (0,1,2,3,4...) You should change it to the department id from your database so it looks more like this:

$depid=$department['dept_id'];
if($req_fields['employees'] == 'YES'){echo "<input type='checkbox' id='employees[{$depid}]' name='employees[{$depid}]' value='YES' checked></td>";}
else echo "<td><input type='checkbox' id='employees[]' name='employees[]' value='YES'></td>";
}

You also need to use double quotes instead of single quotes because only then your string will be parsed with your variables.

Then in your second code the Post part should look like this:

$dep = $_POST['employees'][$department['dept_id'];
mysql_query("Update required_fields set employees = '$dep' where dept_id = '{$department['dept_id']");

People are also looking for solutions to the problem: php - Multi-threading in Symfony2

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.