Hello,

Whenever I try to insert data fromm a checkbox html form into a mysql table using php, there is always an error with the brackets.

Here's an example

INPUT TYPE="checkbox" NAME="friendsdescriptions[]"

when I put it as $_POST[friendsdescriptions] in the php form, it appears an error:

Parse error: syntax error, unexpected '[', expecting ']'

Is there anyway to fix this?

Thanks in advance 🙂

    You are misinterpreting the error a little. 🙂 Here's the correct code:

    $_POST["friendsdescriptions"];
    

      I think we'd need to see more of the actual PHP code (the HTML code is irrelevant to the error message - PHP doesn't know about or care what your HTML looks like).

      @: Your "code" doesn't do anything at all. If you were referring to the correct syntax, we'd have to see the context it's being used in; your version could be incorrect and cause a parse error while funkymonkey's will work just fine.

      EDIT: Also, funkymonkey, note that you can't store an array in MySQL, so I'm not even sure what it is you're hoping to do.

        bradgrafelman;10982300 wrote:

        I think we'd need to see more of the actual PHP code (the HTML code is irrelevant to the error message - PHP doesn't know about or care what your HTML looks like).

        @: Your "code" doesn't do anything at all. If you were referring to the correct syntax, we'd have to see the context it's being used in; your version could be incorrect and cause a parse error while funkymonkey's will work just fine.

        EDIT: Also, funkymonkey, note that you can't store an array in MySQL, so I'm not even sure what it is you're hoping to do.

        I have an html survey with checkbox input questions. I want to store the answers of the questions in a table in mysql. However, whenever I try it out, the mysql is always blank in those spaces.

        VALUES ('$_POST[lastname]','$_POST[firstname]','$_POST[middlename]','$_POST[birthdate]',
        '$_POST[streetaddress]','$_POST[city]','$_POST[state]','$_POST[zip]','$_POST[phone]',
        '$_POST[email]','$_POST[contactmethod]','$_POST[yourgender]','$_POST[yourorientation]',
        '$_POST[ethnicity]','$_POST[yourpresentation]','$_POST[bodytype]','$_POST[height]',
        '$_POST[weight]','$_POST[felony]','$_POST[partnersgender]','$_POST[partnersorientation]',
        '$_POST[partnerethnicity]','$_POST[partnerspresentation]','$_POST[partnerbodytype]',
        '$_POST[partnersage]','$_POST[partnersheight]','$_POST[turnon]','$_POST[turnoff]',
        '$_POST[celebritycrush]','$_POST[firstthing]','$_POST[dealbreaker]')";

        Is it possible to store the results of an checkbox input on an html form into a mysql database? If so, how?

          Do a [man]var_dump/man on the $_POST array to see what types of values you're dealing with.

          PHP doesn't know nor care whether you used a checkbox or smoke signals to send data. All it sees is POST'ed data in the request, be it strings, arrays, etc. Once you know what you're working with, the answer will be obvious (e.g. you'll then be asking "Is it possible to store a string into a mysql database?" and the answer is of course, yes).

          Also note that you should never place user-supplied data directly into a SQL query string, else your code will be vulnerable to SQL injection attacks and/or just plain SQL errors. Instead, you must first sanitize it with a function such as [man]mysql_real_escape_string/man (for string data).

            I am having trouble with the var_dump(). I appreciate the help, but I simply want to know the code for displaying the answers to a checkbox question in mysql.

            The checkbox looks something like this:

            <INPUT TYPE="checkbox" NAME="friendsdescriptions[]" VALUE="0">&nbsp;Good
            Listener <o:p></o:p></span></p>
            <INPUT TYPE="checkbox" NAME="friendsdescriptions[]" VALUE="1">&nbsp;Modest
            <o:p></o:p
            <INPUT TYPE="checkbox" NAME="friendsdescriptions[]" VALUE="2">&nbsp;Predictable

            I want the bolded area to be used with $_POST[friendsdecriptions[]] but there is always an error with the double brackets. Is there a code to this to mysql? How can I fix it so that the brackets won't cause an error?

              You've got three problems:

              1. The array indexes in $POST are strings. $POST[foo] is wrong outside of a double quote delimited string, because you're telling PHP to use the constant named foo rather than the string 'foo'.

              2. PHP will automatically convert elements ending with the square bracket array notation into array elements. In other words, $POST['friendsdescriptions[]'] won't exist, but $POST['friendsdescriptions'] will (and it'll be an array, too).

              3. You can't store an array in MySQL. There is a "code for this", sure, but that'll largely depend on your DB schema and how you want the information to be stored.

                Thank you very much, that information was very valuable!

                Just another question: is there some way to form those checkbox questions in a way to NOT be an array, that way I can store it in the mysql database? The aim is to have those words that the user checks off appear in the mysql database.

                Any suggestions?

                  Write a Reply...