0

Ok, here goes.. ive search through all the posts that seems to mentioned a similar thing but i cant find anything that works for me.

I have a form set for customers to request a brochure from one of our distributors. The form asks for a number of details, and we want to add the feature for them to select which brochure they want, rather than sending out all.

The code i am adding is as follows:

<p>Please select the brochure(s) you would like:<span>*</span></p>
        <br>


            <input type="checkbox" name="brochures[]" value="brochure 1"> brochure 1<br>
            <input type="checkbox" name="brochures[]" value="brochure 2"> brochure 2<br>
            <input type="checkbox" name="brochures[]" value="brochure 3">brochure 3<br>
            <input type="checkbox" name="brochures[]" value="brochure 4" >brochure 4<br>
            <input type="checkbox" name="brochures[]" value="brochure 5" >brochure 5<br>
            <input type="checkbox" name="brochures[]" value="brochure 6" > brochure 6<br>
            <input type="checkbox" name="brochures[]" value="brochure 7" > brochure 7<br> 

Then following is to get that info and pass to the database...

<?php 
        if(isset($_POST['brochures'])){
            $brochureList = implode(' ',$_POST['brochures']);
        } else {
            $brochureList = "no brochure selected";
        }
?>

and of course to insert it into mySQL..

INSERT INTO ". THE_TABLE_NAME ." SET 'brochure'= $brochureList;

whenever i run a test, i fills the brochure column with 'no brochure selected', no matter if i tick one or all of the checkboxes.

Any help, or information that would point me in the right direction would be a massive help.

Ta

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
evbeej
  • 11
  • 3
  • 1
    `SET 'brochure'= $brochureList;` that's failing for a few reasons and the form's method is unknown. – Funk Forty Niner Jan 30 '17 at 16:41
  • Did the form set `method="post"`, and as note: every not checked checkbox will send no data, so if you not check any of the boxes you will not have the field `brochures` in your POST. – JustOnUnderMillions Jan 30 '17 at 16:42
  • Sorry, yes forgot to say its method is post, when i check a selection or just one of the options, it still comes through as no brochure selected. ill check out the link regarding this being a possible duplicate, ta – evbeej Jan 30 '17 at 16:45
  • I have changed the following :INSERT INTO ". THE_TABLE_NAME ." SET `brochure`= $brochureList; and it still isnt working.. I will have to keep looking i spose – evbeej Jan 30 '17 at 16:54
  • `$brochureList` is obviously a string and you only posted raw SQL and not the full code you're using to run all this. I tested your code with some of my modifications; success. – Funk Forty Niner Jan 30 '17 at 16:58
  • Hmm.. ok. when you say 'you only posted raw SQL and not the full code' do you mean the full query that i am running to add this? – evbeej Jan 30 '17 at 17:03
  • Also, you should rethink your db schema; and think to normalize your db. Using multiple values to go inside the one row isn't good practice and you're going to have a hard time to query this later on. – Funk Forty Niner Jan 30 '17 at 17:03
  • `INSERT INTO ". THE_TABLE_NAME ." SET 'brochure'= $brochureList;` that is "next to raw SQL" here. We usually see something like `$query = mysqli_query($connection, "INSERT INTO table SET col_x = '$string'");` type of thing. The mysql api to connnect with is also unknown. – Funk Forty Niner Jan 30 '17 at 17:05
  • @RyanVincent look at their values; they're strings, not integers. – Funk Forty Niner Jan 30 '17 at 17:13
  • ah right, thanks for the heads up. I will make sure i remember that for future posts. I know what the comments will say after i post this but the current one reads: mysql_query("INSERT INTO table SET brochure = '$brochureList'"); (i know that mysql_query is no longer supported, but i just need to get this working until i can update to mysqli..) – evbeej Jan 30 '17 at 17:13
  • @evbeej `mysql_query("INSERT INTO table SET brochure = '$brochureList'");` is correct. If that still fails you, then you need to add `mysql_error()` to the query to see if something did fail, as well as using php's error reporting. – Funk Forty Niner Jan 30 '17 at 17:14
  • @PaulPoisson I just noticed 2 downvotes on some of my answers. I'm seriously thinking it's you after that wrong answer you gave. Just couldn't take the fact that you were wrong. You have a lot of nerve, you know that? Now you changed your profile to "none", nice one. – Funk Forty Niner Jan 30 '17 at 17:16
  • 1
    @RyanVincent No worries Ryan ;-) OP has enough to go on now. ;-) – Funk Forty Niner Jan 30 '17 at 17:32

0 Answers0