-
Posts
5,518 -
Joined
-
Days Won
187
Everything posted by mac_gyver
-
this is because your code is fetching the first row from the result set, see the first line in the following code, not using it, then looping over the remaining rows in the result set, starting at the second line in the following -
-
in html5, no action attribute at all means that the form submits to the same page. you can also set action='#' to submit to the same page, but an empty action attribute, action='', is not valid html5 markup (as of the last time i checked.)
-
no. it will set php's error related setting so that php will help you find what's causing the problem by reporting and displaying all the errors it detects.
-
that's not E_ALL. as an integer, E_ALL would be 32767
-
do you have php's error_reporting set to E_ALL and display_errors set to ON, in the php.ini on your system, so that php will help you by reporting and displaying all the errors it detects?
-
here's a list of things your login code needs to do differently - do NOT store the user_id in a cookie to identify who is logged in. anyone or a bot script can supply any value for a cookie when they request your page and appear to be anyone, such as you or an administrator on your site, just by going through all possible user id's until they find one that works. you would instead generate a random unique value, similar to what a session id cookie is, and store it in a database table that relates it to the actual user_id and store it in the cookie. you must have an exit/die statement after every header() redirect to STOP code execution. your current code is executing all the rest of the code on the page at each header() redirect. don't use fetchAll() and a loop for a query that will at most match one row of data. just directly call the fetch() method and test if a row of data was found. as to your current problem, the code you are dealing with is the login form processing code and the login form. however, you have put the login form processing code at what appears to be the top of the main index.php page. this doesn't make any sense, logically, because you would be redirecting to the main page, that you are already on, if the cookie is set. you are also testing a different cookie name then the one you are setting (id vs user_id), and there's no code setting the $user_id variable you are testing on the page to determine if there is a logged in user.
-
Joining two tables as sums with individual records
mac_gyver replied to 684425's topic in MySQL Help
what have your tried? this assignment seems pretty straight-forward - SELECT the things you want, one of them being the SUM() of the column you want to add up FROM your two tables JOINed together ON the column that relates the data between the tables GROUP BY the column that identifies which rows to SUM() together and any ORDER BY column(s) that get the data into the order that you want to display it as -
an attribute like this is usually related to a wysiwyg theme/template/markup editor and has nothing to with how the web page operates. if you are doing something that you believe this will have an affect on how the page operates, you would need to share that information with us. the post method form you build and the corresponding form processing code you write determine what the web page does.
-
Keep value of selectbox if there is an error on the form
mac_gyver replied to mike3075's topic in PHP Coding Help
your function should not be responsible for making a database connection or querying for the choices. those are the responsibility of your main application code. i would make your function general-purpose, reusable, and supply an array of option choices, with id, label entries, and an optional array, to support the multiple attribute, of currently selected choices. -
variable variables and other 90's stuff...
mac_gyver replied to StevenOliver's topic in PHP Coding Help
-
variable variables and other 90's stuff...
mac_gyver replied to StevenOliver's topic in PHP Coding Help
see the following example code, using an array for the form field name, on how to retrieve existing data, populate a form with it, then do whatever you want with the submitted form data - <?php // example of editing existing data, even if not saving the result, i.e. the U in CRUD session_start(); // fake a logged in user $_SESSION['user_id'] = 123; // recursive trim call-back function function _trim($val){ if(is_array($val)){ return array_map('_trim',$val); } else { return trim($val); } } // require 'pdo_connection.php'; // the pdo connection code should - set the error mode to exceptions, set emulated prepared queries to false, and set the default fetch mode to assoc // if the user is not logged in, go elsewhere if(!isset($_SESSION['user_id'])) { header("Location:index.php"); exit; } // if the user must have a rank or 'edit' permission to access this page, query to get that information and test it here... // at this point, the user has permission to access this page $post = []; // array to hold a trimmed working copy of the form data $errors = []; // array to hold error messages // post method form processing if($_SERVER['REQUEST_METHOD'] == 'POST') { // input(s) - sku, an array with an sku as the index for each element and the submitted price as the element's value // trim all the input data at once $post = array_map('_trim',$_POST); // since some of the form fields are arrays, use a recursive trim call-back function here. // validate all the inputs here, storing error messages in the $errors array, using the field name as the array index... // in no errors, use the input data if(empty($errors)) { // examine the submitted data echo '<pre>'; print_r($post); echo '</pre>'; // loop over the submitted data foreach($post['sku'] as $sku=>$price) { // use each set of submitted $sku/$price for anything you want... } } // if no errors, success if(empty($errors)) { // redirect to the exact same url of this page to cause a get request - PRG Post, Redirect, Get. //header("Refresh:0"); //exit; // note: if you want to display a one-time success message, store it in a session variable, test, display, and clear that session variable in the html document } } // if there's no form data, get the initial data needed to populate the form fields if(empty($post)) { $sql = "SELECT sku, price FROM your_table ORDER BY sku"; // if you have a WHERE term, add it to the sql statement, change this to be a prepared query, and supply an array of inputs to the ->execute() method call. /* $stmt = $pdo->query($sql); foreach($stmt as $row) { $post['sku'][$row['sku']] = $row['price']; } */ // fake some data for demo purposes $post['sku'][12134] = 10.00; $post['sku'][5567] = 5.99; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Edit/Update example</title> </head> <body> <?php // display any errors if(!empty($errors)) { echo '<p?' . implode('<br>',$errors) . '</p>'; } // display the form if(empty($post['sku'])) { echo '<p>There is no data to edit.</p>'; } else { ?> <form method="post"> <input type='submit'><br> <?php foreach($post['sku'] as $sku=>$price) { $price = number_format($price,2); echo "<label>$sku: <input type='text' name='sku[$sku]' value='$price'></label><br>"; } ?> </form> <?php } ?> </body> </html> -
variable variables and other 90's stuff...
mac_gyver replied to StevenOliver's topic in PHP Coding Help
you are the UPDATE'ing the submitted prices back into the database table? what is the significance of multiple same sku values? are there multiple rows in the database table with the same sku, which would indicate bigger problems in the design? -
variable variables and other 90's stuff...
mac_gyver replied to StevenOliver's topic in PHP Coding Help
whatever this code's purpose is, you should probably be using an array for the form field name, with the sku value as the array index. so, what is the purpose of this? who uses it, what are the input(s), how many sets of inputs are there, what processing will be done using those inputs, and what is the desired result or output for some sample input data? -
the error you are getting is a follow-on error, due to code being executed when a 'required' input is not present. if you have a section of code that requires a logged in user, your code should test if the session variable isset() before ever executing that code. this will stop the follow-on error. the actual problem is a session variable that should be set but isn't. in the first post and your post above, you have stated/implied that the SELECT query in the first post is being executed/working. how do you know it's working. what code is using the result from that query and what result or output are you getting that leads you to believe that particular query/section of code is working? taken by itself, a symptom of code that seems to work and also produces an error is usually a sign that the code is being executed twice, once with and once without the expected input data. next, there's a bunch of issues with the rest of the code you have posted - you should have an auto-loader to load the class definitions. also, 'require' isn't a function and the () around the filenames are not needed. the responsibility and most of the code in model.php is not needed. if you want to do something useful for a database class, extend the PDO class with a general-purpose prepared/non-prepared query method that accepts a second optional array of input parameters and prepares/executes the query if there are input parameters and just calls the PDO query() method if there are no input parameters. your main code should be responsible for making a database connection, since it knows if, how many, and where those connections should be used, then use dependency injection to supply the connection to any class that needs it. you should not use defined constants for the database connection credentials. this limits the code to using only a single database connection. you should use exceptions for database statement errors and in most cases let php catch and handle the exception, where php will its error related settings to control what happens with the actual error information (database statement errors will 'automatically' get displayed/logged the same as php errors.) the exception to this rule is when inserting/updating user submitted data and a duplicate or out of range error can occur. in this case, your code should catch the exception, test if the error number is for something that your code is designed to handle, setup and display a message for the user telling them what was wrong with the data that they submitted. for all other error numbers, just re-throw the exception and let php handle it. you should set emulated prepared queries to false, you want to run real prepared queries, and set the default fetch most to assoc, so that you don't need to specify it in each fetch statement. by having a 'query' method that is actually preparing the query, you have created confusion and more work for anyone who will have to read/maintain this code. if you simply supply an array of the input data to the execute([...]) call, you don't need any of the code for binding inputs. also, by using bindValue(), you cannot directly execute the same query again with different data and is_int() in that code isn't doing anything for integer values that are in a string variable type. the verify() method also has responsibility and code issues. just some of them - the only user data you should store in the login session variable is the user id (auto-increment primary index.) you should query on each page request to get any other user related data/permissions. this is so that any change in the user data/permissions will take effect on the very next page request, without requiring the user to log in again. also, all header() redirects need an exit/die statement after them to stop code execution.
-
Is This How To Display Results With mysqli_stmt_bind_result()
mac_gyver replied to 2020's topic in PHP Coding Help
they are settings actually - name = value error_reporting = E_ALL (or even better, a -1 to future proof it, as the E_ALL value can have more added to it) display_errors = on another setting to change, since it is probably off on a live/public server and you may not be able to set it there, is - output_buffering = off the runtime issue with putting these settings into your code is that if the code contains a parse/syntax error, the code where these settings are at never runs to cause the settings to take effect. -
Is This How To Display Results With mysqli_stmt_bind_result()
mac_gyver replied to 2020's topic in PHP Coding Help
this problem goes away if you switch to the much simpler and more consistent PDO extension, because you can fetch data from a PDO prepared query exactly the same way that you fetch it from a traditional, non-prepared query. a PDO prepared query also uses ? for the place-holders, so you can reuse your existing sql query syntax. all you have to do is make the connection using the PDO extension (setting the character set to match your database tables, set the error mode to exceptions, set emulated prepared queries to false, and set the default fetch mode to assoc), which i'm pretty sure has already been posted in some of your threads, but which someone would be willing to re0post, then change the remaining mysqli statements to the equivalent PDO statements, which you can do using by rote/A-B pattern matching. the statements needed for binding input and output parameters also go away. you simply supply an array of the input values to the execute([...]) call and use one of the fetch statements to retrieve the data. yes. you should be internalizing the meaning of the php, sql, html, and css statements/words you are using so that you know what they actually do, so that when you read or write code you know what each statement/word contributes to the 'story' on the web page. you basically need to become fluent enough in each of those languages so that you can write a meaningful and understandable short story using them. when i/we read code that someone posts, we start at the top and actually read the statements/words that have been posted, adding up what the meaning of each statement/word is contributing to the story being told. when we get to the end, if the statements/words all made sense and all have something to do with the task at hand, we know that the code will probably work (through testing will determine if it actually does.) when we see code that has all kinds of unnecessary, repetitive, missing, misused, and out of order statements/words, what do you think pops into our minds? as to your posted code - you are no longer requiring (you should use 'require', not 'include', and both of these are not 'functions' so don't put ( ) around the filename) either the connection or the error .php files. Instead, you have attempted to insert the code into the main file, introducing errors in the code and adding repetitive logic. why did you change this? did you miss the point about putting common code into a .php file and requiring it when needed? php's error related settings should be put into the php.ini on your system. this is so that they will report ALL php detected errors, even parse/syntax errors. this also allows your development system and your live/public server to be configured with their own error related settings and you don't need to remember to edit any code when moving it between those two systems. when you were initially and most lately given the mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); line, you were told and shown to put it before the line where you make the database connection. that would mean that it belongs in the conn.php file. if you had put it there and left it there, you wouldn't be mucking around with it now. you have also been told a number of times that when using exceptions for database statement errors, which is what the mysqli_report() line of code does, that none of the connection, query, prepare, and execute error handling logic will get executed upon an error and all that existing logic should be removed, simplifying and de-cluttering the code. you were also told that using exceptions for database statement errors and letting php catch and handle the exception, meaning there is no try/catch logic in your code, will cause database statement errors to 'automatically' get displayed/logged the same as php errors. you are now asking in the conn.php code how to cause a connection error to get displayed/logged. you have been told how to do this. you also don't display these errors to the user/visitor to your site. you only display these errors to the programmer/developer, i.e. you, when you are learning, developing, and debugging code/query(ies.) you don't even tell the visitor, which could be a hacker, anything about an error that has occurred, since that will just encourage them to do more of the same things that caused the error in the first place. you are putting unnecessary double-quotes around php variables in the conn.php code. again, what do you think pops into our minds when we have to look at unnecessary things that could be causing a problem when someone asks us to look at their code? you have both a die() and an exit() statement in the conn.php code. that do the same thing. are you even looking at and reading your code? the search form should use method='get' (which is the default if you leave the method attribute out.) get is used when determine what will be displayed on a page. post is used when creating/altering data on the server or performing an action, such as sending an email. an empty action='' attribute is not valid html5. you were actually told to remove the entire action attribute. the search form should be 'sticky' and re-populate/select the form field values/choice(s) that correspond to any submitted data. only <input ... > or <button ... > tags that have a type='submit' attribute are capable of submitting the form. use one or the other, not both. only form elements with a name='...' attribute will be included in the submitted data. if you have only one post method form on a page, after you have tested the server REQUEST_METHOD, that's all the logic you need to start the form processing code. except for unchecked/unselected form elements, all other form fields will be set once the form has been submitted. using isset() around always-set form fields is a waste of your typing time. your form processing code should trim, then validate all inputs, storing any validation error messages in an array, using the field name as the array index. if after the end of the validation logic the array holding the error messages is empty, you can use the submitted form data. the get method 'business' logic, that knows how to get/produce the data needed to display the dynamic content on the page should come before the start of the html document. you would fetch all the data from any query into a php variable, then test/loop over that variable at the correct point in the html document. if you are going to continue to use the mysqli extension, just use the single $stmt = mysqli_prepare($conn,$query); statement, instead of $stmt = mysqli_stmt_init($conn); and mysqli_stmt_prepare($stmt,$query). i'm pretty sure you have been told this point before. i don't know why you are using the most complicated way of doing things. remember these two acronyms - Don't Repeat Yourself (DRY) and Keep It Simple stupid (KISS.) if you find yourself repeating the same code over and over or doing something in the most complicated way possible, you are probably doing something wrong. don't do this - if($bind_result === FALSE) the rest of the mysqli stmt error logic you have... the mysqli_stmt_bind_result() statement will produce a php error if it fails, which would be due to a programming mistake on your part. it won't produce a database statement error, and you won't get anything useful from the current code. the only things that produce database statement errors/error numbers are the connection, query, prepare, and execute statements. don't do this are well - if($stmt_fetch === FALSE) the rest of the mysqli stmt error logic you have... there's two problems with this - 1) your code is fetching the 1st row from the result set, but not displaying it, so you will be missing a row of data, and 2) a false value when you fetch data only means that there was no data to fetch. this is not an ERROR, it's how your code knows to stop fetching data. there won't be any mysqli statement error/error number values to display. php will destroy all resources when the script ends, so in most cases, you don't need to close a prepared query statement or close the database connection. most of these things will actually simplify and clean up your code, leaving you with just the implementation logic that you should be concentrating on to get the application to work and the forum members will be more happy about looking at what you are posting since it won't contain a wall of unnecessary elements. -
the error is because you are not using an index in the sql query, meaning that it will have to scan through all the data in the table to find rows that satisfy the WHERE clause, and you are just copy/pasting things you see without even reading what they are saying. the MYSQLI_REPORT_ALL value you are using is causing this. if you read the section of the mysqli error report documentation that i posted in one of your threads, you will see what the ALL value does. it includes the MYSQLI_REPORT_INDEX - Report if no index or bad index was used in a query value. it doesn't. all you have done is randomly change the code so that it is no longer executing that query. btw - since you are using exceptions for database statement error handling, none of the discrete error handing logic you have in your code will be executed, and is therefore pointless, since execution transfers to the nearest exception handler, or to php if none. its php that's giving you the current error output.
-
INSERT queries don't have WHERE clauses. they have a list of columns and corresponding values. you would add the user_id column next to the date column and supply the user id value for that column when the query gets executed. you should not put external, unknown, dynamic values directly into a query. use a prepared query. here's the insert query documentation definition with the most commonly used elements highlighted - the value_list definition - for a prepared query, the value_list would contain a ? place-holder for each value.
-
the above is why you are not seeing any errors. those two are not the same. E_ALL is a defined constant. putting quotes around it makes it a string consisting of the characters E, _, A, L, and L.
-
from the documentation -
-
Why did i get access array offset value on type null?
mac_gyver replied to Abel1216's topic in PHP Coding Help
i just noticed the cause of the problem. you have a stray semi-colon ; on the end of the while() statement. this short-circuits the loop, so that it loops over all of the data first, then your code that you think is part of the loop, inside the {}, is actually after the end of the loop, where there is nothing in $row. this is the the correct syntax for a while() loop when using { } around the conditionally executed statements - while (some condition is true) { code to execute inside the loop } // or even better, put the opening { on the next line so that it is alighned with the matching } while (some condition is true) { code to execute inside the loop } - you shouldn't even be using a loop to fetch the data from a query that will at most match one row. just directly fetch the row of data. btw - since you are comparing the user number in the query, if the query matched a row of data, you know that the user number was found. you don't need to compare it again in the php code. also, your comparison in the php code, because you are using an exact, not equal !== match, will always fail. by definition all form data are strings, regardless of what value they hold and the fetched data from a mysqli query (if using the default settings) is the actual data type of the column in the table. a string data type (the variable with the form data) will always be not equal to an integer data type (the variable holding the fetched user number from the query.) -
what does var_dump($row); show? btw - you should INSERT a new row for every transaction that affects a user's account balance, not update the value in a column. you currently don't have an 'audit trail' to detect if a programming mistake, duplicate form submission, or nefarious activity has altered the value. your current code will also update the value to the wrong amount if there are concurrent instances of your script being executed since the 'last' update query to get executed will replace any previously updated value.
-
code that unconditionally (always) outputs the raw database statement errors for the connection, query, prepare, and execute statements, only helps hackers when they intentionally trigger errors, since these errors contain things like the database hostname/ip address, database username, if a password is being used or not, part of the sql syntax, and web server path information. the only time you should output the raw database statement errors is when learning, developing, or debugging code/query(ies) and you are viewing the site as the developer/programmer. at all other times, you should log these errors. the simple way of doing this is to use exceptions for errors and in most cases let php catch and handle the exception, where php will use its error related settings to control what happens with the actual error information (database statement errors will 'automatically' get displayed/logged the same as php errors.) you would then remove any discrete error handling logic, since it doesn't add any value for a legitimate visitor to your site, and it will no longer get executed when there is an error (execution transfers to the nearest exception handler for the type of exception or to php if there is none.) the line that Barand posted enables exceptions for errors for the mysqli extension.
-
the main reason requinix stated that is because you are not validating input data before using it (which i have mentioned doing multiple times in this thread.) someone can cause your code to execute any php function, like phpinfo(), by simply setting $_GET['form_type'] to phpinfo when they request your page. do you really want someone to be able to see the phpinfo output from your site or execute any php function? your code needs to have direct, positive control over what gets executed on any page request.
-
that's not what i posted. php's short opening php tag may not be enabled and shouldn't be used anyway. what does the 'view source' in your browser of the main page show?