Jump to content

Help. Checkboxes MySQL.


Recommended Posts

Hey. Anybody could help with checkboxes? What I'm trying to do is to store it's value into database. 

This is my code that is actually not working because it fails at try catch block and shows error (exception $e).
Index.php: https://pastebin.com/cYDdm9Jz
Process.php: https://pastebin.com/Kr2Lxf64
Config.php: https://pastebin.com/QtjvYcD5

This is working code that I've found on the net. It stores text and checkbox values perfectly.
Code.php: https://pastebin.com/mPuEGHfT
Index.php: https://pastebin.com/JM8rWE2t

Is there a way to adapt one code to another? I've tried few times but it just saves nothing at database. Please help.

Link to comment
Share on other sites

the error response you are getting has nothing directly to do with checkbox form data. you are getting an error with either the sql query statement or the execution of the sql query, but because the error handling does not display/log the raw error information, you don't have any idea why it is failing.

you need to find what the problem is and correct it. just trying a bunch of different things results in a lot of wasted time, with no actual learning occurring.

the current code is using the PDO extension, a prepared query with external, unknown, dynamic values, is supplying an array of the inputs to the execute() call, and is using exceptions for errors. this is the best choice for performing database operations and you should continue to use these practices.

some changes for the current code -

  1. when you make the connection, set emulated prepared queries to false, i.e. you want to run real prepared queries.
  2. set the default fetch mode to assoc, so that you don't need to specify it in each fetch statement.
  3. i would also name the connection variable $pdo, so that anyone looking at the code can tell what database extension it is using.
  4. because you are using exceptions for errors, there's no point in having discrete conditional error handling logic in your code, because it will never get executed upon an error. so, simply remove the if($result)... logic.
  5. if duplicate data is not an application error for this insert (and also for an update) query, you would NOT have any exception try/catch code at all, and simply let php catch the exception, where php will use its error related setting to control what happens with the actual error information (database statement errors will 'automatically' get displayed/logged the same as php errors.) for what you are currently experimenting with, this is probably the case. if you remove the try/catch code you have now, and assuming that your php is configuration to report and display all php errors, you will now start seeing database statement errors too.
  6. if this insert (and also for an update) query could result in duplicate data values and this is an error for your application, you would catch the exception in your code, test if the error number is for a duplicate index error, and setup a message for the user telling them what was wrong with the data they submitted. if the error number is for anything else, re-throw the exception and let php handle it.

note: $_POST is always set, even if it is empty, so the current logic testing if(isset($_POST)){ will always true. also, don't copy variables to other variables for nothing, this is just a waste of your time typing all of that.

your post method form processing code and form code should -

  1. detect if a post method form was submitted.
  2. keep the form data as an array variable, then operate on elements in this array variable throughout the rest of the code.
  3. trim all the input data at once. by keeping the data in an array variable, you can do this with one single line of code.
  4. validate all the inputs, storing validation errors in an array, using the field name as the array index.
  5. after the end of all the validation code, if the array holding the errors is empty, use the submitted form data.
  6. after using the submitted form date (which could cause user/validation errors in itself), if there are no errors, execute a redirect to the exact same url of the current page to cause a get request for that page.
  7. if you want to display a one-time success message, store it in a session variable, then test, display, and clear that session variable at the appropriate location in the html document.
  8. to allow the user to go to a different page, provide navigation links.
  9. if there are errors at step #6, the code would continue on to display the html document, where you would test and display the contents of the array holding the errors, then display the form, populating the form field values with any existing data.
  10. apply htmlentities to any values you output on a web page to help prevent cross site scripting.
  • Like 1
Link to comment
Share on other sites

People are often confused about the way checkboxes work in a form.

  • The first thing to know about checkboxes is that they will only exist in the $_POST when they are checked.  
  • If they were checked, they will be keyed in $_POST by the name of the input  (name="thing").

I didn't look that closely at all the code but here is an example of problematic markup.

 <p><input type="checkbox" id="var5" name="var6" value="yes"> do you like it?</p>
 <p><input type="checkbox" id="var6" name="var6" value="yes"> are you sure? </p>

Here you have 2 checkboxes with the same name. I don't know if you did this deliberately or by mistake.

Using the same name is allowed for cases where you want to have a few options that the user could check, and you want them to be grouped under a single POST variable.  If one or both of these are checked, $_POST['var6'] will be an array that will have one or more of the value "yes" in it.  The problem is that you don't know which of the checkboxes was checked.

If you understand the things I explained, you should be able to pinpoint what your problem is.  Often people think they will get a $_POST'var6'] element even if the checkbox is unchecked.  That is not the case.  In case of a boolean, for example, you need to check for the existence of the variable in the $_POST, typically by using isset or array_key_exists so you can make the appropriate variable initialization for your database insert when a checkbox is left unchecked.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • 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.