CanMan2004 Posted June 30, 2006 Share Posted June 30, 2006 Hi allI 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;[code]<form action="" method="post">Item 1 <input type="checkbox" name="item" value="1"><br>Item 2<input type="checkbox" name="item" value="2"><br>Item 3<input type="checkbox" name="item" value="3"></form>[/code]So basically if the value;2,4,5appears 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;1,3,4,and I ticked the box with the value '2' then it would look like;1,2,3,4if I then unticked box value '3', it would edit the field to;1,2,4The sql code I use to insert the value currently is[code]<?session_start();$sql ="UPDATE pics SET item='".$_POST['item']."' WHERE id = '".$_POST['idnum']."'";@mysql_query($sql, $connection) or die(mysql_error());exit;?>[/code]Is this possible? I think it is by using explode.Any help or pointers would be very helpful.Thanks in advanceDave Quote Link to comment https://forums.phpfreaks.com/topic/13322-adding-to-a-field/ Share on other sites More sharing options...
Barand Posted June 30, 2006 Share Posted June 30, 2006 Create your c/boxes on the form as, say, <input type="checkbox" name="cb[]" value="1"><input type="checkbox" name="cb[]" value="2">etcOn posting, $vals = join(',' , $_POST['cb']); Quote Link to comment https://forums.phpfreaks.com/topic/13322-adding-to-a-field/#findComment-51372 Share on other sites More sharing options...
jvrothjr Posted June 30, 2006 Share Posted June 30, 2006 My guess on this one would be an array[code]$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; }}[/code]$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,0in 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,01 meaning checked0 meaning uncheckedand 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. Quote Link to comment https://forums.phpfreaks.com/topic/13322-adding-to-a-field/#findComment-51377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.