Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,354
  • Joined

  • Days Won

    173

Everything posted by mac_gyver

  1. technically, there's nothing that would prevent your code from working. however, i can name at least a half-dozen things that could make it 'appear' like it isn't working. 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. i'm betting you would be getting php errors that would help pin down the problem. next, what is the user_id value supposed to be? one of the possibilities for why it may not be 'appearing' is if it happened to intentionally or accidentally be in the form of a html tag, in which case it would appear in the 'view source' of the page and not be displayed. lastly, you need an exit; statement after the header() redirect to stop program execution. the current code allows a non-logged in user to access the page since all the code still runs.
  2. the first two answers don't have anything to do with your question.
  3. a semi-colon is NOT required used after a } by adding them there, in addition to the wasted typing time and clutter, you are adding an empty php statement to the code, that php must parse when it examines the code. i'm not sure if this actually creates a php byte-code/token, that would then waste time during code execution too.
  4. at the point you have correctly authenticated the user, you would store the user id (the auto-increment column value from your user table) in a session variable. then, on any page where you want to reference any of the user data, you would test if there is a logged in user (the session variable has a value in it) and then query for the user's data using the id stored in the session variable. your login form processing code isn't any place you would display the user's data. its purpose is to log the user in/authenticate the user. and in fact, if the user is already logged in, you wouldn't run the login form processing code or display the login form. your login form processing code should also not be mixed in with the html on your page. it should come before the start of your html document. the form processing code should also first detect if a form has been submitted before referencing any of the form data. you would also validate the submitted form data before using it and you would test if the SELECT query matched a row before using the data from the query (your current logic allows an empty submitted password and a non-existent user to trigger the login logic.) you also need to store the hash of the password when the user registers, and check if the submitted password matches the stored hash in the login code (see php's password_hash() and password_verify() functions.) lastly, if you put the form processing code and the form on the same page (putting the form processing code inside conditional logic that has tested if a form has been submitted is about all you need to do to combine the code into one file), you won't have to mess around with providing a link to return to the form when the login fails. this will reduce the amount of work the user has to do to use your site and will eliminate repetitive html you have to write and maintain.
  5. use isset() to (directly) test if the item is in the cart or not - if(isset($_SESSION["cart_products"][$room->id])) {
  6. you cannot mix statements from the different database extensions. your database connection and query are using mysql_ statements. your error reporting is using mysqli_error(). if you were using mysql_error(), you would be getting the actual error information. next, the php mysql_ extension is obsolete and has been removed from php, for a little over a year. you need to be actively converting your code to use the php PDO extension, so that it doesn't stop entirely working should your web host up date the php version.
  7. aside from fixing the problems in this code, you wouldn't add any role based logic to this code. the purpose of the is code is to authenticate who the user is, that has nothing to do with the user's role and what they can do on a web site. you should also store the user_id value in the session variable, not the mysqli string escaped name that was entered in the form. you would add code to the 'protected' pages to retrieve the current user's role on each page request. and why would you do it this way? so that any change to the role value will take affect without requiring the user to log out and log back in.
  8. since only checked checkboxes are submitted, there will be no guaranteed relationship between the submitted opp_id[] and opp_played[] values. upon each un-checked checkbox, the id you are getting from the opp_id[] data will be OFF from the checkbox it is for. you could sequentially number both the opp_id[...] and opp_played[...] using code, but there's no need for the hidden opp_id field at all. read on. perhaps you should use a pair of radio buttons for each player instead? this will submit a value for each player, so that you don't have to deal with checked to un-checked checkboxes that won't be in the submitted data. the checkboxes (or radio buttons) need to contain the player id values, as the array index.
  9. your table_edit_ajax.php code isn't outputting anything back in response to the ajax request, nor does it have any query error checking logic in it, nor does your ajax code do anything with any returned data from the .php code. you MUST have error checking/handling logic in your code, so that your code does something expected when there is an error. your ajax code has a success handler, but it doesn't have an error handler. if the server doesn't respond, shouldn't your ajax code do something? next, all your ajax success handler does is put the two existing values back into the <span></span> elements. shouldn't your php code return a value to the ajax code and your ajax code use that value to tell the user if the operation was successful or not? lastly, the php mysql_ extension is obsolete and has been removed from the latest php version for more than a year. you need to switch your code to use the php PDO extension instead and use prepared queries to supply data values to the sql query statement. this will actually simplify your code since you will no longer need to call the ...escape_string() functions for every pieced of data.
  10. other than a typo you have in the 3rd id under case 3, your code 'works' for me. do you actually have any content with ids - additionalguest1, 2, 3 on your page? is the javascript syntax and html markup on your page error free? errors would stop code execution. errors would show up in the developer console in your browser. next, why are you defining and/or assigning values to the radio, attendValue, and numAdditional variables above each function definition. those lines serve no purpose in the posted code.
  11. i think the wording of your problem is perhaps causing unnecessary work in solving it. are you trying to use a default image when you have no image defined in your database table, i.e. if $row['url_img'] is empty, use no_img.jpg as the image? if so, you would just put a conditional test in the php code that's producing the output.
  12. i would look at row 55982 in your test_db table and see what length the name is (or just run a query to get the maximum length of the data in that column) and make the length of the whisky_name column longer than you expect the names to ever be.
  13. have you researched what the html would be to cause a option to be selected? if you haven't, that would be your first step, because if you don't know what html you are trying to produce, there's no way you can write the code to do it. there's hint in the bold part of my question to you in this paragraph. next, after you know what html you are trying to produce, the easiest way of producing it is to dynamically produce the list of <option ...>...</option> choices and the easiest way of doing that is to have the choices defined in a data structure (database table or an array), then simply loop over that defining data and produce the list of option choices. this will reduce the line of code that causes the the option to be selected to a single line, rather than to repeat it for ever possible option in every select menu. rather than to expect the user to know what the customer numbers/names are and use the error-prone method of having them type the value in a form field, you should query your table that defines the customer numbers/names and produce a select/option menu to allow the customer number to be picked.
  14. have been looking at the code more, and here is a point that will simplify the posted code even more. inserting a row in the requisicoes (requisitions) table, with the datas, atividade, id_ano (date, activity, school year), is an administrative operation. this must exist before a user can even try to pick a room and time for a date/activity/school year. therefore, the insert query for the requisicoes (requisitions) table doesn't belong in this code. the form that the user picks the room and time on would also submit the correct id for the row in the requisicoes (requisitions) table that he/she is trying to sign up for.
  15. the code you posted is expecting a comma delimited list (which could be a single value) in the main $id_bloco parameter. explode() breaks a delimited list into an array. however, your other uses of $id_bloco, when you call ->getDisponibilidadeSala($data, $id_bloco, $id_sala) and ->getDisponibilidadeDocente(($data, $id_bloco, $id_utilizador), appear like they expect only a single value. your current program logic, of first testing if the data exists before deciding to insert new data, would need to call these two methods inside the foreach(){} loop, not call them once before the start of the loop. another problem with the current foreach(){} loop is you are repeatedly executing the prepared insert query, which doesn't use the $id value, inside the loop and also running whatever insert query the ->insertRequisicaoByLastId() call executes. i'm pretty sure this is not what you intended. so, your program logic is confused about what is in $id_bloco and won't work when it is anything more than a single value. you need to make a list what input data you have available, with the data type or format (is the main $id_bloco parameter just one value, a comma delimited list, or an array), then define what processing you are going to do based on the input data. this will help make sure that the code you write is using the data properly and only contains code that you need. somewhat related to first defining what the input data is, your function should not get the school year ($id_ano) internally. all the data that your function needs should be supplied as call-time parameters. this will make your function general purpose and it will work for any set of data values. the current code will only work with the school year value that ->getAnoEscolar() returns. as to detecting if the insert query returned a unique index error, i gave an outline of the method, that you would need to use. however, it turns out that the overall code will need to be more complicated then what you have now, since you have two insert queries, one for the requisicoes (requisitions) table, and whatever insert query the ->insertRequisicaoByLastId() call executes. you would first need to determine when and if you need to execute the insert query for the requisicoes (requisitions) table or get the id for an existing row in the requisicoes (requisitions) table (actually you may be able to use an INSERT on DUPLICATE KEY UPDATE query, where the UPDATE part actually sets up the correct id value for the ->lastInsertId() method to access), to use in the rest of the code.
  16. this should (greatly) reduce the amount of code the OP has, to a single query, executed in a loop, which will make it easier to see where any problems are at, i.e. being able to see the forest for the trees. in general, you should NOT try to select data to test if it already exists, to decide if you should insert it. just try to insert the data, and with the appropriate composite unique index defined for your table, detect if the insert query returned a unique index error. to detect the unique index error (the error number is 1062) you would have a try/catch block around just the execution of the insert query and check the error number in the catch block. since you can insert multiple rows, you should set up an array to hold the returned status from all the queries that get executed inside the loop. your current code is checking the last result after the end of the loop and returning just that one piece of status information. also, i am wondering why you must explode a comma separated list back to an array of data, when your form should be submitting an array of data in the first place. shouldn't you just pass the submitted array of data to the function?
  17. the error is because you are concatenating the string 'count: ' with the isset() expression, which will always be true. you need to put () around the trinary statement so that it is evaluated as an expression that then gets concatenated with that string.
  18. the easiest way of producing this type of output is to index the data using the category value when you retrieve the data, by fetching the data into a multi-dimensional array, using the category value as the main array index, and storing each row of data in a sub-array under that index value. then just loop over the main array, which will give you the category and a sub-array of the rows for that category. then just loop over the sub-array to produce the output. after you execute the query - $data = array(); foreach($result->result() as $row) { $data[$row['CatTitle']][] = $row; } // to produce the output - foreach($data as $CatTitle=>$sub_array) { // use $CatTitle here to produce the category heading foreach($sub_array as $row) { // use the elements in $row to produce the output for each product under the category } }
  19. the OP does have a spelling mistake, but it's in the variable name being used to hold the message body. if you had php's error reporting set to E_ALL and display_errors set to ON, php would help you by reporting and displaying all the errors it detects. next, I'm surprised you are receiving any emails. the 5th parameter of the mail() function is not in the format of 'From: email', if it's used to supply the sending email address, the format is '-femail' and these emails are NOT being sent from the email address that someone entered in a form on your web site and using it as the From: email header is not correct. the line building the email header - $headers .= 'From:'.$name.' <'.$email.'>' . "\r\n"; should not be using the submitted email address. you can put the submitted email address as a Reply-to: $email email header, after validating that the value in $email is only and exactly one properly formatted email address.
  20. you need to define the 'work flow' (steps) and data for each step before you write any code. without knowing what you are ultimately trying to achieve and WHY, it's not possible to advise you based on the content in this thread. just went back and looked at this thread - https://forums.phpfreaks.com/topic/302608-large-number-of-fields-to-upload/ it would appear you are creating 'auto' provisioning files for a phone network, where the mac address is used to identify which configuration file to use for any phone. your phones db table is apparently a list of the mac addresses of the available phones? your templates db table, i would guess is a list of some typical button layouts/template file names? the extensions db table is apparently a list of extension numbers and predefined passwords? if your goal is to edit/assign a phone and a template file name to an extension number, what has been suggested so far in this thread for the form (using an array name for the form field names) is correct. you would actually retrieve any current assignment and pre-select the options in the select/option menus. you would also need to have a way of un-provisioning the phone assigned to an extension number. when the form gets submitted, the form processing code would need to insert new data and update existing data (there a single query that can do this), and delete any data for phones that have been un-assigned (not sure if you need a configuration file in this case that points the phone to a nonexistent extension number or something else to disable it.) the assignment data should be stored in a database table. this will let you edit the current assignments and will also let you produce reports based on the data. the mac-address.cfg configuration files are the end result of the assignment. if the phones are on the same network where the web server/php is at, you can let a php script serve the configuration files, without having actual files. this will keep all the data in sync, since you won't have to create and maintain actual files. anytime a phone makes a request for its configuration, it will get the current values, because they will be retrieved from the database tables at the time they are requested.
  21. actually, no. based on the form you have posted, you are selecting a phone id and a template id to be associated with an extension number. you would store only these three numbers in a database table. if you are trying to edit the secret/password, that's a different concern from assigning a phone id and template id to an extension and should be handled separately. btw - passwords should be hashed when stored, so displaying the stored value doesn't serve any useful purpose. next, you need to separate all the database specific code out of the html document, by fetching the data from each query into a php array variable, then just use the php array variables in the rest of the code. this will make it easier to write and test your code, your code will be less cluttered, and it will make it easier to switch to a different php database extension, since all the database specific code will be grouped together. as long as the same data is fetched into the php variables, you won't have to touch the rest of the code that's using the fetched data. the form field names you are using will be what php uses for the submitted data. if you are using name='phone' and name='template' for the two select/option menus, the data will be in $_POST['phone'] and $_POST['template']. there will not be any - $_POST['extension'], $_POST['secret'], and $_POST['mac'] variables because you don't have any form fields with these names. since the extension number is apparently what identifies the data being inserted, when you change the form fields to be array names, i would use the extension number as the array index. this will also support the next task you will likely have of being able to edit/update data that has already been inserted into the database table.
  22. how many rows of data, where is this data coming from/what format is it in now, and how frequently is this going to be performed, since the answers to these questions determine the best method to use to get the data into a database table(s)? if the data is already in the form of a file or some other computer based format, so that it could be uploaded as a csv formatted file, you can just run a single LOAD DATA LOCAL INFILE ... sql query to get the data into a database table. if the data is something that you can copy/paste into a single form field, a php script could break it into rows to loop over them for inserting the data into a database table. if you are going to have a set of form fields holding the data, you would use array names for the different form fields. this will cause the data to be submitted as arrays, that a php script can use a foreach(){} loop to loop over. for the cases where you will have a loop in a php script, you would use a prepared query, prepared once before the start of any looping, with place-holders for the data values, then supply each set of data and execute the query inside of the loop.
  23. where and how are the agency codes defined now? they must be defined somewhere, since you are able to produce the values you are storing in the orders table. the proper way of doing this is to have the agency codes defined in an agency_code database table, then just retrieve the values to produce the output. once you have 10's of thousands of records in the orders table, you would NOT attempt to extract the agency codes, after the fact, every time you need to produce the select/option menu.
  24. you need to read the documentation for any php function you are trying to use. strcmp returns a zero if the strings are the same. don't even use strcmp for your passwords. you should be using php's password_hash() and password_verify() to handle your password hashing and comparison. there are examples in the php.net documentation showing how to use these. next, Don't Repeat Yourself - DRY. you are already running one sql query to find and then fetch the row matching the username. don't run that query again to get a single piece of data. you already have all the data from the row, from the first time you executed the query. don't loop to fetch data from a query that will at most match a single row. the while() loop code will go away anyway since you don't need to run the query the second time. you need to use a prepared query to supply any data to an sql query statement to prevent sql injection. you should not use or die() logic to handle sql query errors. use exceptions to handle sql errors and you should not output the raw php/mysql errors to the browser on a live site. you should validate each input separately and output a unique message for each validation error. you should NOT identify if the username or the password is the reason for not being able to log in. this will allow someone/bot script to repeatedly submit values to your code and first find valid usernames, then bruit force find a password. output the same generic - invalid username/password for each of these cases. you can log internally the actual reason for a login failure. don't use $_REQUEST variables. if you expect data to be in $_POST variables, use $_POST. you should also detect that a post method form was submitted at all, before trying to use any $_POST variables. this will prevent php errors. if the current visitor is already logged in, you should not run your login form/form processing code. lastly, when the visitor is correctly authenticated, you should store the user's id in a session variable to remember that they are logged in and who they are. all other code that tests if the visitor is logged in would test if the session variable holding the user's id is set and if they need to retrieve any current data about the visitor, they would use the user id to do so. this will insure that any user data is current and up to date.
×
×
  • 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.