Jump to content

benanamen

Members
  • Posts

    2,134
  • Joined

  • Last visited

  • Days Won

    42

Posts posted by benanamen

  1. Here's what I would suggest..

    1. Back up your DB and Code-base
    2. Clean up your orphans
    3. Add the Foreign Keys
    4. Learn and implement cascading update/delete
    5. Clean up the unnecessary code

    You have been doing it "your way" long enough that the difference and time/effort savings will be very apparent.

    In a nutshell, but some fresh eyes on it and just try it. There really isn't much more to say about it.

    * I am assuming your DB is already normalized. If not, now is the time to fix it.

  2. 2 hours ago, SLSCoder said:

    Can you give me a good reason to set the relationships up in the database? Maybe you can talk me into doing it.

    Sure, if you can tell me why the obsession with resetting the auto-increment.

    I will go first to save a post....

    #1 reason to use Foreign Keys is to enforce Data Integrity. You can try and manage it yourself but if you get it wrong you will end up with orphan records (Bad Data). Wouldn't surprise me at all if you already have orphans. As soon as you try to set the FK's you will know right away if you do. It also clearly defines the links between tables (That would be the R in RDMS, Relational, AKA Relationships)

    2 hours ago, SLSCoder said:

    I tried setting up foreign keys once, cascading delete, etc. What a mess.

    Then you didn't learn how to do it. It is not complicated and is a great feature, especially when multiple table record deletes are involved at the same time. The more tables involved, the more code needed and the more chance of something breaking. You also unnecessarily increase your technical debt.

    Another thing, if anyone, including your future self does a reverse engineer data model it is instantly clear how data is related to other data.

    It is not up to me to talk you into anything. I am not the one that has to work with your DB. There are right/better ways to do things and Foreign Keys in an RDMS is one of them.

  3. Navicat saves query's within itself as part of the program.

    Quote

    I do want those records deleted and I want to reset the AUTO_INCREMENT.

    Based on the additional info you provided, there is no reason whatsoever to reset the auto-increment. And as I already said, once you start creating a real relational DB with keys you wont be able to do it anyways.

    If you actually want to be able to call a "saved" query at will from the DB will you will need a stored procedure or run the saved query in Navicat which actually is a "saved" query.

  4. If you truncate ALL the tables in the DB you can use this...

     

    <?php
    
    $pdo = new PDO('mysql:host=localhost;dbname=db_example', 'root', '');
    
    $tables = $pdo->prepare('SHOW TABLES');
    $tables->execute();
    
    foreach($tables->fetchAll(PDO::FETCH_COLUMN) as $table)
    {
        $pdo->query('TRUNCATE TABLE `' . $table . '`')->execute();
    }

     

  5. 21 minutes ago, SLSCoder said:

    need to run it when records get deleted.

    Why?

    If you are deleting all the records you can truncate the table if there are no foreign keys. If you are just trying to keep a continuous id number for left over records, don't do that. It is a pointless noob thing and will fail when you start using foreign keys. I tried the same thing when I was new. If you are keeping some real data,  a better practice is to create a deleted flag column and just mark the data as deleted rather than actually wiping it from the DB.

     

    23 minutes ago, SLSCoder said:

    Why does that matter to you?

    We are here to teach people, not just answer an OP's attempted approach to a problem (See XY Problem). You don't know what you don't know that we most likely know and can tell you the better/correct way to solve the real problem. When posting it is best to tell us the real problem you are trying to solve rather than ask how to solve what you think is the way to solve the real problem.

  6. Before you start, make sure you understand Database Normalization. Look online for Real Estate Data Models to see examples of how it is being done. A GUI DB manager like MySQL Workbench will be very helpful. Stay away from Phpmyadmin. Sketch out a "Conceptual Data Model". Then move on to a Logical Data Model, Then a Physical Data Model. This site will help you understand all of that.

    https://erwin.com/blog/types-of-data-models-conceptual-logical-physical/
     

    Do not "code as you go". Make sure your DB architecture is well planned before you write a single line of code. When you have finalized your DB structure post it here and we will review it for you. When you get to coding, use PDO with prepared statements for you DB actions.
     

    • Like 1
  7. The whole point of the OP's thread is really, "How to debug" (troubleshoot). All of the answers provided are responses to the OP's attempted solution to debugging. (XY Problem)

    OP, make sure you have error reporting and logging turned on. Php is more than happy to tell you exactly what is wrong and what line and file the problem is. The Php and server error logs are your friend.

  8. 9 hours ago, jodunno said:

    I am actually working on my own character map. so Smythe-Jones will be Smythe[hyphen]Jones in my database. Thus, all non alphabetic characters submitted via post will be converted/mapped before application. 12.25 will become 12[point]25.

    Absolutely and completely pointless!

    If you are so convinced there is a problem, show us a case with code using current coding practices where it would actually be a problem if you didn't do this. If you are protecting against something, then surely you can show us the case that it protects against.

  9. Where ever you have been learning from, toss it. There is so much not right with your code there is no point getting into every single thing.

    A couple points though, md5 has been hacked a hundred years ago, don't use it. Not now, not ever. It is also not "encryption". It creates a very hackable hash. Use password_hash and password_verify. I would also highly recommend you use PDO Instead of mysqli. This tutorial will get you going. Your clean function is a junk relic from the 90's. There is no reason to be putting your POST elements into a session. You already have the values in the POST array, just use them. You are also using the same exact $username variable as a database connection parameter AND using it to query a database column.

    NEVER EVER put variables in a query. Use Prepared Statements.

  10. You have made a classic mistake of depending on the name of a button to be submitted for your code to work instead of checking the POST Request Method. Since you didn't name your submit button, the code does not do anything. The fix is NOT to add a name to the button, but to instead check the REQUEST METHOD instead.

    Depending on the name of a button to be submitted will completely fail in certain cases.
     

    if($_SERVER['REQUEST_METHOD'] == POST’){
    
    // Do stuff
    
    }

    Your error checks will also fail. You need to trim the entire POST array and then check for empty.

    Do not create variables for nothing.

    Your code is also vulnerable to an XSS Attack. You are allowing user supplied data directly in your form.

    • Great Answer 1
  11. 2 minutes ago, Aldayne_Henry said:

    1. What do you mean i should check the post method request?

    2. trim the post array?

    3. kill the script after header redirect?

    1.

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    //Process form
    }

    2. https://www.php.net/manual/en/function.trim.php

    3. https://www.php.net/manual/en/function.exit.php

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