Problem with checkboxes and implode

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamesmiddz
Forum Newbie
Posts: 18
Joined: Wed Jun 23, 2004 11:48 am
Location: UK

Problem with checkboxes and implode

Post by jamesmiddz »

Hi, I'm stuck again,

I have a form that submits information to my MySQL database>table. Some of the information is collect via Checkboxes. I want the contents of any chosen checkbox to be applied to only one field in my table. I have tried so many methods, I just can't get it working. I think it all come down to Implode() but I'm stuck on its implementation.

Here's what I've done so far; form.php

Code: Select all

<form enctype="multipart/form-data" action="insert.php" method="post">
<input type="checkbox" name="facilitiesї]" value="Clubhouse">
Clubhouse<br>
<input type="checkbox" name="facilitiesї]" value="Holiday caravan hire">
Holiday caravan hire<br>
<input type="checkbox" name="facilitiesї]" value="Games room">
Games room<br>
<input class="formblockgrey" type="submit" name="submit" value="Add Details">
</form>
insert.php

Code: Select all

<?php 
include("connector.php"); //usual mysql connect stuff

$query = "INSERT INTO hotel (facilities) 
VALUES('".$_POST['facilities']."')"; 

$result = mysql_query($query); 
 
?>
I have trimmed out all other fields just for the sake of the coding here. I anyone could help me on this, It would make me a happy man
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well, $_POST['facilities'] will be an array of values. So you need to join them in some way, eg:

Code: Select all

if(!empty($_POST['facilities'])){
    $query = "INSERT INTO hotel (facilities) VALUES('".join(',', $_POST['facilities'])."')";
    $result = mysql_query($query) or die(mysql_error());
}
that will put them comma separated into the db field.
Post Reply