• Hy i have insert and get_row in function.php but they don’t work there is my code :

    ###########################
    // SIGNALEMENT DE LIEN MORT
    add_action('wp_ajax_signalement_dead_link', 'callback_signalement_dead_link');
    add_action('wp_ajax_nopriv_signalement_dead_link', 'callback_signalement_dead_link');
    function callback_signalement_dead_link()
    {
        global $_REQUEST;
        global $post;
    
        if (!wp_verify_nonce($_REQUEST['nonce'], 'ajax_nonce')) die();
    
        $response = array(
            'error' => ''
        );
    
        $post_id = $_REQUEST['post_id'];
        $post_title = get_the_title( $post_id );
    		$category = get_the_category($post->$post_id);
    		$category_name = $category[0]->name;
        $post_category = $category_name;
        $nom_bouton = $_REQUEST['nom_bouton'];
        $dead_link = $_REQUEST['dead_link'];
    
        $edit_url = '<a href="' . admin_url('post.php?post=' . $post_id . '&action=edit') . '">' . $post_category . $post_title . '</a>';
        $name_dead_link = '<a href="' . $dead_link . '">' . $nom_bouton . '</a>';
    
        if (!empty($post_id) || !empty($nom_bouton) || !empty($link)) {
            global $wpdb;
    		$tablename = $wpdb->prefix . "dead_link";
            $check_if_exist = $wpdb->get_results("SELECT link FROM $tablename WHERE link = $name_dead_link");
    
            if ($check_if_exist != null) {
                $wpdb->insert('$tablename', array(
                    'episode' => $edit_url,
                    'link' => $name_dead_link,
                ) , array(
                    '%s',
                    '%s',
                ));
            }
            else {
                $response['error'] = 'Lien déjà signalé.';
            }
            echo json_encode($response);
            die();
        }
        else {
            die();
        }
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • What errors are you getting? Where is it not working? What values are you getting along the way when the code executes?

    You will need to do some basic de-bugging here to find out what’s going on. It’s really as simple as adding in this sort of thing:

    echo "<p>Expect var to be 5 - '".$var."'</p>";

    Add in things like that to check what the values are when you’re doing if() statements, and you’ll most likely see where your code is going wrong.

    Thread Starter JibsouX

    (@jibsoux)

    The error is :
    no data are input in database, the ajax work well :
    https://imagik.fr/view-rl/140236
    The js console don’t show any error

    My table is (prefix).dead_link
    https://imagik.fr/view-rl/140237

    but it always return : $response[‘error’] = ‘Lien déjà signalé.’;

    Is there a syntax error in my php query ? or not ?

    my code is in function.php if i make some debug test like you suggest where the echo will be print ??

    there is my complete code :

    JS :

    jQuery(document).ready(function() {
        jQuery('#1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16, #17, #18, #19, #20, #21').click(function(e) {
            var answer = confirm("Signaler le lien comme mort ?")
            if (answer) {
                var numero = jQuery(this).attr('id');
                var nom_bouton = jQuery('#lien' + numero).text();
                var dead_link = jQuery('#lien' + numero).attr('href');
    
                jQuery.post(THEMEREX_ajax_url, {
                    action: 'signalement_dead_link',
                    nonce: THEMEREX_ajax_nonce,
                    post_id: <?php echo $post_id; ?>,
                    nom_bouton: nom_bouton,
                    dead_link: dead_link,
                }).done(function(response) {
                    var rez = JSON.parse(response);
                    if (rez.error === '') {
                            alert("Le lien a bien été signalé, merci.");
                    }
                    else {
                        alert("Le lien a déjà été signalé, merci.");
                    }
                });
            } else {
                alert("Votre signalement n'a pas été pris en compte")
            }
            e.preventDefault();
            return false;
        });
    });

    PHP (function.php)

    ###########################
    // SIGNALEMENT DE LIEN MORT
    add_action('wp_ajax_signalement_dead_link', 'callback_signalement_dead_link');
    add_action('wp_ajax_nopriv_signalement_dead_link', 'callback_signalement_dead_link');
    function callback_signalement_dead_link()
    {
        global $_REQUEST;
        global $post;
    
        if (!wp_verify_nonce($_REQUEST['nonce'], 'ajax_nonce')) die();
    
        $response = array(
            'error' => ''
        );
    
        $post_id = $_REQUEST['post_id'];
        $post_title = get_the_title( $post_id );
    		$category = get_the_category($post->$post_id);
    		$category_name = $category[0]->name;
        $post_category = $category_name;
        $nom_bouton = $_REQUEST['nom_bouton'];
        $dead_link = $_REQUEST['dead_link'];
    
        $edit_url = '<a href="' . admin_url('post.php?post=' . $post_id . '&action=edit') . '">' . $post_category . $post_title . '</a>';
        $name_dead_link = '<a href="' . $dead_link . '">' . $nom_bouton . '</a>';
    
        if (!empty($post_id) || !empty($nom_bouton) || !empty($link)) {
            global $wpdb;
    		$tablename = $wpdb->prefix . "dead_link";
            $check_if_exist = $wpdb->get_results("SELECT link FROM $tablename WHERE link = $name_dead_link");
    
            if ($check_if_exist != null) {
                $wpdb->insert('$tablename', array(
                    'episode' => $edit_url,
                    'link' => $name_dead_link,
                ) , array(
                    '%s',
                    '%s',
                ));
            }
            else {
                $response['error'] = 'Lien déjà signalé.';
            }
            echo json_encode($response);
            die();
        }
        else {
            die();
        }
    }

    What is the actual SQL that’s produced for your query? Looking through it I can see two areas of concern with this line:

    $wpdb->get_results("SELECT link FROM $tablename WHERE link = $name_dead_link");

    First, When you use get_results() it returns either an array of result or an empty array if there are no results, so when you check the result against NULL (you should also use !is_null($check_if_exist) instead) that value will never actually be a NULL value anyway, so that check will always fail. It should be if (count ($check_if_exist) == 0)

    The second is that you don’t escape the value of $name_dead_link, so that is the most likely place that you’ll be getting SQL errors.

    It should be done as something like this:

    $query = $wpdb->prepare ('SELECT COUNT(link) FROM '.$wpdb->prefix.'dead_link WHERE link = %s LIMIT 1', $name_dead_link);
    
    if ($wpdb->get_var ($query) == 1) {
        // Found result so does exist
    }
    else {
        // No results so does not exist
    }
    Thread Starter JibsouX

    (@jibsoux)

    Thanks a lot !
    Okey with that the check is passed but still no data add in the database : the insert dont work i think..

    My db : https://imagik.fr/view-rl/140237
    my ajax : https://imagik.fr/view-rl/140236

    ###########################
    // SIGNALEMENT DE LIEN MORT
    add_action('wp_ajax_signalement_dead_link', 'callback_signalement_dead_link');
    add_action('wp_ajax_nopriv_signalement_dead_link', 'callback_signalement_dead_link');
    function callback_signalement_dead_link()
    {
        global $_REQUEST;
    	global $wpdb;
        global $post;
    
        if (!wp_verify_nonce($_REQUEST['nonce'], 'ajax_nonce')) die();
    
        $response = array(
            'error' => ''
        );
    
        $post_id = $_REQUEST['post_id'];
        $post_title = get_the_title( $post_id );
    		$category = get_the_category($post->$post_id);
    		$category_name = $category[0]->name;
        $post_category = $category_name;
        $nom_bouton = $_REQUEST['nom_bouton'];
        $dead_link = $_REQUEST['dead_link'];
    
        $edit_url = '<a href="' . admin_url('post.php?post=' . $post_id . '&action=edit') . '">' . $post_category . $post_title . '</a>';
        $name_dead_link = '<a href="' . $dead_link . '">' . $nom_bouton . '</a>';
    
        if (!empty($post_id) || !empty($post_title) || !empty($nom_bouton) || !empty($dead_link)) {
    		$tablename = $wpdb->prefix . "dead_link";
    
            $check_if_exist = $wpdb->prepare ('SELECT COUNT(link) FROM $tablename WHERE link = %s LIMIT 1', $name_dead_link);
    
            if ($wpdb->get_var ($check_if_exist) != 1) {
                $wpdb->insert('$tablename', array(
                    'episode' => $edit_url,
                    'link' => $name_dead_link,
                ) , array(
                    '%s',
                    '%s',
                ));
            }
            else {
                $response['error'] = 'Lien déjà signalé.';
            }
            echo json_encode($response);
            die();
        }
        else {
            die();
        }
    }

    As I said, what errors are you getting? What are the values that are being generated along the way? Where is the code actually failing?

    What debugging have you tried yourself? What does it show? Where does it get to before it has problems?

    This is where you need to learn how to debug your own code. That’s really the only way that you’re ever going to learn how to do this the right way.

    Thread Starter JibsouX

    (@jibsoux)

    Okey Found it !!!! :
    i have to use just after my query (insert) :

    exit( var_dump( $wpdb->last_query ) );

    To show what is the last query that hit the database and before with :

    $wpdb->insert('$tablename', array(
                    'episode' => $edit_url,
                    'link' => $name_dead_link,
                ) , array(
                    '%s',
                    '%s',
                ));

    it was return : $tablename instead of the real table name, now with :

    $wpdb->insert($tablename, array(
                    'episode' => $edit_url,
                    'link' => $name_dead_link,
                ) , array(
                    '%s',
                    '%s',
                ));

    all work well and i have made the same with :

    $check_if_exist = $wpdb->prepare ('SELECT COUNT(link) FROM $tablename WHERE link = %s LIMIT 1', $name_dead_link);

    to >

    $check_if_exist = $wpdb->prepare ('SELECT COUNT(link) FROM ' . $tablename . ' WHERE link = %s LIMIT 1', $name_dead_link);

    Now all the code :

    JS :

    jQuery(document).ready(function() {
        jQuery('#1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16, #17, #18, #19, #20, #21').click(function(e) {
            var answer = confirm("Signaler le lien comme mort ?")
            if (answer) {
                var numero = jQuery(this).attr('id');
                var bouton = jQuery('#lien' + numero).text();
    			var nom_bouton = encodeURIComponent(bouton);
                var dead_link = jQuery('#lien' + numero).attr('href');
    
                jQuery.post(THEMEREX_ajax_url, {
                    action: 'signalement_dead_link',
                    nonce: THEMEREX_ajax_nonce,
                    post_id: <?php echo $post_id; ?>,
                    nom_bouton: nom_bouton,
                    dead_link: dead_link,
                }).done(function(response) {
                    var rez = JSON.parse(response);
                    if (rez.error === '') {
                            alert("Le lien a bien été signalé, merci.");
                    }
                    else {
                        alert("Le lien a déjà été signalé, merci.");
                    }
                });
            } else {
                alert("Votre signalement n'a pas été pris en compte")
            }
            e.preventDefault();
            return false;
        });
    });

    PHP

    ###########################
    // SIGNALEMENT DE LIEN MORT
    add_action('wp_ajax_signalement_dead_link', 'callback_signalement_dead_link');
    add_action('wp_ajax_nopriv_signalement_dead_link', 'callback_signalement_dead_link');
    function callback_signalement_dead_link()
    {
        global $_REQUEST;
    	global $wpdb;
        global $post;
    
        if (!wp_verify_nonce($_REQUEST['nonce'], 'ajax_nonce')) die();
    
        $response = array(
            'error' => ''
        );
    
        $post_id = $_REQUEST['post_id'];
        $post_title = get_the_title( $post_id );
    		$category = get_the_category($post->$post_id);
    		$category_name = $category[0]->name;
        $post_category = $category_name;
        $nom_bouton = $_REQUEST['nom_bouton'];
        $dead_link = $_REQUEST['dead_link'];
    
        $edit_url = '<a href="' . admin_url('post.php?post=' . $post_id . '&action=edit') . '">' . $post_category . $post_title . '</a>';
        $name_dead_link = '<a href="' . $dead_link . '">' . $nom_bouton . '</a>';
    
        if (!empty($post_id) || !empty($post_title) || !empty($nom_bouton) || !empty($dead_link)) {
    		$tablename = $wpdb->prefix . "dead_link";
    
            $check_if_exist = $wpdb->prepare ('SELECT COUNT(link) FROM ' . $tablename . ' WHERE link = %s LIMIT 1', $name_dead_link);
    
            if ($wpdb->get_var ($check_if_exist) != 1) {
                $wpdb->insert($tablename, array(
                    'episode' => $edit_url,
                    'link' => $name_dead_link,
                ) , array(
                    '%s',
                    '%s',
                ));
    			exit( var_dump( $wpdb->last_query ) );
            }
            else {
                $response['error'] = 'Lien déjà signalé.';
            }
            echo json_encode($response);
            die();
        }
        else {
            die();
        }
    }

    I have an other litle question :
    here i check for response error if there is some string but how can i do somthing more simple like if error = 0 / if error = 1 ?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘$wpdb->insert & $wpdb->get_row in function.php’ is closed to new replies.