Solved

Insert into database if checkbox checked

Posted on 2010-08-13
8
569 Views
Last Modified: 2013-12-13
See my attached code. How do I only insert into the database if the checkbox has been checked?
<label>
      <input name="checkbox_add" type="checkbox" id="checkbox_add" value="1" />
    </label>
    <?php
	$media_id = $row_rs_media['id'];
	$customer_id = $row_rs_customer_selected['id'];
	$user_id = $row_rs_users['id'];
	if($_POST) {
        mysql_query("INSERT INTO bespoke_pricelist (media_id, customer_id, user_id) VALUES ('$media_id', '$customer_id', '$user_id')");
    }
?>

Open in new window

0
Comment
Question by:petewinter
8 Comments
 

Assisted Solution

by:oshirowanen
oshirowanen earned 50 total points
ID: 33428203
You can use jQuery or a post back for this.  Capture the postback with php, check the value of the checkbox.  Then write an if statement to only insert into the database if the checkbox is checked.
0
 
LVL 22

Accepted Solution

by:
Om Prakash earned 300 total points
ID: 33428236
Try:

$value = $_POST["array_checkboxes"];
    if($value=="1")
{
  mysql_query("your INSERT statement");
}
0
 
LVL 19

Assisted Solution

by:Bardobrave
Bardobrave earned 50 total points
ID: 33428240
A checked checkbox will have a value of "-1", while an unchecked one will have a value of "0".

You can compare checkbox object value to 0/-1 to know if it's checked or you can check if it's "selected" attribute is equal to "selected" or it's empty.

Sometimes checkbox are difficult to work with.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:petewinter
ID: 33428246
oshirowanen - Thanks for the reply. Can you me demonstrate the postback with php with sample code please.
0
 
LVL 14

Assisted Solution

by:Kalpan
Kalpan earned 100 total points
ID: 33428249
Please refer the below example

http://www.daniweb.com/forums/thread123187.html
0
 

Author Comment

by:petewinter
ID: 33428992
Still no sure. Can you please look at my attached code and let me know what is wrong.
<label>
      <input name="checkbox_add" type="checkbox" id="checkbox_add" />
    </label>
    <?php
	$media_id = $row_rs_media['id'];
	$customer_id = $row_rs_customer_selected['id'];
	$user_id = $row_rs_users['id'];
	
	$mm_value = 0;
    if( !empty($_POST["checkbox_add"]) ) $mm_value = 1;
				  
    if($value=="1") 
{
        mysql_query("INSERT INTO bespoke_pricelist (media_id, customer_id, user_id) VALUES ('$media_id', '$customer_id', '$user_id')");
    }
?>

Open in new window

0
 

Author Comment

by:petewinter
ID: 33429007
Sorry correction. See code.
<?php
	$media_id = $row_rs_media['id'];
	$customer_id = $row_rs_customer_selected['id'];
	$user_id = $row_rs_users['id'];
	
	$mm_value = 0;
    if( !empty($_POST["checkbox_add"]) ) $mm_value = 1;
				  
    if($mm_value=="1") 
{
        mysql_query("INSERT INTO bespoke_pricelist (media_id, customer_id, user_id) VALUES ('$media_id', '$customer_id', '$user_id')");
    }
?>

Open in new window

0
 

Author Closing Comment

by:petewinter
ID: 33429658
Thanks. You have help me solve the issue.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question