Jump to content

Totally Stuck with Query!


benji87

Recommended Posts

Hi all im trying to create an update query but im stuck as to how to do it so any help would be great if i try and explain wat im trying to do:

 

This is what my sqltable looks like:

 

|  ID  |  SUBJECT  |  VALUE  |  USERNAME  |

    1        SUBJECT1        0              USER

    2        SUBJECT2        0              USER

    3        SUBJECT3        0              USER

 

---------------------------------------------------

 

Now the user fills in a form in which they tick next to the subjects that they would like to learn about which have tick boxes next to them. These tick boxes are named the same as the subjects and have a value of '1'.

 

When the user submits the form i want the table to update the values the column where the user had ticked the checkboxes with the subject name of the subject column. This could be either one column being updated or up to 20.

 

I just cant seem to figure out a query for this that would work  :(

Link to comment
Share on other sites

your problem is you did not do a proper plan. You should do normalization to reduce the reduduncy.

 

if many-to-many relationship, you should create another table. DO you understand?

 

the DB design should have one table for Subject: subject ID, subject name

 

new table: subject ID, user ID

 

cheers. hope this help.

 

 

Link to comment
Share on other sites

ur gonna need javascript for this

 

 

<script language="JavaScript" type="text/javascript">
       function MM_callJS()
            { 
                                
	document.form1.chkIds.value=document.form1.links.value
	for (i=0; i<document.form1.links.length; i++)
                       {
		 if (document.form1.links[i].checked==true)
			{

				document.form1.chkIds.value=document.form1.chkIds.value+","+document.form1.links[i].value

					}   
					  }
					         
		return true;		
		  }
</script>

 

 

now here u wud need a hidden field which is "chkIds" here and the name of the checkbox wud be "links"

as the checkbox is in the loop, the javascript takes the length or number of checkboxes generated-" document.form1.links.length" and gets the value of each selected box-"  if (document.form1.links.checked==true)"

 

then its passes it to the hidden field- "  document.form1.chkIds.value=document.form1.chkIds.value+","+document.form1.links.value"

 

with a separator '+' in between

 

let me knw if u gt this till here

Link to comment
Share on other sites

Thanks but cant seem how to get it to work. I obviously need to put the javascript in the page with the form that is being submitted. I then named all the checkboxes 'links' and put in a hidden field called 'chkIds'.

 

When i submit the form i thought i would try and echo the 'chkIds' post data but nothing is returned?

Link to comment
Share on other sites

ok wat i meant is

u wud have a table for subjects and a query to get them in the form

<form name="">

<table>

$res=mysql_query("select * from subject_tbl");

while($row=mysql_fetch_array($res))

{

?>

 

<tr>

<td><input type="checkbox" name="links" value="<?php echo $subject_name?>"></td><td><?php echo $subject_name;?></td>

</tr>

<?php }?>

</table>

<input type="hidden" name="chkIds">

</form>

 

 

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.