Phi11W
Members-
Posts
163 -
Joined
-
Last visited
-
Days Won
12
Everything posted by Phi11W
-
Auto update Database Table value when time expires
Phi11W replied to Zinn's topic in PHP Coding Help
Barand's point is that you don't need the enabled field at all. Your application should allow submissions when the current date/time is between entrytime and closetime. At any other time, it should not allow submissions. Showing which items are available is a problem for the client web page - Javascript's much better at that sort of thing. (Of course, your server-side Application still needs to check those date/times!) Having databases try to update themselves in near-real time is a pretty difficult undertaking and assumes that your database is always, always available and working and properly. Take it from a 30+ year professional ... 100% Availability is a Myth. Regards, Phill W. -
Could it be as simple as json_decode( JSON_data_In_a_String_variable )? Regards, Phill W.
-
But if you validate and then redirect, then the page at the end of that redirection must repeat the validation, otherwise someone could send data to it directly, bypassing the validation. The basic pattern for my pages is something like this: if ( form data submitted ) { Validate form data - populate variables and error messages ; if ( form data valid ) perform any required Action ; } Display Form, with values and/or error messages and/or results from the Action. Any "validation" that you do in Javascript on the client is for the Users' convenience only - you must not rely upon it because nothing that comes from the client can be trusted. (For example, do you validate the form value submitted from the HTML "select" list that you sent? You probably should ...) Regards, Phill W.
-
Remember that you're building a PHP String that just happens to contain some text (SQL) that means something to your database. You have to build tat string according to PHP rules: $query=$bdd->prepare('SELECT t2.ui_company, t1.* FROM a2billing.nwc_refill_users t1 LEFT JOIN a2billing.nwc_anagrafica t2 ON t1.refag_richiedente = t2.ui_login WHERE t1.refag_paga_a=\'\' AND t1.refag_importo > 0 AND t1.refag_dataora_validazione<>\'\' AND (DATE(t1.refag_dataora)>=\'2022-01-01\' AND DATE(t1.refag_dataora)<=\'2022-05-15\')'); You might also consider using Parameters in place of the literals. A tiny bit more code but you'll avoid headaches like this. Regards, Phill W.
-
How do i query these lines in one single line someone help please??
Phi11W replied to Ronel's topic in PHP Coding Help
Well, yes it does, because that's what's in your data! What you're missing is the action that caused each row to appear in your results. Include the action column and your data should make a little more sense. What are you trying to achieve? Calculation of total stock levels based on actions against each item? For that you'd want something like this: select id , sum( case action when 'Added Qty' then poqty when 'Stock Received' then received_qty when 'Outgoing Record Recorded' then - outgoing_qty /* negate value to deduct from total */ end ) qty from table1 group by id order by action ; Bear in mind that ordering by a text field (whose values might change over time) could give you headaches. It might be better to codify these values (into a "Lookup" Table) so that you can sequence them reliably. Regards, Phill W. -
In almost every DBMS by default, and MySQL if you configure it properly, this query will be flatly rejected as an error. What values of ponum and status would you expect to be returned when you are only grouping by itemname? The fields that you select either must be included in the "group by" clause or must be used in aggregate functions, like SUM. Suppose you had a number of purchase orders - what output would you expect to see for these "extra" fields? +-------+------------+------------+-----+ | ponum | status | itemname | qty | +-------+------------+------------+-----+ | 111 | Complete | Keyboard | 1 | | 222 | Complete | Keyboard | 2 | | 222 | Complete | Mouse | 1 | | 333 | InProgress | Keyboard | 1 | | 333 | InProgress | Mouse | 1 | | 333 | InProgress | Chair | 1 | +-------+------------+------------+-----+ select itemname, ponum, status, sum(qty) from ... group by itemname ; +----------+-------+------------+----------+ | itemname | ponum | status | sum(qty) | +----------+-------+------------+----------+ | Chair | 333 | InProgress | 1 | | Keyboard | ??? | ??? | ??? | | Mouse | ??? | ??? | ??? | +----------+-------+------------+----------+ If you can't tell the query which value you want within the grouping, the query should give up and throw an error. MySQL is one of the few DBMS that does not do this by default. Regards, Phill W.
-
How can I put a condition with this studentno and date at the same ?
Phi11W replied to camille's topic in PHP Coding Help
I'm guessing your problem is with this query: You can't "invent" SQL syntax. You have to use what your DBMS supplies to you. The keyword to group two conditions logically and require both to be TRUE is "AND". Whilst the "&&" operator does the same thing in PHP, C# and other languages, MySQL only understands "AND". This bit might cause you problems as well: IIRC, the MySQL Now() function returns the time as well as the date. If you're only storing the date, then the two will never match, as in: '2017-05-28' != '2017-05-28 00:00:00' You'll probably have to do some truncation on the returned value (say, using the DATE() function). Also, read up about Prepared Statements, to protect yourself against SQL Injection Attacks. Obligatory XKCD Reference - Little Bobby Tables. Regards, Phill W. -
Obligatory XKCD Reference: Standards Simply, you want to arithmetic on the values. Addition, subtraction, multiplication of numeric values that represent monetary amounts in different currencies. If those values are "buried" inside String values inside a single database field, then you have to waste processing time pulling those values apart so that you can "get at" the numeric part and then do your arithmetic on that. Databases are really, really good a finding bits of data and putting them together. They are [all] generally rubbish at finding big chunks of data and pulling them apart again. This is a case where the storage representation of these values (two fields, one numeric value, one character currency code) is different from the way that you or I might choose to think about them. That representation - the one that we would use - should be delivered by the Application, interpreting what's stored in the database into what we are used to seeing (and reinterpreting values going the other way, from what we use into the database). Regards, Phill W.
-
(Being a Character Representations of their actual Data Type) Datetime literals must be enclosed in single quotes, in exactly the same way as you do for Character literals. The error message is definitive. Error: INSERT INTO weather_data ... VALUES ( 2022-01-24T15:40:00.000Z, 47.7, 47.7, 30.048, 29.224, 45.49, 92, 314, 1.3, 2.2, 8.1, 0, 0.161, 0.161, 0.681, 49.024, 2022-01-24T15:34:00.000Z, 29.48, 1 ) \_________/ You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':40:00.000Z, 47.7, 47.7, 30.048, 29.224, 45.49, 92, 314, ' at line 6 That said, you should not be embedding variables directly into SQL - you are leaving yourself wide open to SQL Injection Attacked. Use a Parameterised Query (Prepared Statement) instead. Obligatory XKCD Reference - Little Bobby Tables. Regards. Phill W.
-
Any non-Indian Users that favour keyboard navigation are still going to be very frustrated by this design. As soon as they try to move "down" the list with the arrow keys, the Javascript code will submit the page, including the first option ("country=india"). This reloads the page and, sadly, your code doesn't cater for the list "remembering" which option was chosen previously and just resets itself to the first, "--select--" option. Pressing the down key again just goes round the same loop over and over again (and hits your PHP backend each and every time). Do you really need such real-time handling of a change in country? Regards, Phill W.
-
Ignoring the dodgy HTML syntax for a moment, remember that only the value of the selected option gets submitted in the HTTP form data, not the [inner text] description. With this in mind, how is your PHP code supposed to tell the difference between the 2nd, 3rd and 4th options (all of which will effectively submit "modSelected=$PostId")? Also, submitting a form on each change of the DropDownlist is likely to lose you major points on the "Accessibility" scale. People will have a very hard time trying to navigate your form using the keyboard. Regards, Phill W.
-
Why reinvent a perfectly good Wheel? Compound data structures are already rather well catered for by [things like] JSON: $jsonText = '{"foo":["home","food"],"brand":"tulasi"}'; $json = json_decode( $jsonText ); echo count( $json->foo ); // 2 echo $json->brand; // 'tulasi' var_dump( $json ); //object(stdClass)#1 (2) //{ // ["foo"]=> array(2) // { // [0]=> string(4) "home" // [1]=> string(4) "food" // } // ["brand"]=> string(6) "tulasi" //} The problem with your code is that the comma character (",") is being used to separate both the top-level items (foo and brand) and the individual values of foo! The explode() function cannot tell the difference between these two usages of the comma character. Regards, Phill W.
-
You can work with hyphenated attribute names, but you have to wrap them up a bit more: $var2=$dolar->{'class-variacion'};
-
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\AplikasiInventaris\function.php on line 186 Fatal error: Uncaught TypeError: Unsupported operand types: int + string in C:\xampp\htdocs\AplikasiInventaris\function.php:186 St
Phi11W replied to sandi123's topic in PHP Coding Help
The POST'ed values are always Strings. $stok_awal is whatever numeric value comes back in the qty field (we have to assume that this is numeric because, coming from the database, it really ought to be) or zero, if the retrieved value was NULL. $qty is still the String value from the POST'ed data values. [int] + [string] => Error. First validate that the POST'ed value really is [the String representation of] a numeric value and then convert that value to the correct Data Type before using it. Regards, Phill W. -
Trying to use php rand() function (newbie)
Phi11W replied to DavidAbineri's topic in PHP Coding Help
Did you try following the link under "The XY Problem"?? It's asking a very [very] specific question about a solution you're trying to get working when the "correct" answer involves taking a different path altogether, for example: "X" Question: "How can I stop llamas from chewing the cushions on my sofa." (requesting a solution to a very specific, small-scale issue.) "Y" Question: "How do I stop llamas getting into my house." (asking a far broader question, the solving of which would completely eliminate the need for the original solution). Regards, Phill W. -
And that would be because brackets matter. This takes the value of the variable $id and binds it to the parameter named :id. This creates a single-element array whose only element is the value of the variable $id and tries to bind that to the parameter named :id. I'm guessing that either PHP doesn't know how to bind arrays to parameters or it's trying to do so, getting an error and then not showing that error to you (i.e. failing silently, which is almost always a Bad Thing). Do you have error reporting enabled? Something like this: ini_set( 'display_errors', 1 ); ini_set( 'display_startup_errors', 1 ); error_reporting( E_ALL ); Regards, Phill W.
-
Whilst this may be an assignment that you have been given, the principle is fundamentally wrong. The unique identifier (Primary Key) of a table should never change, from the moment the record is created, right up to the moment that the record is finally destroyed. People change their names. These two aims are incompatible, which is why so many database tables have a "surrogate", numeric identifier, the values of which just goes up and up [and up] forever. Furthermore, using Personal Data (i.e. part of a name) to create a [visible] identifier is distinctly questionable in these days of GDPR and similar legislation. Please use the "Code" button ("<>") when submitting code for others to look at - your image is very difficult to read. Your code manipulating ':email', ':lastname', etc. (line 30) is wrong. You should be using the POST'ed values here, not string literals that just happen to contain the names of the parameters. // This ... $email = ':email'; // ... should be ... $email = $_POST['email']; Regards, Phill W.
-
How to sort Hidden -> A-Z in Top and Show -> Z-A in Bottom
Phi11W replied to Ramcult's topic in PHP Coding Help
Seriously? Did you even look at the Manual Page that requinix pointed you to? Example #2, down in the comments, gives a worked example of how to define a function that compares two elements of an array, and then invoke usort against an input array and that named function. How to use it, explained with code. As requested. Regards, Phill W. -
Php set a Checkbox as checked at start from page
Phi11W replied to Foracc's topic in PHP Coding Help
Or even something like this: $Datei = file('test.txt')[2]; printf( '<input type="checkbox" name="Checkbox" %s value="Y">Checkbox</input>' , ( ( "Line 2" == Datei) ? 'checked' : '' ) ); "Danger, Will Robinson!" Note my change to your "equality test" involving Datei: your original code would have "misbehaved" in interesting ways being, as it was originally given, an assignment! Regards, Phill W. -
Just before closing the book on this one, please ask yourself this: In [another] four months time, are you going to look at this code and ask yourself "What the H*** does this do?" You will spend far more time reading code than writing it (accepted industry stats estimate 80% reading, 20% writing). Always favour Clarity and Correctness over Conciseness or Cleverness. Regards, Phill W.
-
How about something like this? 'title' => sprintf( '%s %s', ucfirst( $page['content_slug'] ), ucfirst( $page['content_title'] ) ), Regards, Phill W.
-
Why do feel the you want to do this? Whatever the reason, it's almost certainly misguided. As Barand said, the unique identifier for each record should be generated when that record is created and should then persist, unchanged, until that record (and all the other records related to it) are finally and forever deleted. Can you imagine the chaos if your bank renumbered all of its accounts every time someone else closed their account? Regards, Phill W.
-
I am getting an error while creating the link.
Phi11W replied to maviyazilim's topic in PHP Coding Help
Remember that PHP does not write HTML. It writes strings that just happen to contain some words that your web browser can do something "clever" with. You are creating a string literal, and you've chosen to use the single-quote to wrap around it. So far, so good. But your literal contains single quotes as well, and those are confusing PHP. You either need to escape your embedded single quotes or, because HTML doesn't care which you use, use double-quotes in the HTML instead: // Either (using double-quotes) echo '<a href="icerik.php?=icerik' . $goster['icerik_id'] . '">' . $goster['baslik'] . '</a>'; // or (with escaped single-quotes) echo '<a href=\'icerik.php?=icerik' . $goster['icerik_id'] . '\'>' . $goster['baslik'] . '</a>'; // or, my personal favourite printf( '<a href=\'icerik.php?=icerik%s\'>%s</a>', $goster['icerik_id'], $goster['baslik'] ); Regards, Phill W. -
I assume you are not using "sort" in it's technical sense here. There is no sorting required in any part of this, which looks like a Homework Assignment, to me (so you don't get the answer straight away! 😀). As an aside, I would say that this function should take three parameters, not two. 1. The array itself, 2. the lower limit of values you want to look for, 3. The upper limit of values you want to look for. Anyway ... Within the function, you'll need a local variable in which to store the calculated total. Remember to start this off at zero. Then, loop through the elements of the array and compare each element to the lower and upper limits (parameters). If the element value is greater than or equal to the lower limit and less than or equal to the upper limit, then add the element value to the total. After the loop, return the total. Regards, Phill W.
-
PHP CSV Upload - Some rows in CSV file aren't in database
Phi11W replied to KN1V3S's topic in PHP Coding Help
Pull the data back out of the database and copy it into your favourite spreadsheet program (e.g. MS Excel). Then do the same with the data from the original file. Put the two lists side by side on a single worksheet, sort the two lists and compare visually. It shouldn't take more than a few minutes to find the ones that appear in one list and not the other. Regards, Phill W.