Jump to content

NigelRel3

Members
  • Posts

    69
  • Joined

  • Last visited

Posts posted by NigelRel3

  1. I agree - it could be an indication that something is wrong - but data can be sourced from all sorts of places, sometimes it is a case of just having to deal with it.  It's especially the case when the data is from a third party and there may be nothing you can do with the source data, you massage it to fit into what you need.  

    Working with data loads can provide a great insight into what people try and do and your left with the impression that relying on people to do anything correctly and consistently is like trying to juggle water.

  2. Your check if the shirt number already exists is checking that a player_id already exists - nothing to do with the shirt number (or team)!

    I'm not sure if your database structure is correct though - if a player is just part of one team - then put the team number on the player record .  If you want players to be in multiple teams - then put shirt number of the other record.

    Then create a unique key on team, shirt number.

  3. You would have to identify just the rows that have changed and update them, so it means finding the players that have been changed and using the match id and old player id to update that specific row to have the new player id.

  4. You will have to rename one of the fields as having 2 fields called id isn't that helpful.

    As for how to compare two values passed into a script - it would be better for you to attempt something and post where you are struggling rather than just ask 'how can I achieve that'!  This is something fairly trivial and should be quick to work out from almost any example using parameters.

  5. If I were to do as you are trying to do, I would pass the object in rather than have to add another stage of fetching the data from the object to then pass into the table layer.

    One thing which you should certainly change is that you should use bind variables for your PDO statements!  Do not just dump data directly into any SQL statement.

  6. On the subject of how to catch/manage errors (exceptions or any combination) - is there a common way in which a PHP applications can trap, log errors and then present the user with a 'Something went wrong' screen, or is this down to individual implementations?  

    I can see the problem here is that this relies on all of the potential areas for errors being complete prior to building any screen output - or does it? Is this something a framework would have built in (am starting to look into Lavravel).

     

    (Sorry for asking so many questions as I'm trying to find out as much as possible from people who actually do these things)

  7. Hmmm... I've changed it to

    print_r($db->errno);
    $msg = "SQL failed -".$db->error;
    

    And that now gives me 0 as the errorno (even though in the Variable panel I can see a value of 1146 at the point of executing that statement.  Once I've stepped over the print_r line, the values are reset.

     

    :happy-04: Think I may have found the culprit - the very helpful debugging.  If I run the code without debugging, it works as expected, through debugging must be doing as you've said and causing some side effects by getting statuses or something.

     

    I use Eclipse and XDebug which although is very useful at times, I must remember that database objects may be affected!

  8. It's early and I'm probably just missing something obvious, but it's confusing me...

    	if (! $stmt = $db->prepare($sql)) {
    		print_r($db);
    		//echo "Error=". $db->error."<br />";
    		//$msg = "SQL failed -".$db->error;
    		print_r($db);
    	}
    
    

    The above code is intended to trap a failure in a prepare and give me something meaningful out of it.  BUT the output (abbreviated) I get from this is...

    mysqli Object ( ... [connect_errno] => 0 [connect_error] => [errno] => 1146 [error] => Table 'warehouse.BinType1' doesn't exist [error_list] => Array ( ) ...
    mysqli Object ( ... [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) ...
    

    So even simply doing a print_r on the $db object is causing the error to be cleared.

    Even stepping through on debug shows that when I arrive at the (commented out) echo statement or the second print_r - the values have been cleared.

    The $db object is from a straight forward new mysqli() and it works for most things I've tried so far.

    So I thought I'd ask for some enlightenment and in the meanwhile I'll fix the SQL causing the error :-/

    Thanks

  9. Firstly - you should be using either mysqli or PDO - mysql_query is outdated and should be your first thing to remove.

     

    The main thing about SQL injection is to not directly put the user entered string into a SQL statement, both mysqli and PDO support bind variables.  This allow the statement to have a place holder and effectively the value is linked to the statement in such a way as to stop SQL injection attacks.

  10. This is not a way to do it.  I'm not sure of exactly what your reasons for doing what you are doing.

     

    The problem is that in your statement you are going to try and update every record for a particular matchid to (for example) have a playerid of 1.  What you probably want to do is just have a list of player ID's and want to make sure that your set of records match.  Update isn't the way to do it.  It would probably be better and a lot easier to delete the existing records and insert new records.

  11. So you only have two values printed from ...

    printf ("%s(%s)\n",$row["ProductCode"],$row["L1"],$row["L2"],$row["L3"],$row["L4"],$row["L5"],$row["L6"]);
    

    the %s indicates that the value of a string should be included (from the parameters), so have you tried adding more %s's to print out all of the values?

     

    If in doubt - use something like print_r() which outputs a variable with all of it's components in a readable format.

  12. Firstly - this line

    $sql = $this->db->prepare("UPDATE items SET stack=:f AND dod=:g AND position=:h WHERE ID=:e");
    

    the syntax is incorrect, if you are updating several values - then you use a comma between items - not 'and'.

     

    As Barand was asking - what is the table structure?  Can you show the list of columns and what each one is defined as? What you show in your last post show one column and this seems to be different to what your using ('stock' instead of 'stack').

     

    Lastly - BUT most importantly - you don't at any point check that your statements have prepared/executed correctly - you simply assume that everything has worked OK and carry on.  You should assume nothing - check at each stage if what you've just done on the database has worked!  At some point this is going to come back and produce more problems.

  13. It looks as thought the user that is running the php process (not sure who this is on Windows) doesn't have read access to these files.

    Are these files under your webserver root directory (where your PHP scripts are)? (You can rung phpinfo() to check this)  Are the permissions similar to the .php files your running?

  14. if you have any type of account (bank, credit, loan, itunes, cell phone data plan, in game purchases, ...) that keeps track of an amount (quantity or money) or have ever done any in person or on-line shopping, you have seen this being used. it's not overkill, it's a necessity that's used everywhere in real situations. it gives you a record/audit trail of each transaction, so that you know all the who, what, when, where, and why information about each transaction. this lets you detect and correct errors, produce reports,... this method also has the advantage of correctly working with multiple concurrent users, since the changes made by each user will be recorded separately (at least the OP removed a race condition, by eliminating a separate SELECT query to get the 'current' starting amount.)

     

    without doing this, by just maintaining the amount in a field, there's no record of any duplicate data submissions, incorrectly entered values (transposed digits, hitting the wrong key), programming errors, fraud/cheating, ... the first sign of a problem is when someone tries to use an amount, that the system says exists, but it doesn't.

     

    if the OP is really doing an inventory system, that's more than just a classroom assignment or part of a beginner's attempt at making a game, the current method will eventually get out of sync with the actual products and you won't know which of the possible reasons why because there's no record being kept of what values have affected the total.

    I can understand the financial reasons for doing this (and I did say that in some systems it may be overkill), but I've worked with systems where you have tens of thousands of products, some with histories going back for several years.  (In this system) There is an inherent assumption that there will be mistakes in values and as stock goes missing/gets damaged it gets recorded and the overall stock figures are manually checked on a regular basis during stock takes when the correct figures are recorded.

    (Just wondering of the overhead it would cause if for various reports to use the audit trail as opposed to the main stock records.  Actually wonder if the full audit trail is still available - hmmm.)

    I guess that any design has to weigh the accuracy and 'currentness' of any data against the cost of maintaining it and the chance that the figure isn't correct anyway.  It's probably easier when dealing with transactions that only have a logical existence rather than physical objects which have a lot of external factors in play.

    Thanks for that anyway - makes me think about the design considerations for future systems.

    • Like 1
  15. next, you shouldn't add/subtract values to maintain a quantity. your current method has no way of detecting/preventing multiple form submissions from incorrectly altering the data. you can end up with the wrong quantity values and you won't even know if this has occurred. the correct way of maintaining a quantity (or any other 'account' total) is to insert a new record for each transaction that adjusts the value, inset a positive value to add to the quantity and negative value to subtract from the quantity. you would then simply SUM() the quantity for any id value (use GROUP BY id) to get the current quantity.

    Interesting approach - something which I've not seen before, I can see where some systems this is vital - but others is it a bit of overkill?  Out of interest - do you have any idea at what point performance becomes an issue?  Just that with SQL usually being the slowest part of most systems, I can imagine that over time or with high volumes of transactions, this may become an issue.

  16. As already mentioned - using multiple statements in one string will not work.

    You could rewrite your query though to be something like 

    UPDATE stock s1, stock s2
       set s1.qty = s1.qty-1, s2.qty = s2.qty +1
       where s1.id= 1 and s2.id= 1
    

    you'll have to change the names as appropriate - and BIND the variables for the ID's.

×
×
  • 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.