Results 1 to 5 of 5

Thread: PHP/MySQL - Delete article from database

  1. #1
    Join Date
    Oct 2010
    Posts
    15

    PHP/MySQL - Delete article from database

    I have problem with a small "CMS" system and in connection with the publication of articles (in a very simple level, that is), etc. Now, I have since come to the point where I will make it possible to delete articles. What I then did was that I picked out all the articles, and included "while" loop where I have a link tag with a variable. Okay difficult to explain, but the main thing was that you could press a title (which was loop), thus deleting the article from the database. Here is my code: (will probably be a little easier to understand more from it)

    PHP Code:
    <h3>Delete one or more articles</h3>
    <?php
    $connect 
    mysql_connect('localhost''root''');
    if(!
    $connect){
            die(
    'Unable to connect to: 'mysql_error());
    }
    mysql_select_db('minicms'$connect);
    $result mysql_query('SELECT * FROM article');
    $delete_one DELETE FROM article WHERE title=$row['title'];
    while(
    $row mysql_fetch_assoc($result)){
            echo 
    '<a href=\"'.$delete_one.'">'.$row['title']
    .
    '</a><br/>';
            echo 
    $row['date'].' '.$row['time'].'<br/><br/>';
    }
    ?>
    What is correct and should it be done? Keep in mind that this should just work without too many other extra stuff, which really is not necessary for it to work so far

  2. #2
    Join Date
    Nov 2008
    Posts
    1,022

    Re: PHP/MySQL - Delete article from database

    1) You have delete query outside whileloop. That will result in $row does not exist, therefore it does not work.
    2) If you move your query into the whileloop all formulas will be deleted when you load the page again. To prevent it, make a clear link.
    3) Use either id to delete the article, so you do not have to delete all of them having the same title.

    Small example:
    PHP Code:
    <?php
    if(@$_GET['slettid']) {
            
    /** query **/
            /** F.eks: DELETE FROM article WHERE id = $_GET['deleteid'] LIMIT 1 **/       
    }
    while() {
            
    /** ... **/
            
    echo '<a href="./?deleteid=' $row['id'] . '">Delete</a>';
            
    /** ... **/
    }
    ?>

  3. #3
    Join Date
    Oct 2010
    Posts
    15

    Re: PHP/MySQL - Delete article from database

    Ok. Thanks for your reply. Have now done that you pointed out, but the problem is not fixed quite yet. (Yet all the Error is gone). When I press, I will be directed to a page (no.php ago, I made) and when I go back nothing has happened. There is certainly something in the code, which I do not quite understand myself. Here is the updated code:

    PHP Code:
    <h3>Delete one or more articles</h3><hr/>
    <?php
    $connect 
    mysql_connect('localhost''root''');
    if(!
    $connect){
            die(
    'Unable to connect to: 'mysql_error());
    }
    mysql_select_db('minicms'$connect);
    $result mysql_query('SELECT * FROM article ORDER BY id DESC');

    if(@
    $_GET['deleteid']){
            
    $delete_one mysql_query('DELETE FROM article WHERE id = $_GET["deleteid"] LIMIT 1');
            echo 
    'The text was deleted!';
    }
    while(
    $row mysql_fetch_assoc($result)){
            echo 
    '<a href="index.php?page=delete_article?deleteid=' $row['ID'] . '">';
            echo 
    $row['ID'].', ';
            echo 
    $row['title'].', ';
            echo 
    $row['date'].'  '.$row['time'].'<br/><br/>';
            echo 
    '</a>';
    }
    ?>

  4. #4
    Join Date
    Nov 2008
    Posts
    1,192

    Re: PHP/MySQL - Delete article from database

    There is some small mistake that you have done in your while loop. You have actually used question mark (?) in the link address. Instead you should have used ampersand (&). So change your link address to "index.php?page=delete_article&deleteid". Moreover you can not use "normal" variables within single block.

    PHP Code:
    try
    mysql_query ('DELETE FROM article WHERE id ='. $ _GET ['deleteid']. 'LIMIT 1'); 

  5. #5
    Join Date
    Oct 2010
    Posts
    15

    Re: PHP/MySQL - Delete article from database

    It solved the problem that I came to findnot.php ago. And even echo 'article was deleted'; appears when I click on the links. But when I check if it really is deleted, it is not! What am I doing that is wrong? Look no specific errors that I could have done. Anyways thanks void for your concerned it helped me a lot. At last I have no problems now.

Similar Threads

  1. Working with MySQL database
    By Garlands in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 10:36 PM
  2. Database Replication in MySQL
    By Muhammad Waqar in forum Software Development
    Replies: 5
    Last Post: 21-12-2009, 12:34 PM
  3. Replies: 3
    Last Post: 07-11-2009, 09:36 PM
  4. How to add mysql database to localhost?
    By gazwsx in forum Software Development
    Replies: 3
    Last Post: 01-07-2009, 07:07 PM
  5. How to Connect MySQL database from PHP
    By Booth in forum Software Development
    Replies: 3
    Last Post: 21-01-2009, 09:12 PM

Tags for this Thread

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
  •  
Page generated in 1,711,673,333.39672 seconds with 17 queries