Forum Moderators: coopster

Message Too Old, No Replies

Multiple checkbox headache

Multiple checkbox

         

smartie

9:46 pm on Jan 12, 2008 (gmt 0)

10+ Year Member



hi all

first post please be gentle with me

the code is as below

//$subject = empty($_POST['subject[]')? die ("ERROR: Select from subject") : mysql_escape_string($_POST['subject']);
$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");
mysql_select_db($db) or die ("Unable to connect to database");
$result = @mysql_query("SELECT "$select FROM partnership WHERE $subject[] ORDER BY $collage " "or die (mysql_error));
//$result = mysql_query($query) or die (mysql_error());
$select = implode(", ", $_post["view"]);

and the checkbox code is
<font face="Arial" color="#004080" size="1">
<form action="check9.php" method="post">
<input type="checkbox" name="subject[]" value="arable"> for Arable<br>
<input type="checkbox" name="subject[]" value="dairy"> for Dairy<br>
<input type="checkbox" name="subject[]" value="beefsheep"> for Beef and Sheep<br>
<input type="checkbox" name="subject[]" value="pigs"> for Pigs<br>
<input type="checkbox" name="subject[]" value="poultry"> for Poultry<br>
<input type="checkbox" name="subject[]" value="engineering"> for Engineering<br>
<input type="checkbox" name="subject[]" value="foodchain"> for Food Chain<br>
<input type="checkbox" name="subject[]" value="management"> for Env Management<br>
<input type="Submit" value="Submit" name="Submit">

I wont to be able to click a maxinum of 3 boxes from 8 and query the database for say 1 box 2boxes or 3boxes the first lot of code is now such a mess i cant sort it out

I have searched and searched for the answer but my head just cant get round it please help

NIgel

venelin13

10:07 pm on Jan 12, 2008 (gmt 0)

10+ Year Member



Hello and welcome to webmasterworld!

Well, there are some issues with the database-related part of your code.

Issue #1 - mysql_escape_string()
The function accepts STRING as parameter and you pass an array. It wouldn't work.

Issue #2 - mysql_escape_string()
This is a depricated function. You should use mysql_real_escape_string() [php.net] instead.

Issue #3 - reorder
Look at this code:


$result = @mysql_query("SELECT "$select FROM partnership WHERE $subject[] ORDER BY $collage " "or die (mysql_error));
//$result = mysql_query($query) or die (mysql_error());
$select = implode(", ", $_post["view"]);

You must have the $select = ... part, before the mysql_query() call.

Also, the $subject[] array - you can not use it in such way. You should transform it somehow to a string.

Just an advice - when your code become mess, sometimes it is east to delete it and start from the beginning, instead of dive into mess-up code and looking for the errors. Sometimes, this saves me a lot of time.

smartie

11:21 am on Jan 13, 2008 (gmt 0)

10+ Year Member



thankyou

I have started again but still cant sort the query out

<form name="services" action="check9.php" method=post>

<table border=1 cellspacing=1 cellpadding=1 width=70% bordercolor='#0000ff' align="center">
<tr>
<td>
<input type="checkbox" name="services[]" value="1">Arible
<input type="checkbox" name="services[]" value="2">Diary
</td>
<td>
<input type="checkbox" name="services[]" value="3">Beek & heep
<input type="checkbox" name="services[]" value="4">Pigs
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="services[]" value="5">Poulty
<input type="checkbox" name="services[]" value="6"> Engineering
</td>
<td>
<input type="checkbox" name="services[]" value="7">Food Chain
<input type="checkbox" name="services[]" value="8">Env Management
<input type="Submit" value="submit" name="Submit">

</td>
</tr>
</table>

And here is my form which is for retriving values

<?

include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if(isset($_POST["services"]))
{
$services = $_POST["services"];
}
else
{
$services=array();
}
$qrySel = "select * from partnership ";
echo $qrySel;
$result = mysql_query($qrySel);
?>
</form>

the error I keep getting is ERROR: Select from subject

you must be able to search from 3 checkboxes

thanks for any comment

Nigel

jatar_k

3:01 pm on Jan 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld smartie,

the code in the first post is a bit of a mess, not too hard to fix

you have this

$result = @mysql_query("SELECT "$select FROM partnership WHERE $subject[] ORDER BY $collage " "or die (mysql_error));
//$result = mysql_query($query) or die (mysql_error());
$select = implode(", ", $_post["view"]);

the first line has a ) in the wrong place, it should be

$result = @mysql_query("SELECT "$select FROM partnership WHERE $subject[] ORDER BY $collage) or die (mysql_error);

the second line is commented out, which is the line that would get data from your query. The third line won't work if you don't have the line before it.

