Jump to content

Barand

Moderators
  • Posts

    24,319
  • Joined

  • Last visited

  • Days Won

    794

Everything posted by Barand

  1. I wouldn't waste time on the first SELECT query. Place a UNIQUE key constraint on the email column then function createNewUser($pdo, $fname, $lname, $email, $password) { try { $sql2 = "INSERT INTO user (fname, lname, email, password) VALUES (:fname, :lname, :email, :password)"; $hashedPwd = password_hash($password, PASSWORD_BCRYPT); $stmt = $pdo->prepare($sql2); $stmt->execute([ ':fname' => $fname, ':lname' => $lname, ':email' => $email, ':password' => $hashedPwd ]); return 'User Created'; } catch (PDOException $e) { if ( str_contains($e->getMessage(), '1062 Duplicate entry')) { return "That email already exists"; } else throw $e; } }
  2. An insert query works better if your sql informs the server it's an INSERT query which table to insert into so needs to begin with "INSERT INTO tablename..." Also, string literals like $imageName need to be inside single quotes. However you should be using a prepared statement. I would also advise storing your image files in the filesystem and putting the metadata (such as filename) in the table.
  3. I don't fancy the chances of sorting your files by the dates embedded in their names. Instead, get the filenames, get the date of each file, store in array, then sort the dates. $files = glob("$dir/*.*"); $fdates = []; foreach ($files as $f) { $fdates[basename($f)] = date('Y-m-d',filemtime($f)); } asort($fdates); echo '<ul>'; foreach ($fdates as $fn => $dt) { echo "<li>$dt &ndash; $fn</li>"; } echo "</ul>"; Results wil look something like this
  4. If you want help sorting an array we need to know what that array looks like. Post the output from ... echo '<pre>' . print_r($files, 1) . '</pre>';
  5. Me too. Welcome.
  6. Aside from the empty WHERE clause mentioned by @gizmola above, you may also have a problem with passing your limit and offset parameters. You don't show us the relevant connection and execution code, but If your PDO connection code does not set ATTR_EMULATE_PREPARES to false (the default is true) then you cannot pass the limit and offset as parameters in an array when executing. You can, however, pass them using bindParam() You should always set this attribute to false
  7. current ------- X 100 = percent full
  8. When posting code, please use the button to place the code in a code block. (One copy of the code will suffice)
  9. That's a 'belt and braces' approach. If you have the screening_id then you have the movie_id and screen
  10. A boolean expression resolves to 0 (false) or 1 (true). Use one in your sort. ORDER BY employee <> 'Senthil', category
  11. All you're showing is snippets of code with no context of where in the code they belong. Anything output from the php script in response to an ajax request is sent back in the ajax response. If ypu want to output the screening_id to the screen it needs to be done in the response-handling function. For example, to create a link to another page, passing the sgid, when a screen number id clicked function(resp) { $.each(resp, function(mid, mdata) { let title = `<h2>${mdata.title}</h2><h4 class='w3-text-gray'>${mdata.genre} (${mdata.runtime})</h4>` $("#movie-listings").append(title) $.each(mdata.screenings, function(dt, ddata) { let datesub = `<h3>${dt}</h3>` $("#movie-listings").append(datesub) $("#movie-listings").append("<div class='screenings'") $.each(ddata, function(k, sdata) { let scr = `<div class='screening'><b>${sdata.start}</b><br> <a href='myOtherPage.php?sgid=${sdata.sgid}'>${sdata.sno}</a> //<<<< ADD </div>` $("#movie-listings").append(scr) }) $("#movie-listings").append("</div>") }) }) },
  12. In you javascript function that handles the jax response, log the response data in the console log. From there you can copy it, json_decode() it then view it with print_r()
  13. Solution1, naturally because it is the simplest. (Although I did once have a holiday job working for a guy who would prefer solution2 as multiplication was beyond him) But being a one-liner doesn't always make it the simplest ... $solution4 = array_sum(array_map(fn($v) => $v / sin(M_PI/6), array_fill_keys(range(0,4), 1)));
  14. A little exercise in logic... If you have startup errors, the code is not executed. If the code is not executed, how can you execute "display_startup_errors"? It has to be pre-set in the php.ini file.
  15. There is an alternative that you might want to consider and that is to create an additional "status" column in your users table, adding it as a generated column which will automatically maintain the user status based on the date calculations (as the above queries do). At present, I have mysql> select * from users; +----+--------------------------------------+-------+---------------------+---------------------+ | id | unique_id | fname | created_at | visited | +----+--------------------------------------+-------+---------------------+---------------------+ | 1 | 5a9c4d9c-c20d-11ee-b072-8cec4b4596d3 | aaaa | 2023-03-24 21:25:06 | NULL | | 2 | 5a9c50bd-c20d-11ee-b072-8cec4b4596d3 | bbbb | 2023-05-14 21:25:07 | 2024-02-01 21:25:07 | | 3 | 5a9c5116-c20d-11ee-b072-8cec4b4596d3 | cccc | 2023-07-11 21:25:07 | 2024-02-01 21:25:07 | | 4 | 5a9c5152-c20d-11ee-b072-8cec4b4596d3 | dddd | 2023-12-13 21:25:07 | NULL | | 5 | 5a9c5189-c20d-11ee-b072-8cec4b4596d3 | eeee | 2023-08-12 21:25:07 | NULL | | 6 | 5a9c51ba-c20d-11ee-b072-8cec4b4596d3 | ffff | 2023-09-27 21:25:07 | NULL | | 7 | 5a9c51ec-c20d-11ee-b072-8cec4b4596d3 | gggg | 2023-05-31 21:25:07 | 2023-09-01 21:25:07 | | 8 | 5a9c5221-c20d-11ee-b072-8cec4b4596d3 | hhhh | 2023-06-25 21:25:07 | NULL | | 9 | 5a9c5255-c20d-11ee-b072-8cec4b4596d3 | kkkk | 2023-09-18 21:25:07 | 2023-04-01 21:25:07 | | 10 | 5a9c5288-c20d-11ee-b072-8cec4b4596d3 | mmm | 2023-04-15 21:25:07 | NULL | +----+--------------------------------------+-------+---------------------+---------------------+ If I then alter the table, adding the new status column... mysql> ALTER TABLE users -> ADD COLUMN `status` VARCHAR(10) GENERATED ALWAYS AS -> (CASE WHEN visited IS NULL -> THEN CASE WHEN timestampdiff(DAY, created_at, now()) < 90 -> THEN 'PENDING' -> WHEN timestampdiff(DAY, created_at, now()) >= 180 -> THEN 'EXPIRED' -> ELSE 'WARNING' -> END -> ELSE CASE WHEN timestampdiff(DAY, visited, now()) < 90 -> THEN 'OK' -> WHEN timestampdiff(DAY, visited, now()) >= 180 -> THEN 'EXPIRED' -> ELSE 'WARNING' -> END -> END) VIRTUAL AFTER `visited`; I can then use a conventional GROUP BY status to get the totals of each type... mysql> SELECT status -> , COUNT(*) as total -> FROM users -> GROUP BY status; +---------+-------+ | status | total | +---------+-------+ | EXPIRED | 4 | | OK | 2 | | PENDING | 1 | | WARNING | 3 | +---------+-------+ and selecting all the data now looks like this... mysql> SELECT * FROM users; +----+--------------------------------------+-------+---------------------+---------------------+---------+ | id | unique_id | fname | created_at | visited | status | +----+--------------------------------------+-------+---------------------+---------------------+---------+ | 1 | 5a9c4d9c-c20d-11ee-b072-8cec4b4596d3 | aaaa | 2023-03-24 21:25:06 | NULL | EXPIRED | | 2 | 5a9c50bd-c20d-11ee-b072-8cec4b4596d3 | bbbb | 2023-05-14 21:25:07 | 2024-02-01 21:25:07 | OK | | 3 | 5a9c5116-c20d-11ee-b072-8cec4b4596d3 | cccc | 2023-07-11 21:25:07 | 2024-02-01 21:25:07 | OK | | 4 | 5a9c5152-c20d-11ee-b072-8cec4b4596d3 | dddd | 2023-12-13 21:25:07 | NULL | PENDING | | 5 | 5a9c5189-c20d-11ee-b072-8cec4b4596d3 | eeee | 2023-08-12 21:25:07 | NULL | WARNING | | 6 | 5a9c51ba-c20d-11ee-b072-8cec4b4596d3 | ffff | 2023-09-27 21:25:07 | NULL | WARNING | | 7 | 5a9c51ec-c20d-11ee-b072-8cec4b4596d3 | gggg | 2023-05-31 21:25:07 | 2023-09-01 21:25:07 | WARNING | | 8 | 5a9c5221-c20d-11ee-b072-8cec4b4596d3 | hhhh | 2023-06-25 21:25:07 | NULL | EXPIRED | | 9 | 5a9c5255-c20d-11ee-b072-8cec4b4596d3 | kkkk | 2023-09-18 21:25:07 | 2023-04-01 21:25:07 | EXPIRED | | 10 | 5a9c5288-c20d-11ee-b072-8cec4b4596d3 | mmm | 2023-04-15 21:25:07 | NULL | EXPIRED | +----+--------------------------------------+-------+---------------------+---------------------+---------+
  16. That's good. You shouldn't be doing that. Don't store derived data. The invoice record should essentially contain the header information for a printed invoice (customer, invoice number, invoice date etc.). All the amounts should be in the invoice items table. The invoice total is the sum of the items in the invoices (Credit items would have a negative amount.) Customer discount would then be appplied.
  17. SELECT id , unique_id , fname , date_format(visited, '%b %e %Y %h:%i%p') as visit_date , date_format(created_at, '%b %e %Y %h:%i%p') as create_date , CASE WHEN visited IS NULL THEN timestampdiff(DAY, created_at, now()) ELSE timestampdiff(DAY, visited, now()) END as elapsed , CASE WHEN visited IS NULL THEN CASE WHEN timestampdiff(DAY, created_at, now()) < 90 THEN 'PENDING' WHEN timestampdiff(DAY, created_at, now()) >= 180 THEN 'EXPIRED' ELSE 'WARNING' END ELSE CASE WHEN timestampdiff(DAY, visited, now()) < 90 THEN 'OK' WHEN timestampdiff(DAY, visited, now()) >= 180 THEN 'EXPIRED' ELSE 'WARNING' END END as status FROM users ORDER BY visited DESC; +----+--------------------------------------+-------+--------------------+---------------------+---------+---------+ | id | unique_id | fname | visit_date | create_date | elapsed | status | +----+--------------------------------------+-------+--------------------+---------------------+---------+---------+ | 2 | 5a9c50bd-c20d-11ee-b072-8cec4b4596d3 | bbbb | Feb 1 2024 09:25PM | May 14 2023 09:25PM | 1 | OK | | 3 | 5a9c5116-c20d-11ee-b072-8cec4b4596d3 | cccc | Feb 1 2024 09:25PM | Jul 11 2023 09:25PM | 1 | OK | | 7 | 5a9c51ec-c20d-11ee-b072-8cec4b4596d3 | gggg | Sep 1 2023 09:25PM | May 31 2023 09:25PM | 154 | WARNING | | 9 | 5a9c5255-c20d-11ee-b072-8cec4b4596d3 | kkkk | Apr 1 2023 09:25PM | Sep 18 2023 09:25PM | 307 | EXPIRED | | 1 | 5a9c4d9c-c20d-11ee-b072-8cec4b4596d3 | aaaa | NULL | Mar 24 2023 09:25PM | 315 | EXPIRED | | 8 | 5a9c5221-c20d-11ee-b072-8cec4b4596d3 | hhhh | NULL | Jun 25 2023 09:25PM | 222 | EXPIRED | | 6 | 5a9c51ba-c20d-11ee-b072-8cec4b4596d3 | ffff | NULL | Sep 27 2023 09:25PM | 128 | WARNING | | 5 | 5a9c5189-c20d-11ee-b072-8cec4b4596d3 | eeee | NULL | Aug 12 2022 09:25PM | 539 | EXPIRED | | 4 | 5a9c5152-c20d-11ee-b072-8cec4b4596d3 | dddd | NULL | Dec 13 2023 09:25PM | 51 | PENDING | | 10 | 5a9c5288-c20d-11ee-b072-8cec4b4596d3 | mmm | NULL | Apr 15 2023 09:25PM | 293 | EXPIRED | +----+--------------------------------------+-------+--------------------+---------------------+---------+---------+ Summary SELECT SUM( (visited IS NULL AND timestampdiff(DAY, created_at, now()) >= 180) OR (visited IS NOT NULL AND timestampdiff(DAY, visited, now()) >= 180) ) as expired , SUM( (visited IS NULL AND timestampdiff(DAY, created_at, now()) BETWEEN 90 and 179) OR (visited IS NOT NULL AND timestampdiff(DAY, visited, now()) BETWEEN 90 and 179) ) as warning , SUM(visited IS NULL AND timestampdiff(DAY, created_at, now()) < 90) as pending , SUM(visited IS NOT NULL AND timestampdiff(DAY, visited, now()) < 90) as ok FROM users; +---------+---------+---------+------+ | expired | warning | pending | ok | +---------+---------+---------+------+ | 5 | 2 | 1 | 2 | +---------+---------+---------+------+
  18. Update - not there yet. Selecting visited and created_at in a query that returns only a single row is meaningless - you will just get single arbitrary dates from any one of your 349 records. The "working" final solution does not include those cases where the user has visited but not in the last 3 or 6 months (You have the Xs but not the Os below) Summary decision table... Has visited? | Y Y Y | N N N | | | | How long ago? | <=3 3 - 6 >=6 | <=3 3 - 6 >=6 | --------------------+-----------------------+-----------------------+ | | | EXPIRED | O | X | | | | WARNING | O | X | | | | PENDING | | X | | | | OK | X | |
  19. What error messages are you getting? Do you have error reporting turned on? [PS]... $values 3 has an element where $z = "10.36.240.65". $values2 has no such element therefore it throws an undefined key error - which error reporting would have told you. In your php.ini file, set error_reporting = E_ALL display_errors = on display_startup_errors = on
  20. Indexing is a prime requirement of relational databases. Even if you aren't using formal foreign key constraints, the foreign keys should be indexed. So should other columns frequently used in searching.
  21. An alternative (if your MySQL supports WINDOW functions) which puts the totals in each record.. SELECT id , unique_id , fname , date_format(visited, '%b %e %Y %h:%i%p') as vis_date , date_format(created_at, '%b %e %Y %h:%i%p') as cr_date , visited , created_at , CASE WHEN visited IS NULL AND timestampdiff(MONTH, created_at, now()) >= 15 THEN 'EXPIRED' WHEN visited IS NULL AND timestampdiff(MONTH, created_at, now()) < 3 THEN 'PENDING' WHEN visited IS NOT NULL THEN 'OK' ELSE '???' END as status , SUM(visited IS NULL AND timestampdiff(MONTH, created_at, now()) >= 15) OVER () as expired , SUM(visited IS NULL AND timestampdiff(MONTH, created_at, now()) < 3) OVER () as pending , SUM(visited IS NOT NULL) OVER () as ok FROM users ORDER BY visited DESC What about those who joined between 3 and 15 months ago but haven't visited yet. What status should they have?
  22. You would get totals of 1 or 0 for every record. SELECT id , unique_id , fname , date_format(visited, '%b %e %Y %h:%i%p') as vis_date , date_format(created_at, '%b %e %Y %h:%i%p') as cr_date , visited , created_at , visited IS NULL AND timestampdiff(MONTH, created_at, now()) >= 15 as expired , visited IS NULL AND timestampdiff(MONTH, created_at, now()) < 3 as pending , visited IS NOT NULL as ok FROM users ORDER BY visited DESC You could then accumulate them as you list the results
×
×
  • 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.