Jump to content

[SOLVED] Forming a SQL statement from multiple checkboxes


gamefreak13

Recommended Posts

I have 4 different checkboxes on a form that adds some data to my MySQL database. The problem, is only one of the 4 checkboxes will work at any given time. If someone checks Checkbox 1 and Checkbox 3, the database field says 'Checkbox 3'. I was expecting to see 'Checkbox 1Checkbox 3', but it doesn't merge them.

 

What I want to do, is if a user selects Checkbox 1 and Checkbox 3, for the 'result' to be merge into 'Checkbox 1, Checkbox 2' with the comma. I don't know if this would be a php array or a foreach or what. I know it's really simple though and I've probably passed over the code before. I will also need to trunicate the last 2 characters no matter what, because of the comma and space. I don't want it showing up as 'Checkbox 1, Checkbox 3, '.

 

<input type="checkbox" name="type" value="Checkbox 1">  Checkbox 1<br>

<input type="checkbox" name="type" value="Checkbox 2">  Checkbox 2<br>

<input type="checkbox" name="type" value="Checkbox 3"> Checkbox 3<br>

<input type="checkbox" name="type" value="Checkbox 4"> Checkbox 4

 

The 'type' field gets inserted into mysql table.

Do you mean like this? (I set this up so I could set the vars via the URL to keep things simple)

 

There has to be a MUCH cleaner way of doing this. I want it to output 'Checkbox1, Checkbox2' or just 'Checkbox1' or just 'Checkbox2' depending on what the person checks.

 

test2.php?checkbox1=aaa&checkbox2=aaa

 

<?

$checkbox1 = $_GET['checkbox1']; // We have register_globals off so we need this
$checkbox2 = $_GET['checkbox2']; // We have register_globals off so we need this

if($checkbox1) { $querypt1 = "Checkbox1, "; } // If it is set then output 'Checkbox1, '
if($checkbox2) { $querypt2 = "Checkbox2, "; } // If it is set then output 'Checkbox2, '

$final = "$querypt1$querypt2"; // Merge everything

$query = substr($final,0,-2); // Removes last two characters of the entire thing which are a comma and a space

echo $query;

?>

The end result is whatever the output is, is to go in to a MySQL statement, and thus there can be no ', ' (comma then a space) at the end. I just think this is an extremely sloppy way of doing this. I am certain there is a much simpler/cleaner way of doing this.

what about this, (something to play with) i hope it makes sense

 

<form method="post" enctype="application/x-www-form-urlencoded" name="myform">
Bike<br />
On road 
<input name="bike" type="radio" value="on" checked="checked"  />
<br />
Off road 
<input name="bike" type="radio" value="off" />
<br />
<br />
CAR<br />
    <input type="checkbox" name="car[]" value="on" />
on road
    <input type="checkbox" name="car[]" value="off" />
off road
<br /> 
<br />
    <input name="submit" type="submit" value="ok" />
  </form>
<?php
$car = implode(",", $_POST['car']);


echo "bike:";
echo $_POST['bike'];
echo "<br /><br />";
echo "car:";
echo $car;

?>

I would possibly get ghetto with it.... Something like the following if I didn't feel like hand coding quite a bit:

 

<?php

if(isset($_POST['submit'])) {

$wheres = array(
1 => 'table.column = \'value\'',
2 => 'other_table.column = \'other value\'',
);

$checks = (isset($_POST['checkbox'])) ? $_POST['checkbox'] : array();

$where = array();

foreach($checks as $v) {
if(!is_numeric($v)) continue;
if(isset($wheres[$v])) {
$where[] = $wheres[$v];
}

$query = 'SELECT * FROM table, other_table';

if(count($where) > 0) {
$query .= 'WHERE ';
$query .= implode(' AND ', $where);
}

$q = mysql_query($query);

}

}
else {
//output a form with a submit button named 'submit' and check boxes named 'checkbox[]'
}

?>

Thanks corbin, but I was looking for just a simple bit of code. What you provided looks rather confusing (arrays are greek to me), and MadTechie's code did the job.

 

Thanks MadTechie! I cleaned it up to the bare essentials and it works great!

 

<form method="post">
  <input type="checkbox" name="type[]" value="On Road"> On Road<br>
  <input type="checkbox" name="type[]" value="Off Road"> Off Road<br><br>
  <input name="submit" type="submit" value="ok">
</form>

<?php

$type = implode(", ", $_POST['type']);

echo $type;

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.