How To Insert Multiple Check Box Values into MySQL Database using PHP

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 49365 Views - Last Post: 03 March 2011 - 08:34 AM Rate Topic: -----

#1 kowsarjahan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-February 11

How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 21 February 2011 - 03:19 AM

I am working on php forms which contains multiple chekbox values. i want to insert only the selected check box values into database. i need help regarding this.
Is This A Good Question/Topic? 0
  • +

Replies To: How To Insert Multiple Check Box Values into MySQL Database using PHP

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 21 February 2011 - 03:34 AM

Hey.

For checkboxes, only those you select will be sent to the server. And if you name them all like boxes[] the selected boxes will be sent as an array.

Consider this example:
<!DOCTYPE html>
<html>
    <head><title>Example</title></head>
    <body>
        <form action="" method="post">
            <input type="checkbox" name="boxes[]" value="1">1</input>
            <input type="checkbox" name="boxes[]" value="2">2</input>
            <input type="checkbox" name="boxes[]" value="3">3</input>
            <br><input type="submit">
        </form>
        <pre><?php
            if(isset($_POST['boxes']))
            {
                foreach ($_POST['boxes'] as $_boxValue)
                {
                    echo "Box #{$_boxValue} was selected!\n";
                }
            }
        ?></pre>
    </body>
</html>


Using that technique, constructing a query is easy.

This post has been edited by Atli: 21 February 2011 - 03:34 AM

Was This Post Helpful? 2
  • +
  • -

#3 Guest_melly*


Reputation:

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 27 February 2011 - 07:34 PM

[quote name='melly' date='27 February 2011 - 07:30 PM' timestamp='1298860249' post='1265621']
[quote name='Atli' date='21 February 2011 - 03:34 AM' timestamp='1298284458' post='1258159']

According to your example..it tell us the box selected. So, can you show how do we insert in mysql db?
Was This Post Helpful? 0

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 27 February 2011 - 08:14 PM

That example shows how you fetch the data from the client. Once you have that part down it's just a matter of building the SQL query and sending it, just like you would do with any other MySQL query.

If you are new to using MySQL and PHP I suggest you start by reading this tutorial. Should get you started. If you run into any problems post your code here and we'll take a look.
Was This Post Helpful? 0
  • +
  • -

#5 Snipeye  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 19-September 09

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 27 February 2011 - 09:38 PM

HOLY CRAP. If I had seen and/or thought of that before I made a ridiculously complex system (force all displayed checkbox names into an array, serialize the array, base 64 encode the array, pass as a hidden variable, decode the base 64, unserialize, then have an array to check names against values) it would have been a LOT easier.
Was This Post Helpful? 0
  • +
  • -

#6 stepbystep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 11

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 01 March 2011 - 07:50 AM

In this form, I want the selected food to be insert to sql. But, it seem there are a problem.

<body>
<?php
$con = mysql_connect("127.0.0.1","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("restaurantordersystem", $con);


if(isset($_POST['food'])) 
        {
			$sql="INSERT INTO t1 (Food);
		} 

if (!mysql_query($sql,$con))
  {
  die;
  }

mysql_close($con)
?>

<form action="FoodAllergies.php" name="food" method="post" style="height: 849px; width: 727px; background-color: #008080;" class="style5">

	<div class="style4" style="height: 846px">

<br /><h1 class="style2" style="height: 809px"/><strong><span class="style8">LET'S ORDER FOOD</span></strong><br />
<br />

<table border="1" class="style3" align="center">
<tr>
<th>&nbsp;</th>
<th style="width: 268px">FOOD</th>
<th style="width: 87px">PRICE</th>
<th style="width: 109px">QUANTITY</th>
<th style="width: 101px">TOTAL (RM)</th>
</tr>
<tr>


<td><input name="food" type="checkbox" value ="Original Fried Chicken" onclick="changeDisplayofchicken(this.checked)"/></td>
<td class="style9" style="width: 268px; ">&nbsp;&nbsp; Original Fried Chicken (2pc) &nbsp;&nbsp;
<input name="btnofchicken" src="http://ocw.ksu.edu.tw/file.php/249/openclipart/album/TzeenieWheenie/TzeenieWheenie_Small_blue_i_info_button.png" type="image" onclick="return myPopupofchicken()" height="20" width="20"/></td>
<td style="width: 87px">RM 5.00&nbsp;&nbsp;&nbsp; </td>
	
<td style="width: 109px">&nbsp;&nbsp;&nbsp;	
<select name="ofchicken" class="ff" id="ofchicken" disabled="disabled" style="width: 37px">
	<option value="0">0</option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>  
	<option value="4">4</option>  
	<option value="5">5</option>
	<option value="6">6</option>
	<option value="7">7</option>
	<option value="8">8</option>
	</select></td>
	
<td style="width: 101px"><input type="text" name="totalofchicken" id="totalofchicken" value="0.00" style="width: 70px"/></td>




i'm melly..i tried the technique but it doesn't work. Can you help me to detect the error?
To understand more what i want the system to be like, i attach my system.

Attached File(s)


This post has been edited by stepbystep: 01 March 2011 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

#7 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 01 March 2011 - 08:18 AM

What i would do is save the value of selected Check-box in hidden field or <form> value and send that value to the database


Modify as follows

<input name="food" type="checkbox" value ="Original Fried Chicken" onclick="ShowValue(this);"/>


And Javascript

<script type ="text/javascript">
    function ShowValue(source)
    {
        alert(source.value);
        //Add values in hidden field for PHP to insert into db
    }

</script>


BTW:

error in your php

this should

 $sql="INSERT INTO t1 (Food); 



be this


 $sql="INSERT INTO t1 (Food)";


Was This Post Helpful? 1
  • +
  • -

#8 stepbystep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 11

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 01 March 2011 - 08:31 AM

Thank you for your help and guide. I already implement it in my coding but there are still error.
Was This Post Helpful? 0
  • +
  • -

#9 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 01 March 2011 - 08:55 AM

What is the error :dangole:
Was This Post Helpful? 0
  • +
  • -

#10 stepbystep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 11

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 01 March 2011 - 09:07 AM

it written
Notice: Undefined variable: sql in C:\Program Files\EasyPHP-5.3.2\www\OrderFood.php on line 389
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 01 March 2011 - 09:10 AM

It means you didn't declare that variable anywhere. You must declare your variables before you can use them.
Was This Post Helpful? 0
  • +
  • -

#12 stepbystep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 11

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 01 March 2011 - 09:15 AM

sorry. I am new here. so, i am clueless.can i know where the appropriate place to issues my problem?
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 01 March 2011 - 09:18 AM

Your error message tells you. Without seeing your relevant code, we can't help more.
Was This Post Helpful? 0
  • +
  • -

#14 stepbystep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 11

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 01 March 2011 - 09:21 AM

I've already attach my full coding.
Was This Post Helpful? 0
  • +
  • -

#15 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: How To Insert Multiple Check Box Values into MySQL Database using PHP

Posted 01 March 2011 - 09:26 AM

Where is $sql declare??

It looks to me $sql is declared out of the scope of you if statement


if(isset($_POST['food']))
{
$sql="INSERT INTO t1 (Food)";
}

if (!mysql_query($sql,$con))//Error Occurs here because $sql is declared out of the scope of this line
{
die;
}
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2