?
Solved

Insert into database if checkbox checked

Posted on 2010-08-13
8
Medium Priority
?
579 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Assisted Solution

by:oshirowanen
oshirowanen earned 200 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 1200 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 200 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
Industry Leaders: 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 400 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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