473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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,purpose)VALUES('$type','$other','$purp ose');

$result =mysql_query($query);

if(!$result){die(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%"><font 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%"><font 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><font 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
Jan 14 '08 #1
11 15224
code green
1,726 Expert 1GB
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,purpose)VALUES('$type','$other','$purp ose')";[/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
Jan 14 '08 #2
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
Jan 14 '08 #3
code green
1,726 Expert 1GB
You need to expand this a little
[PHP]$type=$_POST['type'];
$query ="insert into form
(type,other,purpose)VALUES('$type','$other','$purp ose')";
$result =mysql_query($query);[/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,purpose)VALUES('$types','$other','$pur pose')";
$result =mysql_query($query);
}[/PHP] or generate a VALUES string, the syntax is trickier[PHP]$type=$_POST['type'];
$values = '';
foreach($type as $types)
{
$values .= "('$types','$other','$purpose'),";
}
//Remove the loose comma
$values = trim($values,',');
$query ="insert into form
(type,other,purpose)VALUES $values";
$result =mysql_query($query);
}[/PHP] But please double check this.
Jan 14 '08 #4
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
Jan 15 '08 #5
code green
1,726 Expert 1GB
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
Jan 15 '08 #6
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.textfield,text area,radiobuttons..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,textfield checkboxes..etc and not only of checkboxes, it would be more than convenient for me.

Thanx and Regards
TechnoAtif
Jan 16 '08 #7
code green
1,726 Expert 1GB
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_name <? 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
Jan 16 '08 #8
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..if u can guide me to the end of this solution..that would be great...Otherwise 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($query);

if(!$result){die(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%"><font 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%"><font 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
Jan 23 '08 #9
code green
1,726 Expert 1GB
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?
Jan 23 '08 #10
nathj
938 Expert 512MB
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="selected" (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
Jan 23 '08 #11
code green
1,726 Expert 1GB
Thanks for that nathj, now I see the light.
TechnoAtif,
Expand|Select|Wrap|Line Numbers
  1. DO YOU SEE THE LIGHT
Jan 23 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: nicholas | last post by:
Where can I find a good tutorial on how to insert/update MULTIPLE rows into a database on an SQL2000 server ? In VB !! (not C#) THX
1
by: swapnil shingvi via .NET 247 | last post by:
(Type your message here) -------------------------------- From: swapnil shingvi ----------------------- Posted by a user from .NET 247 (http://www.dotnet247.com/) ...
5
by: rbragg | last post by:
All of my other form data is stored correctly in the db except for my checkbox data. This column in my table is empty. I have this checkbox group on my form: <input name="cbItems"...
1
by: Sharon | last post by:
Hello All, Is it possible to update Sql Table through DataGrid. I have a DataGrid which is being populated through a stored procedure, all i wanted to do is to update one field...
0
by: ramananth | last post by:
i want to update the xml data using pl/sql.can u please give me the pl/sql code for updation.
0
by: DC | last post by:
How can I update data using a Details view without using an AccessDataSource and its associated Insert, Update and Delete but using a DataReader instead?
3
by: simple simon | last post by:
How would I insert into multiple related tables using a table valued parameter? Is there any way to do this without using a WHILE loop? I know how to insert from a table valued parameter into one...
0
by: kumardharanik | last post by:
Hi Friends, The below code is a sample code for insert, update and delete using datagrid but i need to convert the entire code for datagridview.. Plsss help me.. using System; using...
1
by: saifulhaque | last post by:
Hi Dear How can insert, Display ,update multiple records simultaneously through GridView in Asp.net. I have Button for insert, update . I want insert, update multiple records through a GridView...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.