Update More Than One Record
Moderator: General Moderators
Update More Than One Record
I have a script where a manager logs on and checks off which cases to assign to an inspector. Next to the check box is a 3 digit field that the manager fills out that is the number of the inspector that is going to be assigned to that case.
At the bottom of the page I have a submit button that the manager clicks in order to assign these cases to the inspectors. I want to know how after the manager hits submit how can I have the program update the inspector_code in each case record that was selected. If the case was selected I want the inspector field in the database for that record to be updated for each record.
Database Fields:
id
inspector_code
PLEASE HELP!!!
At the bottom of the page I have a submit button that the manager clicks in order to assign these cases to the inspectors. I want to know how after the manager hits submit how can I have the program update the inspector_code in each case record that was selected. If the case was selected I want the inspector field in the database for that record to be updated for each record.
Database Fields:
id
inspector_code
PLEASE HELP!!!
I presume you show a list of selectboxes (give them as name the case_id).
Probably want to save them in $_SESSION (so when the form is posted back, you know which you have displayed and are expecting)
All those that were actually selected will appear in $_POST (those that were not checked simply don't appear).
Now loop over those variables, update your database, and you're set
Probably want to save them in $_SESSION (so when the form is posted back, you know which you have displayed and are expecting)
All those that were actually selected will appear in $_POST (those that were not checked simply don't appear).
Now loop over those variables, update your database, and you're set
More Help!
Thats's not what im trying to do really I do not think.
The loop is what I need help with really, some code example for this would help.
The loop is what I need help with really, some code example for this would help.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Judging from your specifications, the checkbox would be superfluous. Simply leave boxes that you don't want to be assigned false. Your HTML form will look like:
Which will look like
Then, the loop would look like
Totally untested though.
Code: Select all
<tr><td>Name of assignment</td><td><input type="text" name="assign[assignment_id]" /></td></tr>
...Code: Select all
$_POST['assign'] = array(
'4' => 'inspector_number'
'67' => 'inspector_number'
//...
);Code: Select all
foreach ($_POST['assign'] as $id => $inspector_code) {
$mysql_id = (int) $id;
$mysql_inspector_code = (int) $inspector_code;
mysql_query("INSERT INTO `tablename` (`id`, `inspector_code`) VALUES ('$mysql_id','$mysql_inspector_code')");
}Well...
The inspector_codes are dynamic, there are no certain codes. The codes are assigned my the executive accounts. So naming them in a array as if knowing what they are going to be wouldent work. I still need HELP!!!!!!!!!!!!
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
He only named them in the array to show you an example
So lets break it down a little more for you. As you generate the form, you would generate a bunch of input fields where assigment_id would be the your `case id`.
So in this instance, we have 4 different cases (which can all be generated dynamically). Lets say I typed in a 3 digit code of 001 for each of those
As you can see, you don't need to hardcode anything.. your end result should look something like
So lets break it down a little more for you. As you generate the form, you would generate a bunch of input fields where assigment_id would be the your `case id`.
Code: Select all
<form action="" method="POST">
<input type="text" name="assign[5]" /> <br />
<input type="text" name="assign[12]" /> <br />
<input type="text" name="assign[18]" /> <br />
<input type="text" name="assign[19]" /> <br />
<input type="submit">
</form>Code: Select all
Array
(
[assign] => Array
(
[5] => 001
[12] => 001
[18] => 001
[19] => 001
)
)Code: Select all
if (!empty($_POST['assign'])) {
foreach ($_POST['assign'] as $caseId => $inspectorId) {
if (!empty($_POST['assign'][$caseId])) {
//run update
}
}
}Heres My Code
I still need help with this one.
Here is my HTML code:
Now for each one that is checked off and a value is entered I want the inspector_code to be entered into the database for the id in the database. There could bew only one updated, or there could be more. I need to know how to do this.
Here is my HTML code:
Code: Select all
<!-- These below are printed out dynamically from the database -->
Assign: Inspector Code:
<input type="checkbox" name="id" value="<?php echo $row["id"]; ?>"> <input type="text" name="inspector_code">
<input type="checkbox" name="id" value="<?php echo $row["id"]; ?>"> <input type="text" name="inspector_code">
<input type="checkbox" name="id" value="<?php echo $row["id"]; ?>"> <input type="text" name="inspector_code">
<input type="checkbox" name="id" value="<?php echo $row["id"]; ?>"> <input type="text" name="inspector_code">I Still Need Help!!!
I still need help to my question, someone please!
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Something along the lines of
Note that I use a hardcoded array of $ids, this should be generated from a mysql_query() of all cases.
should output something along the lines of
Code: Select all
?>
<form method="post" action="">
<?php
$ids = array(10,11,12,13,14);
foreach ($ids as $id) {
echo '<input type="checkbox" name="assign['.$id.']" value="assigned"> <input type="text" name="inspector['.$id.']"> <br />';
}
if (!empty($_POST['submit'])) {
foreach ($_POST['assign'] as $assignID => $check) {
if (in_array($assign, $_POST['inspector'])) {
echo 'Run update on assignID: '. $assignID .' with inspector '. $_POST['inspector'][$assignID].'<br>';
}
}
}
?>
<input type="submit" name="submit" value="submitted">
</form>should output something along the lines of
Run update on assignID: 10 with inspector 111
Run update on assignID: 11 with inspector 222
Run update on assignID: 12 with inspector 333
hmhmh...
I cant use a hard code of arrays tho, thats my problem.
The inspector_codes are dynamic.
Every part of this is dynamic, I do not know the id of the case, the inspector_code cause they change, and what the user is going to enter obviously, there could be 1000 cases or 500 cases, there could be inspector codes 100-500.
The inspector_codes are dynamic.
Every part of this is dynamic, I do not know the id of the case, the inspector_code cause they change, and what the user is going to enter obviously, there could be 1000 cases or 500 cases, there could be inspector codes 100-500.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
I've already said this any twice now, we know that the array is going to be dynamic.. what I'm saying is just plug in your dynamically generated list of cases from the database...
Code: Select all
$result = mysql_query('SELECT `id` FROM `tablename`') or die(mysql_error());
$ids = array();
while ($row = mysql_fetch_assoc($result)) {
array_push($ids, $row['id']);
}Code Not Working
There is no error on the code. Nothing happens to the database when submit is hit.
Heres my code:
Heres my code:
Code: Select all
<form method="post" action="test.php">
<?php
include("connect.php");
$result = mysql_query('SELECT `id` FROM `cases`') or die(mysql_error());
$ids = array();
while($row = mysql_fetch_assoc($result)) {
array_push($ids, $row['id']);
}
foreach($ids as $id) {
echo '<input type="checkbox" name="assign['.$id.']" value="assigned"> <input type="text" name="inspector['.$id.']"> <br />';
}
if(!empty($_POST['submit'])) {
foreach ($_POST['assign'] as $assignID => $check) {
if(in_array($assign, $_POST['inspector'])) {
mysql_query("UPDATE cases SET inspector_code='" . $_POST['inspector'][$assignID] . "' WHERE id='" . $assignID . "'");
echo 'Run update on assignID: '. $assignID .' with inspector '. $_POST['inspector'][$assignID].'<br>';
}
}
}
?>
<input type="submit" name="submit" value="Assign">
</form>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
oops, change this line
to
Code: Select all
if(in_array($assign, $_POST['inspector'])) {Code: Select all
if(in_array($assignID, $_POST['inspector'])) {Still No Go
Its still not updating, its weird cause there is no errors from the code.
Here is the updated code:
Here is the updated code:
Code: Select all
<form method="post" action="test.php">
<?php
include("connect.php");
$result = mysql_query('SELECT `id` FROM `cases`') or die(mysql_error());
$ids = array();
while($row = mysql_fetch_assoc($result)) {
array_push($ids, $row['id']);
}
foreach($ids as $id) {
echo '<input type="checkbox" name="assign['.$id.']" value="assigned"> <input type="text" name="inspector['.$id.']"> <br />';
}
if(!empty($_POST['submit'])) {
foreach ($_POST['assign'] as $assignID => $check) {
if(in_array($assignID, $_POST['inspector'])) {
mysql_query("UPDATE cases SET inspector_code='" . $_POST['inspector'][$assignID] . "' WHERE id='" . $assignID . "'");
echo 'Run update on assignID: '. $assignID .' with inspector '. $_POST['inspector'][$assignID].'<br>';
}
}
}
?>
<input type="submit" name="submit" value="Assign">
</form>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
mysql_query("UPDATE cases SET inspector_code='" . $_POST['inspector'][$assignID] . "' WHERE id='" . $assignID . "'");Code: Select all
mysql_query("UPDATE cases SET inspector_code='" . $_POST['inspector'][$assignID] . "' WHERE id='" . $assignID . "'") or die(mysql_error());BTW, is any of the
Code: Select all
echo 'Run update on assignID: '. $assignID .' with inspector '. $_POST['inspector'][$assignID].'<br>';