Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,451
  • Joined

  • Days Won

    175

Everything posted by mac_gyver

  1. starting with the line that runs your sql query, the following is all that you need (untested) to produce the result - $result = $conn->query($sql); // run the query $rows = $result->fetch_all(MYSQLI_ASSOC); $data = array(); foreach ($rows as $row) { $data[$row['short_name_i18n']][$row['nickname']] = ($row['combination_exists'] == 1); } if(empty($data)){ echo "Please select again, no players found matching your search"; } else { foreach($data as $tank_name=>$arr){ echo "<tr><td width='167'>".$tank_name."</td>"; // tank name as row label foreach($arr as $nickname=>$element){ // output the data for each cell - you apparently have two images 0.png and 1.png echo "<td><img src='./img/{$element}.png' height='20' width='20' title='$nickname'></td>"; } echo "<td>". array_sum($arr) ."</td></tr>"; } } ?> </table> another recommendation - only SELECT the columns in your sql statement that you are using the data from. based on what you have shown in this thread, the select part of the query only needs to be - SELECT tank_list.short_name_i18n, player_list.nickname, garage_list.tank_id IS NOT NULL AS combination_exists
  2. you may want to revisit the last post in your previous thread where that sql query was developed - ^^^ doing what was shown in the code in that last post to store the data into an array will greatly simplify the code that outputs the table. you will just need to use a foreach loop to loop over the main array. the key is the tank name, the value is an array of data for the row. then just loop over the data for each row with another foreach loop. edit: to get the sum of the not null values, you can use php's array_sum() on the array of data. you should also never use the @ error suppressor in your code. if you were getting errors from those statements, you would want to know so that you could correct the problem that was causing them.
  3. what have you tried? we can help you with your attempted code, but only after you have made an honest attempt. if you are planning on just copy/pasting something together and dumping it on the forum with an 'it doesn't work', you won't get any more of a response than you already have. if you are not at the point where you understand enough about the code or the process, so that you can even make an attempt at integrating the recaptcha check code into the correct location in your existing application, and testing the result to make sure it does what you expect, you need to instead go and study a basic php book/comprehensive tutorial. we are not here to tell you what and where to type something in your code to do what you want or to write your code for you. if you need that level of assistance, you are not ready to do this thing called programming and should just hire someone to do this for you.
  4. except that your form's action="insert.php" attribute is submitting to insert.php, not update.php.
  5. perhaps you missed this information - this assumes you are dynamically producing the html where the images links are at using php code.
  6. your link should only contain the id, not all of the data. if you were doing this for real, ALL external data must be validated before you can use it. by only passing the id in the link, you only have to validate one value, a positive integer greater-than zero. it would appear you are working on the U part of a CRUD (Create, Read, Update, Delete) exercise. the 'work-flow' steps should be - 1) retrieve a list of data and output an 'edit' link for each record. this link should only identify which record the link corresponds to. 2) in the 'edit' page display code, condition/validate the id, and retrieve the data from the database that the id corresponds to. populate the form field values with the retrieved data. this form should use method = 'post' since it will be altering data on the server. 3) in the 'edit' page form processing code, check that the correct post method form was submitted, validate all the input data, and if there are no validation errors, use the data in an UPDATE query. if there are validation errors, you would display them when you re-display the form so that the user can correct any problems with the submitted data. you should always have some type error handling for database queries, so that you will know if and why they are failing. you should also have php's error_reporting set to E_ALL and for development/debugging have display_errors set to ON and when on a live server, have display_errors set to OFF and log_errors set to ON. if your code already had validation logic and separate error messages for each of the expected input data values and error handling for database queries, it (your code) would likely be telling you why it is failing. also, if you were doing this for real, you would have a user/permission check to insure that the current visitor is authorized to perform each of the three steps of this work-flow and that for any id value, that the visitor is authorized to update the data for that id.
  7. if the two values are being used in a setcookie() or a session_set_cookie_params() statement or in individual ini_set() statements for the session cookie parameters, the first one means the variations of the domain that the cookie will match and the second one is the path after the domain that the cookie will match. you can find specific information about these two settings in the php.net documentation for the setcookie() or session_set_cookie_params() statements. http:// is not part of a domain. it's a protocol and would not be part of the cookie settings. the path would typically be a / to match all paths after the domain. do you have php's error_reporting set to E_ALL and display_errors set to ON so that php will help you by reporting and displaying all the errors it detects? this is usually the first step that helps pin down what is causing a problem.
  8. the code you posted at the start of this thread is responsible for dynamically outputting a (one) requested image to the browser/client. you would not change this code (other than to update it and make it secure.) the code you would change is where the image links are being produced.
  9. you must ALWAYS check for and handle query errors. there are multiple possible reasons that any query can fail. for some quick debugging, just echo mysqli_error($mysqli); on the next line after your mysqli_query() statement, assuming you have a valid database connection in $mysqli. do you have php's error_reporting set to E_ALL and display_errors set to ON so that php would help you by reporting and displaying all the errors it detects?
  10. your query isn't matching any row(s). for this condition, which isn't a query error, the ->fetch() method returns a boolean false and the ->fetchAll() method returns an empty array. do the two form variables contain what you expect? are you using the same hashing method on the password when trying to login as when the account was registered (note: md5() is not suitable for hashing passwords. see the password_hash()/password_verify() functions.) is your pass field in the database table large enough to hold the hashed password? and in your viewProtectedArea() method, the catch block for the pdo exception needs to do SOMETHING with the error information . just listing the $exc->getMessage(); on a line doesn't do anything with the value. you should log the error information on a live server and display the information when developing/debugging. if you use php's trigger_error() statement, rather than an exit()/die() statement to handle the error message, it uses php's error_reporting/display_errors/log_errors settings to control where the error information goes to. edit: you would want to use something like this to handle the pdo exception message - trigger_error("Query: $query, Error: {$e->getMessage()}, File: {$e->getFile()}, Line: {$e->getLine()}"); you should be forming your sql statement in a php variable, i.e. $query in this example, so that you can include it in the error handling.
  11. the answer is it depends. it depends on if they provided a default admin account or on what the admin side code is doing. you would need to investigate what if anything was stored in the any database tables associated with authentication/permissions or what the code is doing to identify an admin login. without knowing what script you are using, there's no way of providing any definitive answer due to the multiple different possible ways any one thing could be accomplished in a program.
  12. that particular error is because you cannot bind sql syntax. you can only bind data values. your uname='$user' OR uname='$user' ... syntax cannot be bound into the query. you don't even need the section of code where the error is at. you should be JOINing the user table to the friends table to get the avatar in the same query where you are getting the friends. in fact, all the queries you have shown can be replaced with one query (you would randomize the two different sections of the results, assuming you really want to randomize anything, by pre-processing the rows in your php code to store the two different sections of the results in their own array, then randomize each of those arrays in the php code.) your code also has a problem with the first query. you cannot bind ONE parameter to multiple place-holders when using prepared queries. you would need to use two differently named place-holders and bind the $uname_s variable twice. the reason you aren't getting any php/mysql errors is because your pdo instance is emulating prepares and for emulated prepares, you can bind one parameter to multiple same-name place-holders (thanks php, NOT, for being consistent and doing what the documentation states.)
  13. the OP's phpinfo() output lists a Windows 7 Pro operating system, which is probably his development system, not the web host where he is having the error at. so, what that output from a phpinfo() statement on your web hosting or was that on your localhost xampp system?
  14. in your else {} logic, that corresponds to a failed prepare statement, the error information would be available in $db_connect->error, not in $stmt->error.
  15. the form is probably submitting to process2.php, but process2.php is redirecting back to form.php ^^^ why are you testing if those two values are not equal to a string consisting of a space " "? wouldn't you want to test each if those values are not an empty string "" and then produce a separate message for each one that is empty?
  16. the error information would be accessible via - $db_connect->error
  17. your loop should only be retrieving the data, forming the $message body with the data values, clearing any previous To: address (see the ClearAddresses() method), set the To: address from the data, and call the send() method. all the rest of the phpmailer related logic should exist only once and come before the start of your loop. you would also want to check if the send() method indicated success or failure. if it failed, log the error information and the user's email address. if it was successful, you would probably want to set a status value somewhere to indicate that the email was sent (though this is no guarantee that it was received) so that you don't repeatedly send the same reminder email.
  18. rather than to try and make your existing code 'work' (the comma separated list you are showing as a value makes no sense - the type and the meal number is known from the select name and don't belong in each option value), just define what data you have and what result you want to produce from that data. i gather that $data is an array that somehow contains the possible choices for meat, fruit, and vegs? your goal would be to produce the <option></option> list for for each type, using the $type variable to select what part of the $data to use. the value='...' attribute that you produce should be just be an identifier for the choice. the display label should indicate to the user what each choice means. if the option list is always the same for any type, all three meals each day and all 7 days of the week are the same, you would produce the option choices ONCE, store the result in a php array variable, using the type as the array index, then simply output them when needed, using the $type variable to select which one you output. so, what does the $data array look like?
  19. https://en.wikipedia.org/wiki/Web_search_engine
  20. your would queue the email messages in a database table and use a cron job/scheduled task to actually send them.
  21. as to having a huge amount of code, you should be dynamically producing the forms and dynamically processing the form data. for the form you posted above, you have a hidden field that indicates that day of the week. you will know that all the form fields correspond to that day. you don't need to include the day as part of the form field name. you have two (or more) types of items being selected - meat, fruit. the form field name should just be that type. you are doing this for three meals a day. if you use an array name for the select name, you can use the meal number as the array index, i.e. name='meat[1]', name='meat[2]', name='meat[3]', same for fruit[1], fruit[2], fruit[3] and any other types. if you have a list of the types, you would loop over that list as well, rather than hand-coding all of this out. the submit button doesn't need to be named because you have the hidden field with the name. see this example code that produces 7 days of forms, with 3 meals per day, with whatever different types you have - $days = array('Sunday','Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday' ,'Saturday'); $num_meals = 3; $types = array('meat','fruit'); foreach($days as $day){ echo "<h4>$day</h4>"; echo "<form method='post' action=''>"; echo "<input type='hidden' name='dayofweek' value='$day'>"; foreach(range(1,$num_meals) as $meal_number){ echo "<h5>Meal $meal_number</h5>"; foreach($types as $type){ echo "$type: "; echo "<select name='{$type}[$meal_number]'>"; echo "<option>build your option list in a loop using the variables present - \$day, \$type, \$meal_number to select which data to use</option>"; echo "</select><br>"; } } echo "<input type='submit' value='Save'>"; echo "</form>"; } if you in fact want only ONE form around all the days, you wouldn't use the hidden field with the day in it. you would add a second dimension to the form field array names. the first dimension would hold the day name (the day number would be better). the second dimension would be the meal number.
  22. you would add a unique composite key to your database table consisting of those three columns.
  23. yes, and what if your category values have been referred/linked/bookmarked to by visitors to your site. they would expect to be able to revisit your site and have their links/bookmarks take them to the same category information if it still exists. unless you are using test data in your tables that you will completely delete and start over with real data, you wouldn't ever alter the referral values between tables after it exists.
  24. to get you going, here is your index.php code, rearranged as suggested - <?php // 1) initialization - create/define things your code needs - session_start(), require files holding configuration data/function definitions, setup an autoloader for class definitions... session_start(); $errors = array(); // holds error messages. if empty, no errors, if not empty, at least one error. $products = array(); $products['MMS-1754'] = array('name' => 'Flute', 'cost' => '149.50'); $products['MMS-6289'] = array('name' => 'Trumpet', 'cost' => '199.50'); $products['MMS-3408'] = array('name' => 'Clarinet', 'cost' => '299.50'); require_once('cart.php'); // 2) start of database dependent code - create a database connection. if you are using exceptions to handle database errors, this would be where the try block starts. // none in this example // 3) determine user state and permissions - check if the current user is logged in and retrieve any permissions the user has. the rest of the code on the page would make use of the logged in state and permissions to determine what code can be ran and what content will be produced. // if you were doing this for real, you would require that the user be logged in/authenticated at some point // 4) post method form processing - the post method form processing code, which creates/modifies/deletes data on the server, should come near the start of your file so that you aren't tempted to output anything to the browser before any data has been updated by the processing code. if your page has multiple sections of form processing code, you would have them all groped together in this section of code. if($_SERVER['REQUEST_METHOD'] == 'POST'){ $action = filter_input(INPUT_POST, 'action'); switch($action) { case 'add': $product_key = filter_input(INPUT_POST, 'productkey'); $item_qty = filter_input(INPUT_POST, 'itemqty'); add_item($product_key, $item_qty); break; case 'update': $new_qty_list = filter_input(INPUT_POST, 'newqty', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); foreach($new_qty_list as $key => $qty) { if (true) { update_item($key, $qty); } } break; case 'empty_cart': break; } if(empty($errors)){ // after successfully (no errors) processing any post data, do a header() redirect to the exact same url that the form submitted to. this will cause a get request for your page. this will cause the browser to forget that a form was submitted and it won't try to resubmit the form data if you refresh the page or browse back to the same url. this also enforces separation of concerns. post method form processing, which modifies data on the server is a separate concern from displaying data due to a get request for your page. if you want to display a one-time 'success' message after the header() redirect, pass it in a session variable, then clear he session variable after the the message gets displayed. $host = $_SERVER['HTTP_HOST']; $uri = $_SERVER['REQUEST_URI']; header("Location: http://$host$uri"); die; } // if there are errors while processing any post data, you would not redirect, stay on the page, let the rest of the code on the page display the errors, (re)display the form, and repopulate the form fields with the previously submitted values. } // 5) get method business logic - code that produces/gets data needed for the dynamic content on the page. this code contains any database specific code that knows how to retrieve data from your database tables. the result of this code should be php variables that the code later on the page uses as its input. this code should contain NO html/css/javascript markup. // since this code is using an array to define the product information, not used in this example $action = filter_input(INPUT_GET, 'action'); // condition the get method action input switch($action) { case 'show_cart': // not used in this example break; case 'show_add_item': default: // not used in this example break; } // 6) end of database dependent code - if you are using exceptions to handle database errors, you would catch the errors at this point. you can also destroy any query result resources and the database connection at this point since you won't need them any longer. //none in this example //7) get method presentation logic - // code that knows how to take the data (database data, errors, form data...) from ALL the above code and // produce the dynamic output for the page. if the output doesn't require any 'heavy' processing/formatting, // just use the data directly in the html page/template code. the result from this code should be php variables // that the html page/template uses. this code should contain NO database specific statements. if your page // has multiple sections of get method presentation logic, you would have them all groped together in this // section of code. switch($action) { case 'show_cart': ob_start(); // capture the result of the included file include 'cart_view.php'; $main_content = ob_get_contents(); ob_end_clean(); break; case 'show_add_item': default: ob_start(); // capture the result of the included file include 'add_item_view.php'; $main_content = ob_get_contents(); ob_end_clean(); break; } // html page/template - this section starts with the <!DOCTYPE ... tag and ends with the </html> tag. it is the actual html document that the dynamic output is put into to make the complete page. only simple php conditional logic/loops, function calls (that are responsible for producing output), and echo statements should be present in this section of code. ?> <!DOCTYPE html> <html> <head> <title>My Guitar Shop</title> <link rel="stylesheet" type="text/css" href="main.css"> </head> <body> <header> <h1>My Guitar Shop</h1> </header> <main> <?php if(!empty($errors)){ echo 'The following error(s) occurred:<br>'; foreach($errors as $error){ echo "$error<br>"; } } ?> <?php echo isset($main_content) ? $main_content : ''; ?> </main> </body> </html> note: i didn't actually 'fix' anything in your code. in fact, for the places with php syntax errors due to incomplete logic, i just added a true keyword so that the code didn't throw any php syntax errors. faking/fixing up the php syntax and removing the common start/end of the html document from the two view files, lets your code run, but not necessarily do what you want, within the suggested page layout.
  25. actually, that's not specific enough for us to help you. we already know your code isn't working/broken/doesn't do what it is supposed to do because you are posting a topic on a programming help forum. if it was working/not broken/does do what it is supposed to do, you wouldn't be posting here. to get help, you must post actual errors and the code that they correspond to, symptoms (the output you actually observed, even if the output was a blank page) and at what point in the process they occurred, or specific questions.
×
×
  • 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.