Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. You don't. It's all about efficiently accessing that data after you have stored it. With a separate table the data can be indexed. When searching the query uses the indexes. For instance Who has a child called "John"? With a comma-separated list you have to read every parent record and then search their lists for 'John'. With an indexed table you can can go directly to "John" and see who the parent is. Relational database tables are not like spreadsheet tables. Instead, via a process of normalization (Google it) the data is split into related tables linked via indexes for rapid retrieval.
  2. Remove the single quotes from around the tablename and column name identifiers. Quotes are for string literals. $sql="INSERT INTO 'users' ('name','email','phone','bgroup') VALUES ('$name','$email','$phone','$bgroup')"; ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ REMOVE Also, you should use a prepared statement instead of putting user-provided variables into your query.
  3. How do you inset an array? You don't. Store each array element in a single record in another table with the id of its parent record. Example: We have 3 parents with children Laura has children Peter, Paul, Mary Scott Curly, Larry, Mo Tom Jane, John They would be stored as follows... TABLE: parent TABLE: child +-------+--------------------+ +-------+------------+---------+ | ID | Name | | ID | ParentID | Name | +-------+--------------------+ +-------+------------+---------+ | 1 | Laura Norder | | 1 | 1 | Peter | | 2 | Scott Chegg | | 2 | 1 | Paul | | 3 | Tom DiCanari | | 3 | 1 | Mary | +-------+--------------------+ | 4 | 2 | Curly | | 5 | 2 | Larry | | 6 | 2 | Mo | | 7 | 3 | Jane | | 8 | 3 | John | +-------+------------+---------+
  4. It won't let you INSERT a new record with the same primary key as an existing record. The only way you can overwrite them is if you UPDATE them (or INSERT with an ON DUPLICATE KEY UPDATE option)
  5. Apparently hasn't yet learned how to use option value attributes within <select> inputs.
  6. Have you checked your data and input variable values?
  7. And the problem is what, exactly?
  8. Give us something processable, like a json_encode() version of your object, and you are more likely to get help.
  9. Looks OK to me CODE define("corrections", array("<?=" => "<?php echo ")); $fileContents = ' <? $a = "1"; ?><?=$a;?> '; $newFileContents = str_replace(array_keys(corrections), corrections, $fileContents); echo '<pre>' . htmlentities($newFileContents) . '</pre>'; OUTPUT <? $a = "1"; ?><?php echo $a;?>
  10. Is the database on same computer as your script?
  11. If the username is correct then is your password valid for the usename?
  12. Check the code where you connect to the database server. You appear to be using the database name as the user name - is that correct?
  13. Have you considered using ifnull() on that too? SELECT ..., ifnull(b.qty_bot, 0) as qty_bot, ... Or an aternative to isnull() is coalesce() SELECT ..., coalesce(b.qty_bot, 0) as qty_bot, ...
  14. When you run this script, are you putting a value for "code" in the query string? EG http://localhost/myscript.php?code=123 Check whether code exists before trying to access it if (isset($_GET['code']) { $accessToken = $authProvider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); } else { // error - no code }
  15. You are referencing aliases before they have been defined. For example, you dont define the table alias "b" until the last line of the query but you are referencing it several time earlier subqueries in the query. Why output identical values twice? ... SELECT a.prod_name , a.prod_size , ifnull(a.qty_received, 0) qty_received , ifnull(b.qty_bot, 0) qty_bot , ifnull(a.qty_received, 0) - ifnull(b.qty_bot, 0) qty_remain FROM ( SELECT prod_id FROM tbl_distribution UNION SELECT prod_id FROM tbl_sales_bar ) t LEFT JOIN ( SELECT prod_id, prod_name, prod_size, SUM(prod_qty) qty_received FROM tbl_distribution WHERE staff_id = 2962 GROUP BY prod_id ) a on a.prod_id = t.prod_id LEFT JOIN ( SELECT prod_id, SUM(qty_bought) qty_bot FROM tbl_sales_bar WHERE staff_id = 2962 GROUP BY prod_id ) b on b.prod_id = t.prod_id; results +---------------+-----------+--------------+---------+------------+ | prod_name | prod_size | qty_received | qty_bot | qty_remain | +---------------+-----------+--------------+---------+------------+ | 33 | | 13 | 8 | 5 | | Star Wrangler | | 7 | 6 | 1 | | Star | | 19 | 7 | 12 | | Goldberg | | 10 | 0 | 10 | +---------------+-----------+--------------+---------+------------+
  16. OK, post it and I'll have look.
  17. If you have a different problem, post it in a new topic.
  18. Your query joins each record from 'a' to many records from 'b' thus multiplying your totals. Try SELECT a.prod_id, a.prod_name , a.qty_received , b.qty_bot , a.qty_received - b.qty_bot as qty_remain FROM ( SELECT prod_id , prod_name , sum(prod_qty) as qty_received FROM tbl_distribution WHERE staff_id = 2962 GROUP BY prod_id ) a LEFT JOIN ( SELECT prod_id , sum(qty_bought) as qty_bot FROM tbl_sales_bar WHERE staff_id = 2962 GROUP BY prod_id ) b USING (prod_id); Your tables are in dire need of normalization. For example, "prod_name" should occur once in your database (in a product table) and not be repeated in several tables. PS In future, don't post pictures of your data, post the data. It was not a great deal of fun building the test tables from those images so that I could test your query.
  19. What do get if you run the subquery on its own? SELECT GROUP_CONCAT(DISTINCT Average ORDER BY Average DESC ) FROM mark; I suspect that your "average" column is varchar instead of a numeric type. (in my table it's DECIMAL(8,2) so that it sorts correctly.
  20. The problem is his posting the text parts of the posts in the code boxes so that no line wrapping occurs. And yes, a PIA. (I usually skip posts that require me to scroll right into the middle of next week in order to read them.)
  21. I get results 1 and 3, which look right. DATA +---------+----------+--------+--------+------+---------+ | mark_id | emp_name | emp_no | branch | year | average | +---------+----------+--------+--------+------+---------+ | 1 | Curly | 1 | A | 2023 | 9.00 | | 2 | Larry | 2 | B | 2023 | 75.00 | | 3 | Mo | 3 | C | 2023 | 66.67 | +---------+----------+--------+--------+------+---------+ BPOSITION SELECT Average , Emp_Name , FIND_IN_SET( Average, ( SELECT GROUP_CONCAT(DISTINCT Average ORDER BY Average DESC ) FROM mark WHERE Branch = 'A' )) AS rank FROM mark WHERE Emp_Name = 'Curly' AND Year = '2023' ORDER BY Average DESC; +---------+----------+------+ | Average | Emp_Name | rank | +---------+----------+------+ | 9.00 | Curly | 1 | +---------+----------+------+ NPOSITION SELECT Average , Emp_Name , FIND_IN_SET( Average, ( SELECT GROUP_CONCAT(DISTINCT Average ORDER BY Average DESC ) FROM mark )) AS rank FROM mark WHERE Branch = 'A' AND Emp_Name = 'Curly' AND Year = '2023' ORDER BY Average DESC; +---------+----------+------+ | Average | Emp_Name | rank | +---------+----------+------+ | 9.00 | Curly | 3 | +---------+----------+------+ PS I don't think you should be using DISTINCT in your GROUP_CONCAT. If the 3 values were 9.00, 75.00, 75.00 then, with your DISTINCT it would be ranked 2 whereas it should still be 3.
  22. Use your browser's developer tools network tab to check what is being sent and received by the ajax request
  23. For $_POST['submit'] to exist there must be an input form element with the name 'submit'. A better way to check is if ($_SERVER['REQUEST_METHOD'] == 'POST') { // data was posted - process it }
  24. You're making it look too easy
  25. I wasn't sure if you wanted the Column/Row keys like A1 A2 A3 A4 A5 or the values from the matrix (eg 11 44 66 99 111). I used a recursive method... $combos = []; $matrix = [ 1 => [ 'A' => 11, 'B' => 22, 'C' => 33 ], 2 => [ 'A' => 44, 'B' => 55, 'C' => 66 ], 3 => [ 'A' => 66, 'B' => 77, 'C' => 88 ], 4 => [ 'A' => 99, 'B' => 100, 'C' => 110], 5 => [ 'A' => 111, 'B' => 122, 'C' => 133] ]; buildCombos($matrix, $combos, 0); function buildCombos(&$matrix, &$combos, $row, $col='', $com='' ) { if ($row > 5) return; if ($col) $com .= "$col$row "; foreach (['A','B','C'] as $c) { buildCombos($matrix, $combos, $row+1, $c, $com); } if ($row==5) { $combos[] = $com; return; } } echo '<pre>' . print_r($combos, 1) . '</pre>';
×
×
  • 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.