0

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']

1

2 Answers 2

1

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']}");
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried including that in the sql statement, and I think I did something wrong. Would you be able to apply your suggestion to the update query as shown in the question?
Binform, I tried adding the query you suggested, but I'm getting a syntax error - "unexpected "", expecting }
when I echo $employees, I get the value of the department number, but YES or ""
0

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']");

1 Comment

I am doing exactly as you suggested for the first part of your response. All of the id and name tags have the department id as part of the id name. Also, I have never run into an issue where single quotes don't do the same thing as double quotes. Next, I tried using $dep = $_POST['employees'[$department['dept_id'];, but it did not work. Syntax error. I tried $_POST['employees'][$department['dept_id']], no syntax error, but it did not wokr.

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.