WordPress.org

Ready to get started?Download WordPress

Forums

Delete rows from custom table in db (6 posts)

  1. mikegarlick
    Member
    Posted 1 year ago #

    I have a table i created in the database that holds simple information on projects like their title, description etc...

    I want to add a link that will allow users to delete all date on that certain project if they wish.

    Below is the code I have so far but when I click the button it does nothing.

    if (isset($_POST['delete_all'])) {
    	global $wpdb;
    	$wpdb->query(
    			'DELETE * FROM $wpdb->mg_project_info
    		 	WHERE project_title = "'.$album.'"
                AND user_name = "'.$user_name.'"'
    	);
    	$wpdb->query(
    		  'DELETE * FROM $wpdb->mg_project_images
    		   WHERE project_title = "'.$album.'"
               AND user_name = "'.$user_name.'"'
    	);
    }

    and

    <form action="mylink" method="post" enctype="multipart/form-data">
    <input type="submit"  name="delete_all" value="Delete Project"  />
    </form>

    any help would be appreciated.

  2. mikegarlick
    Member
    Posted 1 year ago #

    I have since removed the * after the DELETE as I was informed thats not allowed but still no joy. it just doesn't do anything (to the table/rows).

  3. jibbius
    Member
    Posted 1 year ago #

    Hi mikegarlick,

    The sql looks ok, but sometimes it's difficult to see what's actually happening.
    For debug purposes, look for and install a plugin called Blackbox - that will tell you what SQL was actually run, and if it created any errors.

  4. jibbius
    Member
    Posted 1 year ago #

    Other suggestion would be to use a 'projectid' field, rather than trying to match on user + title.
    My 2 cents :)

  5. mikegarlick
    Member
    Posted 1 year ago #

    thanks for your input jibbius :)

    It means re-jigging my tables and initial INSERT sql but yeah thats probably the best way forward using the ID.

  6. jibbius
    Member
    Posted 1 year ago #

    No worries.

    In theory, your method should still have worked.
    Try the blackbox plugin, and see what sql it ran (if any). It slows the site a little, so deactivate it when you're done.

    If it's not running any sql - then your problem is in the form/submit.
    If it IS running the SQL, but nothing got deleted:
    - copy and paste it into phpmyadmin
    - replace the delete with " select * "
    - play around with it till you start seeing some rows.

    Goodluck.

Topic Closed

This topic has been closed to new replies.

About this Topic