[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Insert into database if checkbox checked

Posted on 2010-08-13
8
Medium Priority
?
588 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 dynamically set the form action using jQuery.
Suggested Courses

656 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