Jump to content

seandisanti

Members
  • Posts

    60
  • Joined

  • Last visited

Posts posted by seandisanti

  1. Alrighty... let's step through this then. let's say $_POST['id'] contains array(1,2).

     

    $ids_to_be_deleted then contains '1ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name2'

     

    That's obviously garbage, but just for laughs lets keep on...

     

    $query now contains the string '"DELETE FROM tbl_main   WHERE (ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name) IN (1ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name2)"

     

    hopefully you can see how that would never, could never work as selection criteria for a delete or a select or anything else. If you're trying to delete a record or records that match an array of id's, just do it like this:

     

    $ids = implode(',',$_POST['id']);
    $query = "DELETE from tbl_main WHERE id IN(" . $ids . ")";
    
  2. Thanks but I don't fully understand what you wrote. I am a VERY beginner developer. Can you write the things in my code? Also if it is possible, with comments, so I can learn from it. Thank you very much :)

    No problem.

     

     

    1) you shouldn't have to keep listing $link in your mysqli_ calls

    means mysqli_query($link, $query) can and probably should be written as mysqli_query($query)

     

     

    2) Your if statements based on equality will always evaluate to true, because you're using an assignment operator = instead of comparison == since you can assign any value (or none) to $result, it will always evaluate the assignment as true.

    Try this:

    <?php
    $a = 'a';
    if ($a = 'b'){
        echo 'Should have used "=="';
    } else {
        echo 'This will never echo because you are evaluating an assignment instead of an equality';
    }
     
    

     

     

    3) Whenever you run into an issue where you're not getting data, or getting data you don't expect...

    When you're working with a database and get unexpected results (or none at all) your first troubleshooting step should be to verify your query after it's compiled.  There are several ways to do it, but I typically just die($sql); right after my query is assigned to variable $sql.  Then when i go to the page, it outputs the query as passed to the database.  If i can't spot the error (usually it's something silly like a missing space in the concatenation  like 'SELECT idFrom table') then I will copy the whole query and run it on its own in phpmyadmin, or mysql console directly.  If it runs successfully but returns no results or bad results then it's a logic issue.  If it errors out, then it's a syntax issue, and it will usually give you a clue where to look for it.

  3. 1) why create $ids_to_be_deleted if you're not using it?

    2) I really don't think your WHERE statement is valid, but even if it is, that is quite possibly the worst WHERE statement you could ever use. Think about it, you're giving it two sets of values, you're not comparing each member of each set with its relative, you're looking for the presence of each member of group a in group b, regardless of what field group b has the value for.  Imagining the situation with numeric values may make it make more sense.  say you've got 2 groups of numbers, (1,2,3,4) and (4,3,2,1) They're obviously different groups. but checking that each member of the sequence is in the group would select both of them. 

  4. each of your header() redirect statements need an exit statement after it to prevent the rest of the code on the page from running.

    No it doesn't. When you do a header redirect, anything after it basically doesn't exist. even if you put an exit() it would never be reached.

     

    Be sure if you're going to use an active flage to identify logged in users in the database, that you also check at random intervals to toggle the status of users that did not log out but are not actively on the page.  The easiest way to do that is probably with a time of the last time they loaded a page.  it would take just a second to add that to the database, and then on login, instead of checking just the active flag, you can see when they were last active, someone with an active flag for example that hasn't loaded any page but login in the last hour is probable safe to start a new session for. You can also use the actual session id's in the database, so that you delete the session file and essentially kill all $_SESSION variables for the previously active session. 

  5. 1) you shouldn't have to keep listing $link in your mysqli_ calls

    2) Your if statements based on equality will always evaluate to true, because you're using an assignment operator = instead of comparison == since you can assign any value (or none) to $result, it will always evaluate the assignment as true.

    3) Whenever you run into an issue where you're not getting data, or getting data you don't expect, output your query and take a look at it. In this case it would show you that you're quoting the table name, which is a no-no.  quotes are for literal strings only in SQL, your query right now is trying to pull fields from a literal string instead of the table with the name specified in the literal string.

  6. Yeah, either it's a variable and will be preceeded by '$' or it's a literal string that won't be confused for a keyword anyway. The only way you could have problems is if you decided you wanted to use a reserved word as the name of a constant AND you decided not to follow capitalization conventions, at which point you'll have made multiple intentional decisions to cause yourself problems and really shouldn't be surprised when you succeed (at failing)

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