SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast tommy168's Avatar
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to delete a specific entry

    Hey guys

    I have trouble picking the specific id of an entry i want to delete

    so please take a look at the codes first

    Here's the function for making the form:

    PHP Code:
    function am_query_display_quick($page,$order ='DATE DESC'){
    global 
    $email;
    $conn db_connect_2();
    $color1 "tableRowOdd";
    $color2 "tableRowEven";
    $RowCount 0;

    $result $conn->query("SELECT pro,pro_update,ana,ana_update,cell,cell_update,cellother,cellother_update,gen,gen_update,genother,genother_update,author,author_update,other,other_update,id
    FROM mailing_list
    WHERE email = '
    $email'
    ORDER BY 
    $order LIMIT $start_row$max_per_page;");

    echo 
    "<form method = \"post\" action=\".{$_SERVER['PHP_SELF']}.\">";
    echo 
    "<table class=\"sortable\" id=\"query_quick2\" width=\"100%\" >\r\n";
    echo 
    "\t<tr><th></th><th>Promoter
    Locus</th><th>Update?</th><th>Anatomical Area</th><th>Update?</th><th>Cell
    Type</th><th>Update?</th><th>Other Cell Type</th><th>Update?</th><th>Genetic
    Background</th><th>Update?</th><th>Other Gen.
    Back.</th><th>Update?</th><th>Author</th><th>Update?</th><th>Other</th><th>Update?</th><th></th>\r\n"
    ;

    if(
    $result->num_rows){
    while (
    $row $result->fetch_array()){
    $RowCount ++;
    $row_color = ($RowCount 2) ? $color1 $color2;

    echo 
    "\t<tr class=\"$row_color\" ><td><input type=\"submit\" name=\"edit_mail\" value = \"Edit\"/></td>
    <td>
    {$row['pro']}</td><td>{$row['pro_update']}</td><td>{$row['ana']}</td>
    <td>
    {$row['ana_update']}</td><td>{$row['cell']}</td><td>{$row['cell_update']}</td><td>{$row['cellother']}</td>
    <td>
    {$row['cellother_update']}</td><td>{$row['gen']}</td><td>{$row['gen_update']}</td><td>{$row['genother']}</td>
    <td>
    {$row['genother_update']}</td><td>{$row['author']}</td><td>{$row['author_update']}</td><td>{$row['other']}</td><td>{$row['other_update']}</td>
    <td><input type=\"submit\" name=\"delete_mail\" value =\"Delete\"/></td>
    <td><input type =\"hidden\" name = \"id\" value=\"
    {$row['id']}\"/></td>
    </tr>"
    ;


    }
    }

    echo 
    "</table>";
    echo 
    "</form>"

    And this is where the delete command comes into place (excerpt):

    PHP Code:
    } elseif(isset($_SESSION['user_id']) AND isset($_POST['delete_mail'])){
    //user is deleting existing queries
    $conn=db_connect_2();
    $id mysqli_real_escape_string($conn$_POST['id']);
    echo 
    $id;

    $sql2 "DELETE FROM mailing_list WHERE id='$id'";
    $result mysqli_query($conn$sql2) or mysqli_error($conn);

    $msgs[] = "Query deleted successfully.";
    $body "account.php";


    yes it can delete entries all right

    but it doesn't delete a specific entry when i click the "delete" button near it

    somehow, it ALWAYS deletes the last row of entry at the table

    as you can see, i echoed out $id as it's referring to the LAST entry as well

    So my question is, how to delete a particular entry when i click the corresponding "delete" button??

    Thanks

  2. #2
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the value of:

    <input type =\"hidden\" name = \"id\" value=\"{$row['id']}\"/>
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  3. #3
    SitePoint Enthusiast tommy168's Avatar
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the value of:

    <input type =\"hidden\" name = \"id\" value=\"{$row['id']}\"/>
    'id' column of the table 'mailing_list'

    it turns out that the entry's id in LAST row of this table is selected every single time

    how to fix it up so i can choose to delete only the specific id ?

  4. #4
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From the code it seems that you will have multiple entries in the form?

    So there is not just one of these: <input type=hidden name=id value={$row['id']} />

    Is that correct?

    You should have a form element for each row returned and echo'd, otherwise it won't know what $_POST["id"] to use..
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  5. #5
    SitePoint Enthusiast tommy168's Avatar
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes indeed there are multiple entries in the form

    so do i create another form element in the above function just for id?

    and is the script that has the delete command sufficient or more needs to be added to correspond to the changes in the function?

    it will be great if you could elaborate on that thanks

  6. #6
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A form can have as many submit buttons as you like, but they all do the same thing, they just submit the same form with the same elements, so you should rather have a form around each row of items, that way when you click submit or delete rather, it will process that specific item. Otherwise it will just use the last form name as the only one.

    e.g.

    function am_query_display_quick($page,$order ='DATE DESC'){
    global
    $email;
    $conn = db_connect_2();
    $color1 = "tableRowOdd";
    $color2 = "tableRowEven";
    $RowCount = 0;

    $result = $conn->query("SELECT pro,pro_update,ana,ana_update,cell,cell_update,cellother,cellother_update,gen,gen_update,genother,genother_update,author,author_update,other,other_update,id
    FROM mailing_list
    WHERE email = '$email'
    ORDER BY $order LIMIT $start_row, $max_per_page;"
    );

    echo
    "<table class=\"sortable\" id=\"query_quick2\" width=\"100%\" >\r\n";
    echo
    "\t<tr><th></th><th>Promoter
    Locus</th><th>Update?</th><th>Anatomical Area</th><th>Update?</th><th>Cell
    Type</th><th>Update?</th><th>Other Cell Type</th><th>Update?</th><th>Genetic
    Background</th><th>Update?</th><th>Other Gen.
    Back.</th><th>Update?</th><th>Author</th><th>Update?</th><th>Other</th><th>Update?</th><th></th>\r\n"
    ;
    echo "</table>";

    if(
    $result->num_rows){
    while (
    $row = $result->fetch_array()){
    $RowCount ++;
    $row_color = ($RowCount % 2) ? $color1 : $color2;

    echo "<form method = \"post\" action=\".{$_SERVER['PHP_SELF']}.\">";
    echo "<table width=\"100%\" >";
    echo "\t<tr class=\"$row_color\" ><td><input type=\"submit\" name=\"edit_mail\" value = \"Edit\"/></td>
    <td>{$row['pro']}</td><td>{$row['pro_update']}</td><td>{$row['ana']}</td>
    <td>{$row['ana_update']}</td><td>{$row['cell']}</td><td>{$row['cell_update']}</td><td>{$row['cellother']}</td>
    <td>{$row['cellother_update']}</td><td>{$row['gen']}</td><td>{$row['gen_update']}</td><td>{$row['genother']}</td>
    <td>{$row['genother_update']}</td><td>{$row['author']}</td><td>{$row['author_update']}</td><td>{$row['other']}</td><td>{$row['other_update']}</td>
    <td><input type=\"submit\" name=\"delete_mail\" value =\"Delete\"/></td>
    <td><input type =\"hidden\" name = \"id\" value=\"{$row['id']}\"/></td>
    </tr>"
    ;
    echo "</table>";
    echo "</form>";

    }
    }

    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  7. #7
    SitePoint Enthusiast tommy168's Avatar
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THANK YOU smftre !!!

    man this is the best php forum existed on the internet,

    i can ALWAYS get great help from here

    Appreciate it !

  8. #8
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem tommy168!
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  9. #9
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    One more thing that you should get into the habit of doing is indenting your code.
    Its a personal preference but it does make it easier to read and debug.
    PHP Code:
    function am_query_display_quick($page,$order ='DATE DESC'){
        global 
    $email;
        
    $conn db_connect_2();
        
    $color1 "tableRowOdd";
        
    $color2 "tableRowEven";
        
    $RowCount 0;

        
    $result $conn->query(
            
    "SELECT 
                  pro
                , pro_update
                , ana
                , ana_update
                , cell
                , cell_update
                , cellother
                , cellother_update
                , gen,gen_update
                , genother
                , genother_update
                , author
                , author_update
                , other
                , other_update
                , id
            FROM
                mailing_list
            WHERE 
                email = '
    $email'
            ORDER BY 
                
    $order 
            LIMIT 
                
    $start_row$max_per_page
                "
    );

        if(
    $result->num_rows){
            while (
    $row $result->fetch_array()){
                
    $RowCount ++;
                
    $row_color = ($RowCount 2) ? $color1 $color2;
                
                echo 
    '
                <form method = "post" action=" '
    $_SERVER['PHP_SELF'] .'">
                    <table class="sortable" id="query_quick2" width="100%" >
                        <tr>
                            <th></th>
                            <th>Promoter Locus</th>
                            <th>Update?</th>
                            <th>Anatomical Area</th>
                            <th>Update?</th>
                            <th>Cell Type</th>
                            <th>Update?</th>
                            <th>Other Cell Type</th>
                            <th>Update?</th>
                            <th>Genetic Background</th>
                            <th>Update?</th>
                            <th>Other Gen.Back.</th>
                            <th>Update?</th>
                            <th>Author</th>
                            <th>Update?</th>
                            <th>Other</th>
                            <th>Update?</th>
                            <th></th>
                        </tr>
                        <tr class="'
    $row_color .'" >
                            <td><input type="submit" name="edit_mail" value ="Edit"></td>
                            <td>'
    $row['pro'] .'</td>
                            <td>'
    $row['pro_update'] .'</td>
                            <td>'
    $row['ana'] .'</td>
                            <td>'
    $row['ana_update'] .'</td>
                            <td>'
    $row['cell'] .'</td>
                            <td>'
    $row['cell_update'] .'</td>
                            <td>'
    $row['cellother'] .'</td>
                            <td>'
    $row['cellother_update'] .'</td>
                            <td>'
    $row['gen'] .'</td>
                            <td>'
    $row['gen_update'] .'</td>
                            <td>'
    $row['genother'] .'</td>
                            <td>'
    $row['genother_update'] .'</td>
                            <td>'
    $row['author'] .'</td>
                            <td>'
    $row['author_update'] .'</td>
                            <td>'
    $row['other'] .'</td>
                            <td>'
    $row['other_update'] .'</td>
                            <td><input type="submit" name="delete_mail" value="Delete"/></td>
                            <td><input type="hidden" name="id" value="'
    $row['id'] .'"/></td>
                        </tr>
                    </table>
                </form>'
    ;
                
            
            }
        }
                

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  10. #10
    SitePoint Enthusiast tommy168's Avatar
    Join Date
    Feb 2011
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very true, thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •