Hi,
I have a a form where users can select an item from a checkbox list, the list is taken from a table and generated as checkboxes on the page.. I also have a check box named 'other' in which the user can check if their option is not listed. If 'other' is checked then they can write in a text field the new item. This new item is then inserted into a table and the when the page is reloaded it will become a checkbox item also.
On the form processing page this is the code i am using to perform this task:
if($_POST['project_type'] == 'other'){
$project_other_other = $_POST['project_type_other'];
//an unique code is generated for the new item
$sqlp="
SELECT project_type_code
FROM auto_codes
WHERE code_id = 1
";
$resultpt1 = mysql_query($sqlp, $dbconnect)or die(mysql_error());
//will go through each row in the result set and display data
while ($rowpt = mysql_fetch_array($resultpt1)) {
//give a name to the fields
$project_type_code = $rowpt['project_type_code'];
}
//query to put the new item in the table from which the checkboxes are generated.
$sqlf = "
INSERT INTO project_type
VALUES('', '$project_type_code', '$project_other')";
$resultf = mysql_query($sqlf,$dbconnect)or die(mysql_error());
}
As well as this task which deals with a new item, the main dish of this code is to allow multiple checkbox selections to be stored in another table.
So if the user checks three boxes i have this code on my form processing page to insert the values in a table.
if (isset($_POST['project_type']) && is_array($_POST['project_type'])) {
foreach ($_POST['project_type'] as $value2) {
//echo "value: $value<br>\n";
$sqlpt = "INSERT INTO projtypexprogrammes
( `projtypexprogrammes_id` , `prog_code` , `project_type_code`)
VALUES('', '$prog_code', '$value2')";
$resultpt = mysql_query($sqlpt, $dbconnect)or die(mysql_error());
}
}
My problem arises when a user decides to add a new item and check existing boxes. I have added a simple insert to the first block of code to allow the new value to be inserted into the projtypexprogrammes table, like so:
$sqlproj = "INSERT INTO projtypexprogrammes
( `projtypexprogrammes_id` , `prog_code` , `project_type_code`)
VALUES('', '$prog_code', '$project_type_code')";
$resultproj = mysql_query($sqlproj, $dbconnect)or die(mysql_error());
But only the first block of code gets carried out in this situation. I can't fathom it and would really appreciate some fresh eyes.
Thanks in advance.