1

I need your help on a subject that is out of my knowledge.

So, a short story what's going on. I'm developing a WordPress plugin for a company that wants to have custom casting platform for its actors. However I ran in to an issue while trying to use wp Ajax and custom DataBase queries in a php foreach loop.

Detailed explanation:

  • I have included my ajax file properly (followed by the wp documentation, the file is localized)

  • I got a file actor-credits.php and in this file I use many custom fields for data input into the WordPress user_meta table, I'm mainly using $key and $value for the different fields, but in this case I had to use also and the umeta_id column because I'm using an array with options to include into the db.

I have 6 fields that their value must be used as an array (it's a actor credit for a movie) so all those 6 fields must be in array added to the database and I'm using add_user_meta to add the records into the database, here's a snippet of the code that I'm using to add the to the wp db.

$current_user_id = get_current_user_id();

if(!empty($_POST)){
$post_vars = array('credit_stage', 'credit_film', 'credit_featuredfilm','credit_commercial','credit_comedy','credit_voiceproject','credit_furthercredits');
    foreach ($post_vars as $pkey => $pvalue) {
        if(isset($_POST[ $pvalue ])){
            if(is_array($_POST[ $pvalue ])){
                foreach ($_POST[ $pvalue ] as $post_key => $post_value) {
                    add_user_meta( $current_user_id, $pvalue , trim($_POST[ $pvalue ][ $post_key ]) );
                }
            }
        }
    }
}

and I want tot have the functionality to delete single credit from the database and that's why I've managed to write my own sql to connect to the database and retrieve the fields with the umeta_id incl. and here's a snippet now I did it:

Starting code -

echo '<form style="margin-bottom:70px;" action="" id="dltftr" method="post">';
$further_credits = $wpdb->get_results( "SELECT * FROM  `{$wpdb->prefix}usermeta` WHERE  `user_id` =". $current_user_id ." AND  `meta_key` LIKE  '%credit_furthercredits%'" );
echo '<h3>Further credits</h3>';
foreach ($further_credits as $obj)
{
    $further_id = $obj->umeta_id;
    echo '<p> '.$obj->meta_value.' <button style="color:#000;background:transparent;" class="delete-further glyphicon glyphicon-trash" style="display:inline;" type="submit" name="dltftr" value=""></button></p>';
}
echo '</form>';
if(isset($_POST['dltftr']))
{$wpdb->get_results( "DELETE FROM `{$wpdb->prefix}usermeta` WHERE `{$wpdb->prefix}usermeta`.`umeta_id`= ".$further_id."" );}

-End code

As you can see I've added also a delete db query connected to a rendered html delete button.

Now the real problem here comes that each time when I hit the delete button deletes either the first or the last record.

What are your thoughts guys?

If you want to see a real example of what's happening, with pleasure I'm sharing with you a login details.

URL: https://nuboyana.bgshared.top/sign-in
username: 123123
password: 123123

The page I'm struggling with is located at the Credits tab

Any help is appreciated, highly and kindly.

Thank you for the time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.