Replies To: Add the value of checbox and save them in data using php

Re: Add the value of checbox and save them in data using php

Posted 29 April 2014 - 10:18 AM

One issue with your code is that you're trying to get the check boxes before checking if the form was submitted. If the form wasn't submitted then that's going to be an error. You should also check if $_POST['Days'] is set, it will not be set if they submitted the form but did not check any boxes. You're also using the mysql extension, which means your code is vulnerable to SQL injection attacks.

Right now you are imploding the array of values into a comma-separated string, and putting that in the database. It sounds like instead you want to loop over $_POST['Days'] and add each of the values up, then insert the total in the database.

Re: Add the value of checbox and save them in data using php

Posted 30 April 2014 - 12:18 AM

You would need a for each loop to loop over the values passes and then you could use intval() to convert the strings to their numeric forms. It would need some checking to ensure the passed values are numeric equivalents though.

Re: Add the value of checbox and save them in data using php

Posted 05 May 2014 - 05:22 AM

Let's say that i have a database contain the day of the week
how can i insert the value of each day in data like if checkbox of monday is checked i want to enter in data for monday the value 1 and if other checkbox is checked i want to enter in near the day of week checked the value is there anyway i can do it

If i create a database include all the day of the weeks how can i check which day is checked and enter it's value in the database ?
example : if user checked monday and saturday the data of each will hold , Monday value = 3 and saturday value = 8 any help ?

Re: Add the value of checbox and save them in data using php

Posted 06 May 2014 - 03:41 PM

Your question is not entirely clear to me, but if they enter 3 (Monday) and 8 (Saturday) so that your field might store the string "38" then you would typically used explode() to get the individual values back.

Personally, I would probably use 8 separate bit/binary fields for these values. (I know that this would look, technically, wrong but I think it's probably overkill to create a related table to store these values.)

Re: Add the value of checbox and save them in data using php

Posted 06 May 2014 - 03:53 PM

I am guessing (I haven't checked) that php is not under an obligation to supply the values in the order 3,8 and, depending how another page might be set-up, there is the possibility that 8,3 could be returned instead. I doubt this for the current, uniform page set-up though.