Insert And Update Multiple Checkbox Data Using Phpmysql Or Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TechnoAtif
    New Member
    • Sep 2007
    • 63

    Insert And Update Multiple Checkbox Data Using Phpmysql Or Javascript

    INSERT AND UPDATE MULTIPLE CHECKBOX DATA USING PHPMYSQL OR JAVASCRIPT




    Hi All

    I want to check the multiple checkboxes update them after revisiting that page.
    I am taking the name as name=type[]..in the form of array..but on checking it the result in the database simply gives the world 'array'.i want multiple checkboxes to be checked and updated simultaneously. .
    and the query to insert the checkbox value has to be included along with the query performed to insert data for rest of the form..

    Here is the code:

    [PHP]
    <?

    include "dbconnect.php" ;
    //error_reporting ('notice');
    if(isset($_POST['submit']))
    {//handle the form
    $message=NULL;

    $type=$_POST['type'];
    $query ="insert into form
    (type,other,pur pose)VALUES('$t ype','$other',' $purp ose');

    $result =mysql_query($q uery);

    if(!$result){di e(mysql_error() );}
    else {echo 'Record Inserted';}



    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" ><table width="90%" align=center cellpadding=2 class=medium>
    <tbody>
    <tr>
    <td width="42%"><fo nt color="#333333" size="1"
    face="Verdana, Arial, Helvetica, sans-serif"><br>
    <input type=checkbox name=type[] value=Logo/Brochure>

    Logo/Brochure </font></td>
    <td><font color="#333333" size="1"
    face="Verdana, Arial, Helvetica, sans-serif"><br>
    <input type=checkbox value= Software/Web application development name=type[]>
    Software/Web Application Development</font></td>
    </tr>
    <tr>
    <td width="42%"><fo nt color="#333333" size="1"
    face="Verdana, Arial, Helvetica, sans-serif"><br>
    <input type=checkbox value=CD presentations name=type[]>
    CD presentations</font></td>
    <td><font color="#333333" size="1"
    face="Verdana, Arial, Helvetica, sans-serif"><br>
    <input type=checkbox value=2D/3D animations name=type[]>
    2D/3D animations</font></td>
    </tr>
    <tr>
    <td><font color="#333333" size="1"
    face="Verdana, Arial, Helvetica, sans-serif"><br>

    <input type=checkbox value= Web banner/intro name=type[]>
    Web Banner/Intro</font></td>
    <td><font color="#333333" size="1"
    face="Verdana, Arial, Helvetica, sans-serif"><br>
    <input type=checkbox value=E-learning name=type[]>
    E-learning </font></td>
    </tr>
    <tr>
    <td><p><font color="#333333" size="1"
    face="Verdana, Arial, Helvetica, sans-serif"><br>
    If other, please explain: </font></p></td>
    <td valign=bottom>< div align=right><fo nt color="#333333" size="1"
    face="Verdana, Arial, Helvetica, sans-serif">
    <input name="other" type="text" size="50" value="<?php if(isset($_POST['other'])) echo($_POST['other'])?>" >
    </font></div></td>
    </tr>
    </tbody>
    </table></form>[/PHP]
    If the solution is in javascript still its fine with me

    So this is the code for checkbox...if any have the solution..plz post it

    Thanx and Regards
    TechnoAtif
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    am taking the name as name=type[]..in the form of array..but on checking it the result in the database simply gives the world 'array'
    You have surely answered your own question. If it is an array, and you insert an array you will get an array.
    You can't expect MySql to handle this as you expect?
    [PHP]$query ="insert into form
    (type,other,pur pose)VALUES('$t ype','$other',' $purpose')";[/PHP]
    The above query either needs to be executed multiple times with a single value in place of $type',
    or you need to constrct a multiple values INSERT query

    Comment

    • TechnoAtif
      New Member
      • Sep 2007
      • 63

      #3
      HI CODEGREEN...
      thanx for your reply...but i was unable to get what u want to explain my..can you please elaborate a bit.Or simply can u please tell me how to
      check multiple checkboxes using arrays and insert them into php mysql table irrespective of my code.
      ..plz help if you can

      Thanx and regards
      TechnoAtif

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        You need to expand this a little
        [PHP]$type=$_POST['type'];
        $query ="insert into form
        (type,other,pur pose)VALUES('$t ype','$other',' $purpose')";
        $result =mysql_query($q uery);[/PHP] Because $type is not being handled at all;
        I mentioned two solutions [PHP]$type=$_POST['type'];
        foreach($type as $types)
        {
        $query ="insert into form
        (type,other,pur pose)VALUES('$t ypes','$other', '$purpose')";
        $result =mysql_query($q uery);
        }[/PHP] or generate a VALUES string, the syntax is trickier[PHP]$type=$_POST['type'];
        $values = '';
        foreach($type as $types)
        {
        $values .= "('$types','$ot her','$purpose' ),";
        }
        //Remove the loose comma
        $values = trim($values,', ');
        $query ="insert into form
        (type,other,pur pose)VALUES $values";
        $result =mysql_query($q uery);
        }[/PHP] But please double check this.

        Comment

        • TechnoAtif
          New Member
          • Sep 2007
          • 63

          #5
          Its good..thanx for your solution..it has started inserting checkbox values into mysql database.I guess you are very good at php. I have been wondering for this solution but all in vain until you gave the right answer.Thanx once again.

          However still there is another problem.The multiple checked data has started to be inserted into the mysql database.Now in the next page if i want to update those checked data from some new checked data,in that case can you please show some light. I mean to say that when i open the edit page ,at first the earlier checked data be visible.After that when i check some other data in the checkbox and click the submit buttton,in the database this updated data should be visible.
          It would be great to seek your help

          Thanx and Regards
          TechnoAtif

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            However still there is another problem
            .
            The description that follows is not a problem, it is a requirement for a full script.
            I'm sorry I just don't have time for that.

            But basically you can pass data from the current script in the same way.
            The new script needs a SELECT query reading the current data from the database.
            This data needs displaying in text boxes so it can be edited.
            Then a Submit button calls the same scipt but is directed to code that can UPDATE the database first with the new data.
            Then the same code that generates the SELECT above query runs again displaying the new data..
            It can all be done in one script with one form and one submit button.
            If you make a start I can help

            Comment

            • TechnoAtif
              New Member
              • Sep 2007
              • 63

              #7
              Hi CodeGreen!!!
              Thanx for the reply
              yes i have tried that but the checked sates of the checkboxes is still not visible.
              I want the entry of those checked boxes to be made alog with other items..for example.textfie ld,text area,radiobutto ns..etc. If i update the check boxes ina separate form where i have only checkboxes then it may work but along with the other items i just get messed up..eg

              [PHP]update form set
              check='$check',
              design='$design '
              .
              .
              .where id='$id'[/PHP]

              do i have to take chekbox entries in a separate table...i want to make the entry of all the form items through the same query and in the same table.
              Please help...if u 'll say then i can post the code also...but its is so lengthy that u will loose yourself in it.
              Thats why if you can help me with a fresh code..that could be a combination of more than items.i.e. listboxes,textf ield checkboxes..etc and not only of checkboxes, it would be more than convenient for me.

              Thanx and Regards
              TechnoAtif

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                You are beginning to lose me.
                But if you want checkboxes to be re-appear on another page, still in their same state,
                that is checked or unchecked, then in the new page you need something like
                [PHP]<input type=checkbox name=checkbox_n ame <? php if(isset($_POST['checkbox_name'])) echo 'checked'; ?>[/PHP]radio buttons and checkboxes are not passed in the POST variables if they are not selected

                Comment

                • TechnoAtif
                  New Member
                  • Sep 2007
                  • 63

                  #9
                  You are beginning to lose me.
                  But if you want checkboxes to be re-appear on another page, still in their same state,
                  Hi CodeGreen and thanx for the reply...
                  I have managed to insert the checkbox checked data into mysql database.Below is the code for it.Now the main problem ,as i earlier mentioned is,how to make the checked status of the checkbox visible in the next page while updating the data.

                  I want that as i open the update/edit form the already cheked checkboxes be displayed..and later on i could update them by cecking on some other or the same checkbox.This updated data should also be updated i the datanase.
                  The same goes in case of Radiobuttons also.

                  I have managed to insert and update data for the entire form.Only checkbox and radiobutton are remaining.So..i f u can guide me to the end of this solution..that would be great...Otherwi se also it would be great [:)]
                  Here goes the code....

                  [PHP]

                  if(isset($_POST['submit']))
                  {//handle the form
                  $message=NULL;
                  $type=$_POST['type'];
                  $type = implode(",", $type);

                  $query ="insert into form (type) VALUES('$type') ;
                  $result =mysql_query($q uery);

                  if(!$result){di e(mysql_error() );}
                  else {echo 'Record Inserted';}
                  <form name="form" id="form" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" >

                  <table width="90%" align=center cellpadding=2 class=medium>
                  <tbody>
                  <tr>

                  <td width="51%"><fo nt color="#333333" size="1"
                  face="Verdana, Arial, Helvetica, sans-serif"><br>
                  <input type=checkbox id=1 value="Logo/Brochure " name=type[0]>
                  Logo/Brochure </font></td>
                  <td><font color="#333333" size="1"
                  face="Verdana, Arial, Helvetica, sans-serif"><br>
                  <input type=checkbox id="2" value= Software/Web Application Development name=type[1]>
                  Software/Web Application Development</font></td>
                  </tr>
                  <tr>
                  <td width="51%"><fo nt color="#333333" size="1"
                  face="Verdana, Arial, Helvetica, sans-serif"><br>
                  <input type=checkbox id=3 value=CD presentations name=type[2]>
                  CD presentations</font></td>
                  <td><font color="#333333" size="1"
                  face="Verdana, Arial, Helvetica, sans-serif"><br>
                  <input type=checkbox id=4 value=2D/3D animations name=type[3]>
                  2D/3D animations</font></td>
                  </tr>
                  <tr>
                  <td><font color="#333333" size="1"
                  face="Verdana, Arial, Helvetica, sans-serif"><br>
                  <input type=checkbox id=5 value= Web Banner/Intro name=type[4]>
                  Web Banner/Intro</font></td>
                  <td><font color="#333333" size="1"
                  face="Verdana, Arial, Helvetica, sans-serif"><br>
                  <input type=checkbox id=6 value=E-learning name=type[5]>
                  E-learning </font></td>
                  </tr>
                  [/PHP]

                  Here i have used array values like...type[0],type[1]....type[6].

                  using the same table and array now these checkboxes are to be updated..So please help...if you can..

                  Thanx and Regards
                  TechnoAtif

                  Comment

                  • code green
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 1726

                    #10
                    I don't understand your problem.
                    Now the main problem ,as i earlier mentioned is,how to make the checked status of the checkbox visible in the next page.
                    If checked they are passed through the POST array. You read the POST variables and re-create the checkboxes
                    while updating the data
                    Which data where?
                    I want that as i open the update/edit form the already cheked checkboxes be displayed
                    Are you trying to pass the values from one page to another or get the values from a database?..
                    and later on i could update them by cecking on some other or the same checkbox
                    You can only check or uncheck a checkbox.
                    This updated data should also be updated i the datanase.
                    What updated data. We are looking at checkboxes.
                    using the same table and array now these checkboxes are to be updated
                    Again, what do you mean by updated?

                    Comment

                    • nathj
                      Recognized Expert Contributor
                      • May 2007
                      • 937

                      #11
                      Hi,

                      Sorry to just wade in on this but I think I understand what TechnoAtif is after.

                      He has a page with a form on it that visitors to the siter use to submit details. This is where the first, or original issue was, which, thanks to CodeGreen, has been solved.

                      Following this users can return to the site and edit the information they supplied. In this case the form needs to display the information from the database. In the case of text boxes it's simply the value. But in the case of select boxes you have to set one option to have selected="selec ted" (I think).

                      The particular trouble in this case is how to ensure the check boxes appear correct.

                      I will assume (I haven't read all the code) that you have a field in a table that is a tinyInt for storing the values set in check boxes. So if it is 0 the box was not checked and if it is 1 the box was checked.

                      So as you load the form populated with data when you get to displaying the check boxes simply set the checked property to true if the data is 1 otherwise set it to false.

                      This, I think is all you are after. If I've mis-understood then sorry but I will be in the same place as CodeGreen - lost!

                      Cheers
                      nathj

                      Comment

                      • code green
                        Recognized Expert Top Contributor
                        • Mar 2007
                        • 1726

                        #12
                        Thanks for that nathj, now I see the light.
                        TechnoAtif,
                        Code:
                        DO YOU SEE THE LIGHT

                        Comment

                        Working...