Solved

Insert into database if checkbox checked

Posted on 2010-08-13
8
563 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
 

Author Comment

by:petewinter
ID: 33428246
oshirowanen - Thanks for the reply. Can you me demonstrate the postback with php with sample code please.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now