Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,367
  • Joined

  • Days Won

    173

Everything posted by mac_gyver

  1. the reason the code went from working to not, when you changed to the * is because the capitalization of your actual column names don't match what the php code is using. when you were specifically selecting the columns, the names in the SELECT statement are what was used in the retrieved data AND they matched the letter case of whatever the column names actually are because in the sql statement the letter case of the column names don't matter. if you do a print_r($row); in your code, you will see what the associative array index names are. constancy counts in programming, whatever your column names are, should be used throughout your code. also, if had php's error reporting set to E_ALL and display_errors set to ON (preferably in the php.ini on your development system), php would help you by reporting and displaying all the errors it detects. next, don't write out line after line of code who's only purpose is to copy variables to other variables. use the original $row[...] variables. finally, when something doesn't work, provide the actual output you are getting. getting 'no results' has different meaning depending on who you are and what your definition of results is.
  2. if that question is about doing things automatically, you would set up a cron job/scheduled task to run a php script, once a day. the php script would check if there are any operations to perform on the particular date or day of the month. for the add rent operation, it would query for the active tenancies (where the current date is between the start date and the end date of the tenancy), and add 'rent due' records with the appropriate field values to the payment table.
  3. actually, i see a mis-statement - the late fee would be a negative amount (an amount due), waiving of the late fee would be row with a positive amount (a credit made to the account.)
  4. the problem is in the 'save the data' part of the process. all the posted information, except the one line of php code with the escape string call in it, is about the google map auto-fill api, which occurs before you submit the form. you haven't shown the form (is there a form? is the form field inside the form? does the form use method='post'?), how the form gets submitted (is the browser submitting the form or are you using ajax?), or enough of the form processing code (could the form processing code be running, but you are redirecting/re-requesting the page and the error you are seeing is from the second execution of the page?) in short, post the code that's relevant to the problematic part of the process, that would be needed to reproduce the problem, in order to get the quickest solution.
  5. this is/should be the same as a bank or credit card account. you are inserting rows that represent positive and negative amounts for an account, the tenancy_id in this case. you can SUM the amounts for each account/tenancy_id to determine what the balance is on any date. when the rent becomes due in a month, you would add a row to the payment table, with a negative amount and the appropriate amount_type_id for rent due, along with the correct tenancy_id and date. this will result in a negative balance (amount due) when you SUM() the amount column for each tenancy_id (you would GROUP BY tenancy_id in the sql statement.) if payment(s) are made (row(s) with a positive amount) they offset the rent due, when you SUM() the amount column on the 'late fee date' you will get a zero. if you get a negative result, it would mean that the (whole) rent has not been paid and you would insert a row in the payment table for the late fee (a negative amount with the appropriate values for the other columns.)
  6. the tables that hold 'defining' data should contain the unique/one-time and generally static information for what they define. a property table should only contain information about properties. a property can have a succession of tenants, each with different rent amounts, so the information about each instance of a tenancy needs to be stored in a second table. the payment table holds information related to the instances of tenancy. the actual tenant/renter personal information is separate from this data. see the following list of tables i would start with - property - id | title | address tenancy - id | property_id | tenant_id | rent | start_date | end_date tenant - id | first_name | last_name | email | phone payment - id | tenancy_id | amount | amount_type_id | date | note amount_type - id | name most of this should be self-explanatory. the amount_type would be things like full rent, partial rent, late fee, waived late fee, rent refund, ... the payment table would hold actual payment amounts. plus amounts are payment in and minus amounts are payment out. the code that calculates the late fees would add row(s) to the payment table, with the amount_type_id for a late fee. if a specific late fee is waived, the row that gets added to the payment table would have a negative amount equal to the late fee amount and an amount_type_id for a waived late fee.
  7. to create a multi-user system, you must first have a login system. this will define user ids that you would store in any related data table to identify who the data is for. you would NOT have columns in your data table for each user. you would instead store a row in your data table for each user/exercise combination. if your page(s) can only be accessed when there is a logged in user, you would detect if there is a logged user first and only display information and process forms if there is. if there is not a logged in user, you would display a login form and enable the processing of the login form. next, you would have a table where the exercises are defined, that has an id (auto-increment) column and an exercise name column. you would query this table to get a list of the exercises for the select/option menu. you would store the exercise id in any table holding data related to the exercises. your data table would then have columns for the user id, the exercise id, and the weight. you would insert a new row if the data for a user/exercise doesn't exist and update the row if it does. there's a single query that will do this, an INSERT ... ON DUPLICATE KEY UPDATE ... query. you would define the user id and exercise id columns as a composite index to allow this query to work. lastly, your code needs to be organized as follows - define or require any configuration/settings and functions that your main code needs. start the session. create the database connection. you should only do this once. process any post method form data. there would be a section of code for each form the page must process. get/produce any data needed to display the page. the data should be stored in php variables. there would be a section of code for each different thing that can be displayed on the page. output the html document.
  8. example code - // build the dynamic WHERE terms $and_terms = array(); // an array to hold the AND terms for the sql statement $params = array(); // an array to hold the data values for the prepared query if(!empty($_GET['d1'])) { // there is a starting date // you would validate the submitted data here... // if the data is valid, add the elements to the two arrays $and_terms[] = "date >= ?"; $params[] = $_GET['d1']; } if(!empty($_GET['d2'])) { // there is an ending date // you would validate the submitted data here... // if the data is valid, add the elements to the two arrays $and_terms[] = "date <= ?"; $params[] = $_GET['d2']; } if(!empty($_GET['status'])) { // there is a status (Open/Closed) value // you would validate the submitted data here... // if the data is valid, add the elements to the two arrays $and_terms[] = "status = ?"; $params[] = $_GET['status']; } $where_term = ''; // define an empty default where term if(!empty($and_terms)) { $where_term = 'WHERE ' . implode(' AND ',$and_terms); } $query = "SELECT * FROM invoices $where_term ORDER BY s_doc ASC"; $result = $db->prepare($query); $result->execute($params); // note: this works even if $params is an empty array
  9. you need to build the WHERE clause dynamically, so that it only contains the term(s) that you want the query to use to match data. the easiest way of doing this is to add each term to an array, then implode the array using the ' AND ' keyword as the implode separator. this will even work if there is only one term. at the same time you add each term to this array, you would add the corresponding data value to a parameters array, to be used to supply the data to the prepared query. btw - your existing php conditional logic isn't doing what you think. all three of the form fields will be set any time the form has been submitted. you should instead just detect if each form field has a non-empty value. the empty() function will not throw an error if the form field isn't set. if you want your date comparison to work if none, only one, or both dates have values, use individual comparisons in the sql statement, not the BETWEEN comparison. you would add the comparison term to the array i have suggested for each date that has a value. this again will cause the logic to work regardless of how many of the dates have values. also, your search form should be on the same page as your report code and you should re-populate the form fields with any existing get data. this will allow the user to both see and alter the search parameters. if i get a chance i will post some example code.
  10. 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.
  11. the first two answers don't have anything to do with your question.
  12. 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.
  13. 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.
  14. use isset() to (directly) test if the item is in the cart or not - if(isset($_SESSION["cart_products"][$room->id])) {
  15. 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.
  16. 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.
  17. 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.
  18. 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.
  19. 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.
  20. 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.
  21. 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.
  22. 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.
  23. 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.
  24. 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.
  25. 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?
×
×
  • 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.