-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
PHP 5 code not running in commandline, runs in browser in PHP 7.2
Barand replied to paul_97's topic in PHP Coding Help
That dzone link advocates some really bad practices. Ignore. -
Every element in an array has a key and a value A value may itself be an array So your would probably look something like this $questionnaire = [ 'title' => 'questionnaire one', 'intro' => 'Synopsis goes here', 'sections' => [ '1' => [ 'intro' => '', 'questions' => [ '1' => 'question 1 text', '2' => 'question 2 text' ] ], '2' => [ 'intro' => 'Some text here', 'questions' => [ '3' => 'question 3 text', '4' => 'question 4 text' ] ] ] ]; which could come from data like this +---------------------+ | questionnaire | +---------------------+ | quaire_id |-----+ | title | | | intro | | +---------------------+ | +-----------------+ | | section | | +-----------------+ | | section_id |------+ +-------<| quaire_id | | | section_no | | | intro | | +-----------------+ | +-------------------+ | | question | | +-------------------+ | | q_id | +----------<| section_id | | q_number | | q_text | +-------------------+
-
The problem is superglobals (post) values are not defined
Barand replied to JoshEir's topic in PHP Coding Help
Cold, cloudy, wet and in covid lockdown. Apart from that everything's great. -
The problem is superglobals (post) values are not defined
Barand replied to JoshEir's topic in PHP Coding Help
The bit I gave you a fix for in my first reply. -
The problem is superglobals (post) values are not defined
Barand replied to JoshEir's topic in PHP Coding Help
Looks like you still have name='$productID' instead of name = 'productID' -
The problem is superglobals (post) values are not defined
Barand replied to JoshEir's topic in PHP Coding Help
Not from what you've posted. I get no sense of structure or sequence so I don't know how it all hangs together. -
The problem is superglobals (post) values are not defined
Barand replied to JoshEir's topic in PHP Coding Help
Then the form data isn't being submitted (to upload2) -
The problem is superglobals (post) values are not defined
Barand replied to JoshEir's topic in PHP Coding Help
put this at the top of upload2.php so you can see what's being posted to it echo '<pre>' . print_r($_POST, 1) . '</pre>'; -
The problem is superglobals (post) values are not defined
Barand replied to JoshEir's topic in PHP Coding Help
If $productID contains '123' and $filename contains 'abc.txt' then upload2 will receive $_POST['123'] with a value of 'A' $_POST['abc.txt'] with a value of 'B' It looks like you need <input type='hidden' name='productID' value='$productID'> <input type='hidden' name='filename' value='$flename'> -
Yes, you do. You are using a database, not a spreadsheet. Don't repeat multiple like columns in a single row. You should put them in separate rows along with the id of the parent record as a foreign key. Each row in a database table table should contain data about a single entity (in this case, an image). Read up on "Data normalization" +----------------+ | tableA | +----------------+ | id (PK) |-----+ | etc. | | +----------------+ | +----------------+ | | list | | +----------------+ +---<| tableA_id (FK)| | image | +----------------+ You can't rely on the file extension to be an accurate indication of the file type (You could rename a text file to "mytext.png" but that wouldn't make it an image.) The mime type is in the $_FILES array.
-
Don't lose any sleep over it
-
Hmm! I would have expected your original code to throw a syntax error. Apparently it doesn't. Very odd..
-
The settings are in you "php.ini" file. I have highlighted the settings you want for development. On a production site you would log errors instead of displaying them.
-
Too many "="s. $stmt = $dbo->prepare = ("SELECT * FROM products WHERE ProductName = ?"); ^ REMOVE EDIT: Turn on your error reporting.
-
Have you remembered the "[]" in the file input field name? <input type='file' name='filestoupload[]' multiple>
-
-
Have you considered defining email and phone columns as UNIQUE then trapping dupe key errors on insert? That should be far better than searching for potential dupes before inserting. (You would have to cleanse your data of current duplicates first, however) Yes thanks. The ~3000 duplicates in my last post are from your data. Perhaps, for a single search, you could mysql> SELECT id -> FROM customers -> WHERE email = '[email protected]' -> UNION -> SELECT id -> FROM customers -> WHERE phone = '612-772-4311'; +-------+ | id | +-------+ | 11902 | +-------+ 1 row in set (0.00 sec)
-
The killer with my query with your data is the join condition ((a.email = b.email) OR (a.phone = b.phone)) Using the OR ... mysql> SELECT DISTINCT -> a.id -> FROM customers a -> JOIN ( -> SELECT c.id -> , c.email -> , c.phone -> FROM customers c JOIN quotes q ON q.customer_id = c.id AND q.purchased = 1 -> ) b ON ((a.email = b.email) OR (a.phone = b.phone)) -> AND a.id <> b.id -> ORDER BY a.id; +-------+ | id | +-------+ | 9 | | 13 | | 21 | | 36 | | 38 | | 41 | . . . | 11878 | | 11879 | | 11887 | | 11893 | | 11900 | | 11903 | +-------+ 2987 rows in set (1 min 29.25 sec) !!! Using a UNION to pull those matching on email then those matching on phone ... mysql> SELECT -> a1.id -> FROM customers a1 -> JOIN -> customers b1 ON (a1.email = b1.email) AND a1.id <> b1.id -> JOIN -> quotes q1 ON q1.customer_id = b1.id AND q1.purchased = 1 -> UNION -> SELECT -> a.id -> FROM customers a -> JOIN -> customers b ON (a.phone = b.phone) AND a.id <> b.id -> JOIN -> quotes q ON q.customer_id = b.id AND q.purchased = 1 -> ORDER BY id; +-------+ | id | +-------+ | 9 | | 13 | | 21 | | 36 | | 38 | | 41 | . . . | 11878 | | 11879 | | 11887 | | 11893 | | 11900 | | 11903 | +-------+ 2987 rows in set (1.33 sec)
-
Your query has "dependent subqueries" (to be avoided) which are ineffecient compared to joins (which mine uses) which is why I did my own 10000 record test. I couldn't believe yours was faster. Adding a dupe email on records 1 and 10,000 (2 rows returned) gave your time as 0.156 sec and mine as 0.015 (10x diff).
-
TEST 2 10,000 customers. IDs 890 and 8000 have same phone number. 890 has purchased = 1 mysql> select count(*) from customers; +----------+ | count(*) | +----------+ | 10000 | +----------+ Your query mysql> SELECT -> `a`.`id` AS `customer_id` -> FROM -> `customers` `a` -> WHERE -> ( -> SELECT -> `b`.`id` -> FROM -> `customers` `b` -> JOIN `quotes` ON `b`.`id` = `quotes`.`customer_id` -> WHERE -> ( (`a`.`email` = `b`.`email`) OR(`a`.`phone` = `b`.`phone`) ) -> AND(`a`.`id` <> `b`.`id`) -> AND (`quotes`.`purchased` = 1) -> GROUP BY -> `a`.`email` -> ); +-------------+ | customer_id | +-------------+ | 8000 | +-------------+ 1 row in set (0.13 sec) My query mysql> SELECT DISTINCT -> a.id -> FROM customers a -> JOIN -> customers b ON ((a.email = b.email) OR (a.phone = b.phone)) -> AND a.id <> b.id -> JOIN -> quotes qb ON qb.customer_id = b.id AND qb.purchased = 1 -> ORDER BY a.id; +------+ | id | +------+ | 8000 | +------+ 1 row in set (0.02 sec) 6.5x faster !?
-
I have added quotes to my data and queries. This is my test data.. My query mysql> SELECT DISTINCT -> a.id -> FROM customers a -> JOIN -> customers b ON ((a.email = b.email) OR (a.phone = b.phone)) -> AND a.id <> b.id -> JOIN -> quotes qa ON qa.customer_id = a.id AND qa.purchased = 1 -> JOIN -> quotes qb ON qb.customer_id = b.id AND qb.purchased = 1 -> ORDER BY a.id; +----+ | id | +----+ | 1 | | 2 | | 3 | | 4 | | 5 | | 9 | +----+ Your query (on the same data) gives mysql> SELECT -> `a`.`id` AS `customer_id` -> FROM -> `customers` `a` -> WHERE -> ( -> SELECT -> `b`.`id` -> FROM -> ( -> `customers` `b` -> JOIN `quotes` ON -> ( -> ( -> `b`.`id` = `quotes`.`customer_id` -> ) -> ) -> ) -> WHERE -> ( -> ( -> (`a`.`email` = `b`.`email`) OR(`a`.`phone` = `b`.`phone`) -> ) AND(`a`.`id` <> `b`.`id`) AND(`quotes`.`purchased` = 1) -> ) -> GROUP BY -> `a`.`email` -> ); +-------------+ | customer_id | +-------------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 9 | | 12 | | 15 | +-------------+
-
75 seconds seems excessively long! If you want to email me a dump of those two tables I'll see if can can speed it up for you.
-
Here's my version of your page, keeping the PHP separated from the HTML a far as is possible. <?php $res = $con->query("SELECT user_id , user_firstname , user_lastname , user_username , user_phone , GROUP_CONCAT(site_name SEPARATOR ', ') as site , user_title_id , user_role_id FROM users u LEFT JOIN site s ON u.user_id = s.site_manager_id GROUP BY user_id; "); $data = ''; foreach ($res as $row) { $data .= "<tr><td>" . join('</td><td>', $row) . "</td><td class='table-action'> <a href='#'><i class='align-middle' data-feather='edit-2'></i></a> <a href='#'><i class='align-middle' data-feather='trash'></i></a> </td> </tr>\n"; } ?> <!DOCTYPE html> <html> <head> <title>Example Multiple Locations</title> <meta http-equiv="content-language" content="en"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <table id="datatables-column-search-select-inputs" class="table table-striped" style="width:100%"> <thead> <tr> <th>ID</th> <th>FirstName</th> <th>LastName</th> <th>Username</th> <th>Phone #</th> <th>Location</th> <th>Title</th> <th>Role</th> <th>Actions</th> </tr> </thead> <tbody> <?=$data?> </tbody> </table> </body> </html>
-
Don't run queries inside loops. Use a single with join/s to get all the data in one go. Don't use "SELECT * ". Specify the fields you need TEST DATA TABLE: users +---------+----------------+---------------+---------------+------------+---------------+--------------+ | user_id | user_firstname | user_lastname | user_username | user_phone | user_title_id | user_role_id | +---------+----------------+---------------+---------------+------------+---------------+--------------+ | 1 | Peter | Dowt | peterd | 1234 | 1 | 1 | | 2 | Laura | Norder | lauran | 2345 | 2 | 2 | | 3 | Tom | DiCanari | tomd | 3456 | 1 | 1 | | 4 | Scott | Chegg | cheggs | 4567 | 2 | 2 | | 5 | Polly | Vinyl | pollyv | 5678 | 3 | 1 | +---------+----------------+---------------+---------------+------------+---------------+--------------+ TABLE: site +---------+-----------+-----------------+ | site_id | site_name | site_manager_id | +---------+-----------+-----------------+ | 1 | Site A | 2 | | 2 | Site B | 2 | | 3 | Site C | 4 | | 4 | Site D | 4 | | 5 | Site E | 5 | +---------+-----------+-----------------+ QUERY SELECT user_id , user_firstname , user_lastname , user_username , user_phone , GROUP_CONCAT(site_name SEPARATOR ', ') as site , user_title_id , user_role_id FROM users u LEFT JOIN site s ON u.user_id = s.site_manager_id GROUP BY user_id; RESULTS +---------+----------------+---------------+---------------+------------+----------------+---------------+--------------+ | user_id | user_firstname | user_lastname | user_username | user_phone | site | user_title_id | user_role_id | +---------+----------------+---------------+---------------+------------+----------------+---------------+--------------+ | 1 | Peter | Dowt | peterd | 1234 | | 1 | 1 | | 2 | Laura | Norder | lauran | 2345 | Site B, Site A | 2 | 2 | | 3 | Tom | DiCanari | tomd | 3456 | | 1 | 1 | | 4 | Scott | Chegg | cheggs | 4567 | Site D, Site C | 2 | 2 | | 5 | Polly | Vinyl | pollyv | 5678 | Site E | 3 | 1 | +---------+----------------+---------------+---------------+------------+----------------+---------------+--------------+
-
That strikes me as a problem with the data, not the query.