Solved

Insert into database if checkbox checked

Posted on 2010-08-13
8
575 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 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 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