-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
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.
-
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.
-
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 | +-------+------------+---------+
-
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)
-
Apparently hasn't yet learned how to use option value attributes within <select> inputs.
-
Have you checked your data and input variable values?
-
And the problem is what, exactly?
-
Give us something processable, like a json_encode() version of your object, and you are more likely to get help.
-
str_replace using array map not returning expected results.
Barand replied to jasonc310771's topic in PHP Coding Help
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;?> -
Is the database on same computer as your script?
-
If the username is correct then is your password valid for the usename?
-
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?
-
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, ...
-
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 }
-
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 | +---------------+-----------+--------------+---------+------------+
-
OK, post it and I'll have look.
-
If you have a different problem, post it in a new topic.
-
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.
-
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.
-
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.
-
Use your browser's developer tools network tab to check what is being sent and received by the ajax request
-
PHP Form just errors every time - Can't figure out why!
Barand replied to Johnnygrr's topic in PHP Coding Help
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 } -
You're making it look too easy
-
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>';