You have a couple different options for extracting data from a query

you might want to take a look at this thread
Basics of extracting data from MySQL using PHP [webmasterworld.com]

it should help you figure it out a bit but if not just ask

smartie

6:40 pm on Jan 13, 2008 (gmt 0)

10+ Year Member



thankyou jatar_k

I have read that before

i now have this in check9.php

mysql_connect(localhost,$username,$password);
@mysql_select_db($db) or die( "Unable to select database");
$result = @mysql_query("SELECT "$select FROM partnership WHERE $subject[] ORDER BY $collage) or die (mysql_error);
$select = implode(", ", $_post["view"]);
$dh=mysql_query($sql);
while ($i=mysql_fetch_row($dh))
mysql_close($connect);

below code in checkbox.html
<?php

error_reporting(E_ALL);
if (!isset($_POST['Submit'])) {
// form not submitted
?>
<P>
<font face="Arial" color="#004080" size="1">
<form action="check9.php" method="post">
<input type="checkbox" name="subject[]" value="arable"> for Arable<br>
<input type="checkbox" name="subject[]" value="dairy"> for Dairy<br>
<input type="checkbox" name="subject[]" value="beefsheep"> for Beef and Sheep<br>
<input type="checkbox" name="subject[]" value="pigs"> for Pigs<br>
<input type="checkbox" name="subject[]" value="poultry"> for Poultry<br>
<input type="checkbox" name="subject[]" value="engineering"> for Engineering<br>
<input type="checkbox" name="subject[]" value="foodchain"> for Food Chain<br>
<input type="checkbox" name="subject[]" value="management"> for Env Management<br>

<input type="Submit" value="Submit" name="Submit">

and to display i have this

$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$collage=mysql_result($result,$i,"collage");
$contact=mysql_result($result,$i,"contact");
$address=mysql_result($result,$i,"address");
$address1=mysql_result($result,$i,"address");
$town=mysql_result($result,$i,"town");
$city=mysql_result($result,$i,"city");
$postcode=mysql_result($result,$i,"postcode");
$telephone=mysql_result($result,$i,"telephone");
$fax=mysql_result($result,$i,"Fax");
$email=mysql_result($result,$i,"email");
$website=mysql_result($result,$i,"website");
$education=mysql_result($result,$i,"education");

it will query only the last checkbox checked

i am so lost with this please help

jatar_k

2:17 pm on Jan 15, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sorry I was indisposed for a couple days

well this line is still wrong

$result = @mysql_query("SELECT "$select FROM partnership WHERE $subject[] ORDER BY $collage) or die (mysql_error);

there is an extra double quote after the word SELECT, for one. The other issue is that $subject is an array.

you would have one of two options based on the setup of the database you need to

1. do a query for each value that is in $subject if you are querying on different columns

2. if all of these checkbox values are different values in the same column then you need to construct your query differently

I can show you how to do either way

so the question

do these values appear in different columns or are they different string values in the same column?

smartie

8:25 am on Jan 16, 2008 (gmt 0)

10+ Year Member



Thankyou

if i understand you right they are all in differant fields in the table

I now have this code and it does seem to work i only need to click a maxium of 3 boxes so i used onclick which works.

<form action="check9.php" method="post">
<input type="checkbox" name="subject[]" value="arable" onClick="CountChecks(this)"> for Arable<br>
<input type="checkbox" name="subject[]" value="dairy" onClick="CountChecks(this)"> for Dairy<br>
<input type="checkbox" name="subject[]" value="beefsheep" onClick="CountChecks(this)"> for Beef and Sheep<br>
<input type="checkbox" name="subject[]" value="pigs" onClick="CountChecks(this)"> for Pigs<br>
<input type="checkbox" name="subject[]" value="poultry" onClick="CountChecks(this)"> for Poultry<br>
<input type="checkbox" name="subject[]" value="engineering" onClick="CountChecks(this)"> for Engineering<br>
<input type="checkbox" name="subject[]" value="foodchain" onClick="CountChecks(this)"> for Food Chain<br>
<input type="checkbox" name="subject[]" value="management" onClick="CountChecks(this)"> for Env Management<br>

<input type="Submit" value="Submit" name="Submit">

check9.php code below

$connect = mysql_connect($host, $user, $pass) or die ("Unable to connect to host");

mysql_select_db($db) or die ("Unable to connect to database");

$query = "SELECT * FROM partnership WHERE id = '$search'";

$result = mysql_query($query) or die (mysql_error());

$num = mysql_numrows($result);

mysql_close($connect);

$result_array = mysql_fetch_assoc($result);
print_r($result_array);