dziewalex Posted October 18, 2022 Share Posted October 18, 2022 I havnt used php in a long time same with mysql mysqli and pdo are all very new to me and i have a project that is now due tomorrow... ive been trying to get this to work for over a week now and now as the deadline looms im about to throw up my hands and hope theres someone that could help me finish the code. ( i do plan on learning pdo and the new php8, but i do not have that time atm for this project. I currently only have the html portion made of the form, as well as the db tables / fields. I have also made the results page that would display the inputed fields from the form and that works great "when entering the data manually through 'phpmyadmin' what i am missing is the php to update those fields in the database. I am using a CMS so its a little different as i dont need to connect to the data base as im already connected. this is my current code... <?php require_once "maincore.php"; require_once "subheader.php"; if (iMEMBER) { $data = dbarray($result); $result = dbquery("UPDATE ".$db_prefix."users (user_loyalty, user_cavs, user_foot, user_archer, user_reset, user_6, user_8, user_12, user_16, user_18, user_23") $loyalty = isset($_POST['user_loyalty']) ? stripinput(trim($_POST['user_loyalty'])) : ""; $cavs = isset($_POST['user_cavs']) ? stripinput(trim($_POST['user_cavs'])) : ""; $foot = isset($_POST['user_foot']) ? stripinput(trim($_POST['user_foot'])) : ""; $archer = isset($_POST['user_archer']) ? stripinput(trim($_POST['user_archer'])) : ""; //availability match check boxes with table $reset = isset($_POST['user_reset']) ? stripinput(trim($_POST['user_reset'])) : ""; $reset6 = isset($_POST['user_6']) ? stripinput(trim($_POST['user_6'])) : ""; $reset8 = isset($_POST['user_8']) ? stripinput(trim($_POST['user_8'])) : ""; $reset12 = isset($_POST['user_12']) ? stripinput(trim($_POST['user_12'])) : ""; $reset16 = isset($_POST['user_16']) ? stripinput(trim($_POST['user_16'])) : ""; $reset18 = isset($_POST['user_18']) ? stripinput(trim($_POST['user_18'])) : ""; $reset23 = isset($_POST['user_23']) ? stripinput(trim($_POST['user_23'])) : ""; echo "<div class='' align='center'> <h1>Stats for Eden</h1> </div> <form class='' name='test' method='post' action='".FUSION_SELF."' enctype='multipart/form-data'' target='_self'><br> <div class='' align='center'> <label for='availability' class=''>Select Available Times<span class='tooltip' tooltip='Check the times your available for battle'> </span></label>' <ul id='availability'> <li><input name='reset' value='yes' type='checkbox'>Reset</li> <li><input name='6' value='yes' type='checkbox'>+6</li> <li><input name='8' value='yes' type='checkbox'>+8</li> <li><input name='12' value='yes' type='checkbox'>+12</li> <li><input name='16' value='yes' type='checkbox'>+16</li> <li><input name='18' value='yes' type='checkbox'>+18</li> <li><input name='23' value='yes' type='checkbox'>+23</li> </ul> </div> <div class='' align='center'><label for='footmen' class=''>Footmen<span class='tooltip-element' tooltip='highest level footmen'> </span></label> <select class='form' name='footmen' id='footmen' required='required' aria-required='true'> <option disabled='null' selected='null'>-</option> <option value='7' id='footmen'>T7</option> <option value='8' id='footmen'>T8</option> <option value='9' id='footmen'>T9</option> <option value='10' id='footmen'>T10</option> </select> <label for='archers' class=''>Archers<span class='tooltip' tooltip='highest level archers'></span> </label> <select class='form' name='footmen' id='footmen' required='required' aria-required='true'> <option disabled='null' selected='null'>-</option> <option value='7' id='archers'>T7</option> <option value='8' id='archers'>T8</option> <option value='9' id='archers'>T9</option> <option value='10' id='archers'>T10</option> </select> <label for='archers' class=''>Calvary <span class='tooltip-element' tooltip='highest level calvary'></span> </label> <select class='form' name='calvary' id='calvary' required='required' aria-required='true'> <option disabled='null' selected='null'>-</option> <option value='7' id='calvary'>T7</option> <option value='8' id='calvary'>T8</option> <option value='9' id='calvary'>T9</option> <option value='10' id='calvary'>T10</option> </select> </div> <div class='' align='center'><br> </div> <div class='' align='center'> <label for='loyalty' class=''>Loyalty Level <span class='tooltip' tooltip='EDEN Loyalty'> </span></label> <input placeholder='0' class='form' name='loyalty' value='0' id='number-loyalty' title='eden loyalty level' required='required' form='stat_update' min='0' max='30000' type='number'> </div> <div class='' align='center'> <input name='submit' form='test' formaction='submit-form-db' formmethod='post' formtarget='_self' type='submit'> <input name='reset' type='reset'> </div> </form>"; } else { redirect("index.php"); } require_once "footer.php" ?> Quote Link to comment https://forums.phpfreaks.com/topic/315436-help-connecting-form-to-mysql/ Share on other sites More sharing options...
mac_gyver Posted October 18, 2022 Share Posted October 18, 2022 (edited) the code for any page should be laid out in this general order - initialization post method form processing get method business logic - get/produce data needed to display the page html document the post method form processing should - detect if a post method form was submitted keep all the input data in an array variable, then operate on elements in this array variable throughout the rest of the code trim all the inputs at once. after you do item #2 on this list, you can accomplish this with one line of code validate all the inputs, storing user/validation errors in an array using the field name as the array index after the end of all the validation logic, if the array holding the errors is empty, use the form data if an insert or update query can result in duplicate data, detect this in the error handling for the database query, and setup a message for the user telling them what was wrong with the data that they submitted after the end of the form processing logic, if there are no user/validation errors, perform a redirect to the exact same url of the current page to cause a get request for the page. to display a one-time success message, store it in a session variable, then test, display, and clear the session variable at the appropriate location in the html document. if there are errors at item #5 or #7 on this list, the code will continue on to display the html document, redisplay the form, where you should populate the form field values/selects/checkboxes with any existing values so that the user doesn't need to keep reentering/selecting/checking data over and over. you should get your code to fully work first with one form field, then one field of each type, before you worry about all the code needed for all the rest of the fields. you can use the following to display what post data is being submitted to your code - echo '<pre>'; print_r($_POST); echo '</pre>'; the posted code contains a php syntax error on line #13. to get php to display syntax errors, you must set php's error_reporting to E_ALL and display_errors to ON, in the php.ini on your development system (putting these settings into your code won't work for php syntax errors since your code never runs to change the settings.) stop and start your web server to get any changes made to the php.ini to take effect and confirm that the settings actually got changed by using a phpinfo() statement in a .php script file. you should also validate any resulting web page at validator.w3.org to help make sure the markup is valid and you should only write markup that is necessary. Edited October 18, 2022 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/315436-help-connecting-form-to-mysql/#findComment-1601759 Share on other sites More sharing options...
ginerjm Posted October 19, 2022 Share Posted October 19, 2022 All of the above is top-notch advice. Listen to it even if you don't understand why. Quote Link to comment https://forums.phpfreaks.com/topic/315436-help-connecting-form-to-mysql/#findComment-1601762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.