Jump to content

How to store Id in checkbox using PHP


aixen083
Go to solution Solved by mac_gyver,

Recommended Posts

Good Day, So this is the scenario. i have two tables Organization, and Course, so in organization.tbl i have org_name, org_description, and course_id and in course.tbl i have course_id, course_name. there is no problem in inserting the org_name and org_description but i have a problem in inserting the multiple course_id into organization.tbl

This is my checkbox script.

<?php $query = "SELECT * FROM course";
$result = mysql_query($query); 

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
<input type="checkbox" name="course" value="<?php echo $line["course_name"]?>">
<?php echo $line["course_name"]?> 
<br> <?php } ?>

now how to insert multiple course id into org.tbl

 

this is my query in saving the course_id.

 

<?php
if (isset($_POST['save'])) {
$course_id = $_POST['course_id'];

    mysql_query("insert into organization (course_id)
                        values ('$course_id')
                        ") or die(mysql_error());


}
?>
 

 

Link to comment
Share on other sites

Use

<input type="checkbox" name="course[]" value="<?php echo $line["course_id"]; ?>" /><?php echo $line["course_name"]; ?>

Then $_POST['course'] is an array();

if (isset($_POST['save']))
{
  for($x=0, $c=count($_POST['course']); $x<$c; $x++)
  {
    $course_id = mysql_real_escape_string($_POST['course'][$x]); // I recommend doing more validation, e.g. checking if it exists in the courses table...
    mysql_query("insert into organization (course_id) values ('$course_id')") or die(mysql_error());
  }
}

Note the real_escape_string - I am preventing an SQL Injection by escaping the value...  Always escape and validate user inputs.

 

Also note the mysql function are depreciated.  You should be using mysqli or PDO instead, as mysql WILL be removed in a future version of PHP.

 

Hope this answers your question.

Edited by kierany5
Link to comment
Share on other sites

you need a third table to do this correctly.

 

your organization table should just contain the unique information about each organization. your course table should just contain the unique information about each course. the third table that relates them would have columns for an id, org_id, and course_id. the org_id and course_id columns should also be setup as a unique composite index to prevent duplicate org_id/course_id entries.

Link to comment
Share on other sites

you need a third table to do this correctly.

 

your organization table should just contain the unique information about each organization. your course table should just contain the unique information about each course. the third table that relates them would have columns for an id, org_id, and course_id. the org_id and course_id columns should also be setup as a unique composite index to prevent duplicate org_id/course_id entries.

 

Sorry for this question but How can i Do that ... ?

Link to comment
Share on other sites

The set up you describes is the first in the three below. The one you probably want is the third

ORGANISATION                COURSE
------------                ------------                   (MANY-to-ONE)
org_id               +----- course_id                      courses are
org_name             |      course_name                    run by many 
org_description      |                                     organisations
course_id       >----+


ORGANISATION                COURSE
------------                ------------                   (ONE-to-MANY)
org_id          -----+      course_id                      organisations
org_name             |      course_name                    run many 
org_description      +----< org_id                         courses


ORGANISATION        ORG_COURSE            COURSE           (MANY-to-MANY)
------------        ------------          -----------      organisations run 
org_id      ---+    id             +---   course_id        many course and
org_name       +-<  org_id         |      course_name      courses run by
                    course_id   >--+                       many organisations
Link to comment
Share on other sites

 

The set up you describes is the first in the three below. The one you probably want is the third

ORGANISATION                COURSE
------------                ------------                   (MANY-to-ONE)
org_id               +----- course_id                      courses are
org_name             |      course_name                    run by many 
org_description      |                                     organisations
course_id       >----+


ORGANISATION                COURSE
------------                ------------                   (ONE-to-MANY)
org_id          -----+      course_id                      organisations
org_name             |      course_name                    run many 
org_description      +----< org_id                         courses


ORGANISATION        ORG_COURSE            COURSE           (MANY-to-MANY)
------------        ------------          -----------      organisations run 
org_id      ---+    id             +---   course_id        many course and
org_name       +-<  org_id         |      course_name      courses run by
                    course_id   >--+                       many organisations

Sir, this is actually i do to my database. but the problem is how to insert multiple course id into my organization table. So for example i have org_id which i want to insert the course into 1 org_id..

Link to comment
Share on other sites

  • Solution

inserting data to my table. 

 

to allow for both the possibility of adding a new or removing an existing course entry for an organization, you should probably use a set of radio buttons for each choice.

 

your form should submit the org_id, course_id, and if the course should be added or removed for that org_id. you would then remove any existing rows that have been selected for removal (the remove radio button was picked) and add any new rows that have been selected to be added (the add radio button was picked.) trying to delete rows that don't exist won't hurt anything and trying to insert duplicate rows, using the IGNORE keyword in the query, also won't hurt anything, so your logic doesn't need to try to determine which data has been changed, it can simply delete/insert whatever the submitted form data says to do.

 

i would set up the radio buttons with a two dimensional array name in the html, with the org_id value as the index for the first dimension and the course_id as the index for the second dimension. there would be two radio buttons with the same array index values, one radio button would have the value 'add' and the second would have the value 'remove'. you would pre-select the 'add' radio button based on if there is already a row in the database table and pre-select the 'remove' radio button if there is not already a row in the database table.

 

initially, with no data in the database table, all the remove radio buttons would be selected. an example of what the browser would show -

post-144491-0-81546600-1424024125_thumb.png

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.