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

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 622 Views - Last Post: 12 May 2014 - 11:22 AM Rate Topic: -----

#1 Desertt  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 27-October 13

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

Posted 29 April 2014 - 10:13 AM

Hi i have this form
<html>
<body>
<form method="post" action="chk123.php">
Flights on: <br/>
<input type="checkbox" name="Days[]" value="1">Daily<br>
<input type="checkbox" name="Days[]" value="2">Sunday<br>
<input type="checkbox" name="Days[]" value="3">Monday<br>
<input type="checkbox" name="Days[]" value="4">Tuesday <br>
<input type="checkbox" name="Days[]" value="5">Wednesday<br>
<input type="checkbox" name="Days[]" value="6">Thursday <br>
<input type="checkbox" name="Days[]" value="7">Friday<br>
<input type="checkbox" name="Days[]" value="8">Saturday <br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>


and this is the php
<?php

// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("table") or die(mysql_error());

$checkBox = implode(',', $_POST['Days']);

if(isset($_POST['submit']))
{       
    $query="INSERT INTO tablename(x) VALUES ('" . $checkBox . "')";     

    mysql_query($query) or die (mysql_error() );

    echo "Complete";

}

?>


The result i got in database if i checked the first 4 option is :
x : 1234

What i want is the result to be :
x : 10
What do i have to change in my php code ??

Is This A Good Question/Topic? 0
  • +

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

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 318
  • View blog
  • Posts: 1,173
  • Joined: 15-January 14

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.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

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

Posted 29 April 2014 - 10:19 AM

You are asking specifically about PHP. Moved to that forum.

Once you've added the values though, you won't be able to distinguish between someone checking 1,2,3 or 1 and 5.

This post has been edited by andrewsw: 29 April 2014 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 548
  • View blog
  • Posts: 2,449
  • Joined: 03-December 12

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

Posted 29 April 2014 - 11:55 AM

Just a question but, it looks as if you are using the check boxes to distinguish between days of flights, why then are you adding the numbers and changing days into a sum?
Was This Post Helpful? 0
  • +
  • -

#5 Desertt  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 27-October 13

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

Posted 29 April 2014 - 10:06 PM

That's just an example about how to add numbers before adding it to database.
How can i loop over $_POST['Days'] and add each of the values in sum and then insert sum into database ?
Was This Post Helpful? 0
  • +
  • -

#6 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 548
  • View blog
  • Posts: 2,449
  • Joined: 03-December 12

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.
Was This Post Helpful? 0
  • +
  • -

#7 Desertt  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 27-October 13

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
Was This Post Helpful? 0
  • +
  • -

#8 Desertt  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 27-October 13

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

Posted 06 May 2014 - 01:54 PM

I have this code
<html>
<body>
<form method="post" action="chk123.php">
Flights on: <br/>
<input type="checkbox" name="Days[]" value="1">Daily<br>
<input type="checkbox" name="Days[]" value="2">Sunday<br>
<input type="checkbox" name="Days[]" value="3">Monday<br>
<input type="checkbox" name="Days[]" value="4">Tuesday <br>
<input type="checkbox" name="Days[]" value="5">Wednesday<br>
<input type="checkbox" name="Days[]" value="6">Thursday <br>
<input type="checkbox" name="Days[]" value="7">Friday<br>
<input type="checkbox" name="Days[]" value="8">Saturday <br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>


and this is the php
<?php

// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("table") or die(mysql_error());

$checkBox = implode(',', $_POST['Days']);

if(isset($_POST['submit']))
{       
    $query="INSERT INTO tablename(x) VALUES ('" . $checkBox . "')";     

    mysql_query($query) or die (mysql_error() );

    echo "Complete";

}

?>



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 ?
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,000
  • Joined: 08-August 08

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

Posted 06 May 2014 - 02:01 PM

Read this twice, then study it for a while, and finally, start using what it teaches you:
http://www.dreaminco...duction-to-pdo/
Was This Post Helpful? 0
  • +
  • -

#10 Desertt  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 27-October 13

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

Posted 06 May 2014 - 02:08 PM

I am not asking if i use mysql or mysqli or PDO sorry but i need an answer to my question if you would
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

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.)

This post has been edited by andrewsw: 06 May 2014 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#12 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 548
  • View blog
  • Posts: 2,449
  • Joined: 03-December 12

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

Posted 06 May 2014 - 03:48 PM

Why didn't you continue this on your original post?

http://www.dreaminco...data-using-php/

This post has been edited by andrewsw: 06 May 2014 - 03:51 PM
Reason for edit:: n't

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

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.

It is a possibility worth mentioning though IMO.

View Postastonecipher, on 06 May 2014 - 10:48 PM, said:

Why didn't you continue this on your original post?

http://www.dreaminco...data-using-php/

I've merged the topics. @OP please don't create duplicate topics.
Was This Post Helpful? 0
  • +
  • -

#14 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 548
  • View blog
  • Posts: 2,449
  • Joined: 03-December 12

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

Posted 06 May 2014 - 03:57 PM

I think I would approach this differently the Andrew.

Have a days_of_week column. Store the days separated by a delimiter. Then check if it needed to be split when pulling the data out.

Selecting daily by default would be values 1-7 . Stored as 1,2,3,4,5,6,7

If anything is deselected while daily IS selected it would deselect daily.

When pulling the data run a check if it can be split . If not print the original value.

This post has been edited by astonecipher: 06 May 2014 - 03:58 PM

Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

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

Posted 06 May 2014 - 04:05 PM

View Postastonecipher, on 06 May 2014 - 10:57 PM, said:

Selecting daily by default would be values 1-7 . Stored as 1,2,3,4,5,6,7

If anything is deselected while daily IS selected it would deselect daily.

If Daily is selected then possibly the individual days could be deselected and disabled, but it doesn't really matter which way round this happens ;)

I suppose Daily could be represented by the number 8 but I would probably have it as a separate field; if it is chosen (true) ignore the other Days-field.

“You make your choices, and you live with them”
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2