Jump to content

Help connecting form to mysql


dziewalex

Recommended Posts

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"

?>

 

Link to comment
Share on other sites

the code for any page should be laid out in this general order -

  1. initialization
  2. post method form processing
  3. get method business logic - get/produce data needed to display the page
  4. html document

the post method form processing should -

  1. detect if a post method form was submitted
  2. keep all the input data in an array variable, then operate on elements in this array variable throughout the rest of the code
  3. trim all the inputs at once. after you do item #2 on this list, you can accomplish this with one line of code
  4. validate all the inputs, storing user/validation errors in an array using the field name as the array index
  5. after the end of all the validation logic, if the array holding the errors is empty, use the form data
  6. 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
  7. 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.
  8. 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.
  9. 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 by mac_gyver
  • Great Answer 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

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