-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
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) ?? ''?>" ^ ^
-
DO I need foreach for accessing specific array data here?
Barand replied to Rascalsailor's topic in PHP Coding Help
Of course you can. foreach() is for iterating through all elements in an array. In this case you don't want to do that. $arr = json_decode($jsn, 1); $elem = $arr['identifiers'][0]['device_ids']; echo $elem['application_ids']['application_id'] . '<br>'; echo $elem['dev_eui'] . '<br>'; echo $elem['device_id'] . '<br>'; However, are you certain that the "identifiers" array will only have a single element? If the number is unknown, use foreach()... $arr = json_decode($jsn, 1); foreach ( $arr['identifiers'] as $ids ) { $elem = $ids['device_ids']; echo $elem['application_ids']['application_id'] . '<br>'; echo $elem['dev_eui'] . '<br>'; echo $elem['device_id'] . '<br>'; } -
+ "the matching row of data never existed" Not getting a matching row is not a database error - in fact it may often be the desired result.
-
Selecting from duplicate values but wanting the max
Barand replied to mongoose00318's topic in MySQL Help
mysql> select * from production_data_archive; +----+----------+------------+-----------+---------------------+ | id | order_id | job_number | line_item | insert_time | +----+----------+------------+-----------+---------------------+ | 1 | 16824 | 22000412 | A | 2021-03-26 00:00:00 | | 2 | 16824 | 22000412 | A | 2021-03-30 00:00:00 | | 3 | 16824 | 22000412 | A | 2021-04-09 00:00:00 | | 4 | 16825 | 22000412 | B | 2021-03-26 00:00:00 | | 5 | 16825 | 22000412 | B | 2021-03-29 00:00:00 | | 6 | 16825 | 22000412 | B | 2021-04-06 00:00:00 | | 7 | 16825 | 22000413 | C | 2021-03-26 00:00:00 | | 8 | 16825 | 22000413 | C | 2021-03-29 00:00:00 | | 9 | 16825 | 22000413 | C | 2021-05-06 00:00:00 | | 10 | 16825 | 22000414 | C | 2021-03-26 00:00:00 | | 11 | 16825 | 22000414 | C | 2021-03-29 00:00:00 | | 12 | 16825 | 22000414 | C | 2021-05-06 00:00:00 | +----+----------+------------+-----------+---------------------+ 12 rows in set (0.00 sec) mysql> SELECT a.id -> , a.order_id -> , a.job_number -> , a.line_item -> , a.insert_time -> FROM production_data_archive a -> JOIN ( -> SELECT job_number -> FROM production_data_archive -> GROUP BY job_number -> HAVING count(distinct line_item) = 1 -> ) nodupe USING (job_number) -> LEFT JOIN -> production_data_archive b -> ON a.job_number = b.job_number -> AND a.line_item = b.line_item -> AND a.insert_time < b.insert_time -> WHERE b.job_number IS NULL; +----+----------+------------+-----------+---------------------+ | id | order_id | job_number | line_item | insert_time | +----+----------+------------+-----------+---------------------+ | 9 | 16825 | 22000413 | C | 2021-05-06 00:00:00 | | 12 | 16825 | 22000414 | C | 2021-05-06 00:00:00 | +----+----------+------------+-----------+---------------------+ 2 rows in set (0.03 sec) -
Unable to copy table data from one database to another
Barand replied to wongle's topic in PHP Coding Help
If the two tables have the same structure REPLACE INTO db2.tablename SELECT * FROM db1.tablename WHERE id = 223; -
Unable to copy table data from one database to another
Barand replied to wongle's topic in PHP Coding Help
I'd take out the try..catch stuff and let php report any errors with a meaningful message. I can't see the problem off-hand but it does seem evident that the term "data normalization" is foreign to you. That is a spreadsheet, not a relational db table. -
how to validate a date based on user input from a form
Barand replied to webdeveloper123's topic in PHP Coding Help
No. It is the expected input format that is being defined here. Consider changing the function thus function check_date($input, $format='m/d/Y') { $date = DateTime::createFromFormat($format, $input); return ($date && $date->format($format) === $input) ? $date->format('Y-m-d') : false; } It then returns "false" if the date is invalid but returns the date in Y-m-d format if valid. -
What would you want this function to do? Why "different forms"?
-
Here's an alternative using PHP to do to the date arithmetic <?php // create pdo connection here $date = $_GET['date'] ?? date('Y-m-d'); // date defaults to today if no input $days = $_GET['days'] ?? 0; // days defaults to 0 $dt = new DateTime($date); $newdate = $dt->modify("$days days")->format('Y-m-d'); $disabled = $newdate == date('Y-m-d') ? 'disabled' : ''; // if today, disable tomorrow button $res = $pdo->prepare("SELECT * FROM cercles WHERE dates = ? "); $res->execute([$newdate]); ?> <!doctype html> <html> <head> <title>Sample</title> <style type="text/css"> </style> </head> <body> <form> <button name='days' value='-1'><</button> <input type='date' name='date' value='<?=$newdate?>' > <button name='days' value='1' <?=$disabled?> >></button> </form> <table> <?php // output your data rows here ?> </table> </body> </html>
-
how to validate a date based on user input from a form
Barand replied to webdeveloper123's topic in PHP Coding Help
Adapt @kicken's code function check_date($input, $format='m/d/Y') { $date = DateTime::createFromFormat($format, $input); return ($date && $date->format($format) === $input); } if (!check_date($input) ) { $error['date'] = 'Invalid date'; } -
That is your main problem - or it will be. Database dates in that format are totally useless. Always store dates in Y-m-d format. Such dates can be sorted/compared and can be used by the inbuilt datetime function. To get today's data... SELECT * FROM cercles WHERE dates = curdate() To get yesterday's... SELECT * FROM cercles WHERE dates = curdate() - interval 1 day The above will not work with your d/m/Y dates.
-
I suspect (as I implied above) that it's because it is repeatedly receiving the same query. With the time condition it changes.
-
try adding a creation timestamp to the instruction records then query select created_time, instruction ... where created_time > :last_time_retrieved You then have a different query each time which may prevent the caching effect. It also means you won't have to delete records enabling you to maintain a history of instructions.
-
How can I anchor to a <h1 id="title1"> tag of external url?
Barand replied to eaglehopes's topic in HTML Help
If you remove "page=", does it work? -
Are you loading it your browser with a URL in the address bar? eg http://localhost/myfile.php or are you just opening the file.
-
How can I anchor to a <h1 id="title1"> tag of external url?
Barand replied to eaglehopes's topic in HTML Help
How much success? Are you getting to the page but not the heading, or are you not even getting to the page? -
How to sum columns that is in SQL, to table that use php
Barand replied to hendrikbez's topic in PHP Coding Help
Better show your code echo 1.15 + 0.03 + 0.04 + 0.01 + 0.02; gives me 1.25 mysql> select * from a_sample; +------+------+------+------+------+ | fee1 | fee2 | fee3 | fee4 | fee5 | +------+------+------+------+------+ | 1.15 | 0.03 | 0.04 | 0.01 | 0.02 | +------+------+------+------+------+ 1 row in set (0.00 sec) mysql> select fee1 -> , fee2 -> , fee3 -> , fee4 -> , fee5 -> , fee1 + fee2 + fee3 + fee4 + fee5 as total -> from a_sample; +------+------+------+------+------+-------+ | fee1 | fee2 | fee3 | fee4 | fee5 | total | +------+------+------+------+------+-------+ | 1.15 | 0.03 | 0.04 | 0.01 | 0.02 | 1.25 | +------+------+------+------+------+-------+ 1 row in set (0.03 sec) -
How to sum columns that is in SQL, to table that use php
Barand replied to hendrikbez's topic in PHP Coding Help
SUM won't work with a dog's breakfast of a table like that one - it needs normalizing. if the table were to be normalized to table : fee +--------+-----------+---------+----------+ | id (PK)| nmc_id(FK)| seq | amount | +--------+-----------+---------+----------+ | 1 | 1 | 1 | 100 | | 2 | 1 | 2 | 75 | | 3 | 1 | 3 | 150 | | 4 | 1 | 4 | 225 | | 5 | 1 | 5 | 50 | | 6 | 2 | 1 | 125 | | 7 | 2 | 2 | 85 | | 8 | 2 | 3 | 350 | | 9 | 2 | 4 | 215 | | 10 | 2 | 5 | 125 | +--------+-----------+---------+----------+ with 5 fee records for each parent nmc record then you can use SUM() SELECT nmc_id , SUM(amount) as fees FROM fees GROUP BY nmc_id -
Simple example <?php if ($_SERVER['REQUEST_METHOD']=='POST') { $errors = []; $post = array_map('trim', $_POST); if ( strlen($post['name']) < 3 || strlen($post['name']) > 18 ) { $errors['name'] = 'Must be between 3 and 18 characters'; } if (!isset($post['confirm'])) { $errors['confirm'] = 'Box must be checked'; } if (!$errors) { // update database header("Refresh: 0"); exit; } } ?> <!doctype html> <html> <head> <title>Sample</title> <style type="text/css"> .errormsg { color : red; } </style> </head> <body> <form method='post'> Name <input type='text' name='name' value='<?=$_POST['name'] ?? ''?>' > <br><span class='errormsg'><?=$errors['name'] ?? ''?></span> <br> <input type='checkbox' name='confirm' value='1'> Click this box <br><span class='errormsg'><?=$errors['confirm'] ?? ''?></span> <br> <input type='submit'> </form> </body> </html>