-
Posts
24,606 -
Joined
-
Last visited
-
Days Won
831
Everything posted by Barand
-
How to count duplicated values in a table and display it?
Barand replied to imbruceter's topic in PHP Coding Help
I use the standard convention of naming tables in the singular (category, post etc). You don't. Put this code immediately before you create your connect... mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); and make sure your error reporting is on.- 11 replies
-
- php
- phpmyadmin
-
(and 3 more)
Tagged with:
-
How to count duplicated values in a table and display it?
Barand replied to imbruceter's topic in PHP Coding Help
try $result = $connection->query("SELECT c.title , COUNT(p.category_id) as total FROM category c LEFT JOIN post p ON c.id = p.category_id GROUP BY c.id "); foreach ($result as $row) { echo "{$row['title']} : {$row['total']}<br>"; }- 11 replies
-
- php
- phpmyadmin
-
(and 3 more)
Tagged with:
-
How to count duplicated values in a table and display it?
Barand replied to imbruceter's topic in PHP Coding Help
It would look something like this SELECT c.title , COUNT(p.category_id) as total FROM category c LEFT JOIN post p ON c.id = p.category_id GROUP BY c.id- 11 replies
-
- php
- phpmyadmin
-
(and 3 more)
Tagged with:
-
Use an array - it cuts out a lot of repetetive code $domains = [ "bscscan" => "bscscan.com", "tronscan" => "tronscan", // .com ??? "dgb" => "dgb.com", "hecoinfo" => "heco.com", "polygonscan" => "polyn.com", "rvn" => "rvn.com", "etherscan" => "ethn.com", "thundercore" => "tttn.com", "solscan" => "solscan.com" ]; $address = strtolower($checkSqlRow["ADDRESS"]); echo "<td><a href='https://{$domains[$checkSqlRow["BLOCKCHAIN"]]}/token/$address'>$address</a></td>";
-
PHP comment system, handling replies of replies using a loop of some sort.
Barand replied to oz11's topic in PHP Coding Help
-
Duplicate ID issue when trying to copy table row
Barand replied to EarthDay's topic in PHP Coding Help
If you never want any data to be overwritten then you need always to insert a new record every time with a new id. Ensure your table in the destination db has an auto_incrementing primary key, for example CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `studentname` varchar(20) DEFAULT NULL, `class_id` char(2) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Let's start with db1.student and db2.student tables db1.student db2.student +----+-------------+----------+ +----+-------------+----------+ | id | studentname | class_id | | id | studentname | class_id | +----+-------------+----------+ +----+-------------+----------+ | 1 | Peter | A1 | | 1 | Curly | A1 | | 2 | Paul | A2 | | 2 | Larry | A2 | | 3 | Mary | A3 | | 3 | Mo | A3 | +----+-------------+----------+ +----+-------------+----------+ To auto-generate a new key, the insert needs to exclude the id column. This means you cannot use "SELECT * ", you must define the column excepting the id. Similarly, the query must define the destination columns in the receiving table. So we have this (assuming we are connected to db1 as default db) ... INSERT INTO db2.student (studentname, class_id) SELECT studentname, class_id FROM student WHERE id = 1 which, when executed, gives this in db2.student table +----+-------------+----------+ | id | studentname | class_id | +----+-------------+----------+ | 1 | Curly | A1 | | 2 | Larry | A2 | | 3 | Mo | A3 | | 4 | Peter | A1 | +----+-------------+----------+ -
If you know it's Y-m-d why have you specified Y/m/d ??? It's the <input type='date'> that is using the locale to display dd/mm/yyyy. It posts it to your code as yyyy-mm-dd as the print_r($_POST) shows... All the clues are there but you seem to be ignoring them
-
But the format of your dates is "Y-m-d"...
-
My theory is that class and section are defined as foreign keys in the student table. Even though they are no longer mandatory is not sufficient just to to leave them as blank values. Consider this scenario with class table and student table. Class_id in the student table is defined as a foreign key, so id values in the that table must match a perent records id in the class table. CREATE TABLE `class` ( CREATE TABLE `student` ( `id` char(2) NOT NULL, `id` int(11) NOT NULL, `classname` varchar(20) DEFAULT NULL, `studentname` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) `class_id` char(2) DEFAULT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=utf8; PRIMARY KEY (`id`), KEY `idx_student_class_idx` (`class_id`), CONSTRAINT `idx_student_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +----+-----------+ +----+-------------+----------+ | id | classname | | id | studentname | class_id | +----+-----------+ +----+-------------+----------+ | A1 | Class A | | 1 | John | A1 | | A2 | Class B | | 2 | Jane | A2 | | A3 | Class C | | 3 | Curly | A3 | +----+-----------+ +----+-------------+----------+ If we now attempt to insert a new student with a blank class_id insert into student (id, studentname, class_id) values (4, 'Larry', '' ); result: ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`student`, CONSTRAINT `idx_student_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) The way around this is explicitly to write NULL into the class_id of the student record insert into student (id, studentname, class_id) values (5, 'Mo' , null); Query OK, 1 row affected (0.06 sec) +----+-------------+----------+ | id | studentname | class_id | +----+-------------+----------+ | 1 | John | A1 | | 2 | Jane | A2 | | 3 | Curly | A3 | | 5 | Mo | NULL | +----+-------------+----------+
-
And I wasn't volunteering; I was suggesting a means by which you might find the answer you seek.
-
Would you like us to look through all your topics and replies to see what you were told previously?
-
What do $customers['birthdate'] and $_POST['birthdate'] contain when it says invalid? They are what the function is testing.
-
@Strider64 According to your code, 29.07.2022 and 07/29/2022 both give a value of true in $valid but you will end up writing an empty date value to the db if you use $date as neither comply with the expected $format.. Also. checkdate is an existing function. (PHP Fatal error: Cannot redeclare checkDate() ).
-
plus I can't see the current state of your function code which is a fairly important component, don't you think?. This getting to be too much like hard work.
-
What were you expecting as a reply to that statement? I have no way of knowing what exact code gave you that result and with what input value. I ain't psychic.
-
-
-
Getting var from database, then getting record based on it
Barand replied to Shredon's topic in PHP Coding Help
try $sql = "SELECT users_galaxyViewSystem FROM users WHERE users_id = 1"; $result = $conn->query($sql); $row = $result->fetch_assoc(); $testowanySystem = $row['users_galaxyViewSystem']; However, you should be using a single query with a JOIN instead of two queries $sql = "SELECT planets_name FROM planets p JOIN users u ON p.planets_starsystem = u.users_galaxyViewSystem WHERE planets_galaxy = $testowanaGalaktyka AND planets_planet = $testowanaPlaneta AND users_id = 1 "; $result = $conn->query($sql); foreach ($result as $row) { echo $row['planets_name'].'<br>'; } -
Then change the "1" to "true" if you're using strict variable typing (There is a manual!) Not what you said earlier ...
-
No - they are independent of one another. They can be be the same but do not have to be. your users may be typing 29/07/22 into a text input so your expected format is "d/m/y" but your output format for the db field would still be Y-m-d, requiring conversion of the input . My test code... <?php if ($_SERVER['REQUEST_METHOD']=='POST') { echo '<pre>' . print_r($_POST, 1) . '</pre>'; echo check_date($_POST['birthdate']) ? 'Date is valid' : 'Date is NOT valid'; } function check_date($input, $format='d/m/Y') { $date = DateTime::createFromFormat($format, $input); return ($date && $date->format($format) === $input); } ?> <form method='POST'> <input type='date' name='birthdate'> <input type='submit'> </form> With input format defined as "d/m/Y" (which you said "worked") my output is With $format='Y-m-d' I get
-
The input, as stated, displays d/m/Y but printing the posted data shows it is sent in Y-m-d format So the input format you are validating is Y-m-d
-
You asked if you should change it because you were outputting to the database in Y-m-d format. I told you that $format was defining the expected input format. You are using an INPUT type="date" therefore it will display the date according to your locale. However it will be sent in the POST data as Y-m-d, therefore your expected input format is "Y-m-d".
-
What does $customers['birthdate'] contain when you have a problem? I notice your location is London but your default input format in your function is US format.
-
DO I need foreach for accessing specific array data here?
Barand replied to Rascalsailor's topic in PHP Coding Help
Use echo '<pre>' . print_r($arr, 1) . '</pre>'; to give a readable formatted display of your array. Then follow the chain of keys... -
Instead of value='<?= htmlentities($post['JardinConseil'],ENT_QUOTES) ?? ''?>' ^ ^ try value="<?= htmlentities($post['JardinConseil'],ENT_QUOTES) ?? ''?>" ^ ^