Jump to content


Adding to a field

  • Please log in to reply
2 replies to this topic

#1 CanMan2004

  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 30 June 2006 - 06:01 PM

Hi all

I have a question about updating data in a field in my sql database. Basically im storing numbers in a field called "items", they are stored as comma seperated values, so for example;

22, 34, 65,

Currently I have to manually enter this, but I want to be able to add values to that field or remove values by using html tick boxes, the html code I made looks like;

<form action="" method="post">
Item 1 
<input type="checkbox" name="item" value="1">
Item 2
<input type="checkbox" name="item" value="2">
Item 3
<input type="checkbox" name="item" value="3">

So basically if the value;


appears in the database, I want to be able to add a number to that sequence or remove a number by ticking or unticking the html tick boxe. An example would be if the field contained;


and I ticked the box with the value '2' then it would look like;


if I then unticked box value '3', it would edit the field to;


The sql code I use to insert the value currently is

$sql ="UPDATE pics SET item='".$_POST['item']."' WHERE id = '".$_POST['idnum']."'";
@mysql_query($sql, $connection) or die(mysql_error());

Is this possible? I think it is by using explode.

Any help or pointers would be very helpful.

Thanks in advance


#2 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 30 June 2006 - 06:30 PM

Create your c/boxes on the form as, say,

<input type="checkbox" name="cb[]" value="1">
<input type="checkbox" name="cb[]" value="2">

On posting,

$vals = join(',' , $_POST['cb']);
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 jvrothjr

  • Members
  • PipPipPip
  • Advanced Member
  • 300 posts
  • LocationOhio

Posted 30 June 2006 - 06:35 PM

My guess on this one would be an array

$max = 5;
For ($i=1; $i <= $max; $i++) {
	if ($item[$i] == '1') {
		echo "<input type='checkbox' name='item' value='".$item[$i]."' checked>".$i;
	} else {
		echo "<input type='checkbox' name='item' value='".$item[$i]."'>".$i;

$max being the max number of Options this sample I am using 5 as the max

$item[] array would hold the value for the field example 0,1,1,1,0
in this array it would not create the array as you have stated 1,2,4 this style would create that string as 1,1,0,1,0

1 meaning checked
0 meaning unchecked

and within your loop you would see whether its checked or unchecked and very record entered in the DB would hold the same 5 values in this field but it would be 0 or 1 and not the number to display checked.

If you understand everything you know nothing!


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users