Jump to content

Recommended Posts

Trying to update to database but not updating an not getting any errors.  The info is posting but not sure why not updating.

if (!empty($_POST)) {
        global $wpdb;
            $table = $tablename;
            
                $kudomsg = $_POST['kudomsg'];
                $kudoagent = $_POST['kudoagent'];
                $kudoagid = $_POST['kudoagentid'];
                $kudocust = $_POST['kudocust'];
                $kudoacct = $_POST['kudoacct'];
                $kudoclient = $_POST['kudoclient'];
                $kudoloc = $_POST['agentloc'];

            $data = array(
                'kudomsg' => $kudomsg,
                'kudoagent' => $kudoagent,
                'kudoagid' => $kudoagid,
                'kudocust' => $kudocust,
                'kudoacct' => $kudoacct,
                'kudoclient' => $kudoclient,
                'kudoloc' => $kudoloc
            );
 
            $where = array('kudoid' => $_POST['kudoid']);             

            $success=$wpdb->update($table , $data, $where);
            if($success){
            
        echo "Kudos has been updated.  Go to Kudos page to view";
        echo "<br /><a href='" .$kudourl."'>Close</a>";   
} } else {
 
  $kudoacct = $_GET['acct'];
     $tablename=acikudos_table_name();
     $editkudos = $wpdb->get_results( "SELECT * FROM $tablename WHERE kudoacct = '$kudoacct'");
    
   foreach ( $editkudos as $editkudo )
	{ 
?> 
<form name="acikudos_form" id="updatekudos" method="post" action="" enctype="multipart/form-data">
	<table>
	<tr>
		<td>Agent Name</td>
		<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
		<td><input name="kudoagent" type="text" value="<?php echo $editkudo->kudoagent; ?>" /></td>
	</tr>
	<tr>
		<td>Agent ID</td>
		<td></td>
		<td><input name="kudoagentid" type="text" value="<?php echo $editkudo->kudoagid; ?>" /></td>
	</tr>
	<tr>
		<td>Agent Location</td>
		<td></td>
		<td><select name="agentloc" size="1">
	<option value="<?php echo $editkudo->kudoloc; ?>"><?php echo $editkudo->kudoloc; ?></option>
	<option value="Amherst Center">Amherst Center</option>
	<option value="Charlottetown Center">Charlottetown Center</option>
	<option value="Kingston Center">Kingston Center</option>
	</select></td>
	</tr>
	<tr>
		<td>Agent Queue</td>
		<td></td>
		<td><select name="kudoclient" size="1">
	<option value="<?php echo $editkudo->kudoclient; ?>"><?php echo $editkudo->kudoclient; ?>"</option>
	<option value="AR">AR</option>
	<option value="Eastlink Tech Support">Eastlink Tech Support</option>
	<option value="PNI">PNI</option>
	</select></td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
	</tr>
		<tr>
		<td>Customer Name</td>
		<td></td>
		<td><input name="kudocust" type="text" value="<?php echo $editkudo->kudocust; ?>"/></td>
	</tr>
		<tr>
		<td>Customer Account #</td>
		<td></td>
		<td><input name="kudoacct" type="text" value=" <?php echo $kudoacct; ?>" /></td>
	</tr>
		<tr>
		<td>Kudos</td>
		<td></td>
		<td><textarea cols="50" name="kudomsg" rows="5"><?php echo $editkudo->kudomsg; ?></textarea></td>
	</tr>
		<tr>
		<td><input type="hidden" value="<?php echo $editkudo->kudoid; ?>" name="kudoid"></td>
		<td></td>
		<td></td>
	</tr>
		<tr>
		<td><button type="submit" value="sendkudos" class="aciformbutton" name="sendkudos" />Update Kudos</button>
		<td></td>
		<td><button type="reset" value="reset" class="aciformbutton">Start Over</button></td>
	</tr>

</table>

</form>

 

Link to comment
https://forums.phpfreaks.com/topic/308758-update-to-database/
Share on other sites

Stripped out the permission section of the script until I get the update issue resolved.  Here is the full page:

function kudos_edit_new() {
global $userdata, $current_user, $wpdb;
wp_get_current_user();

//start of update

if(isset($_POST['submit'])) {
        global $wpdb;
        
            $tablename=acikudos_table_name();
            $kudotable = $tablename;
            
                $kudomsg = $_POST['kudomsg'];
                $kudoagent = $_POST['kudoagent'];
                $kudoagid = $_POST['kudoagentid'];
                $kudocust = $_POST['kudocust'];
                $kudoacct = $_POST['kudoacct'];
                $kudoclient = $_POST['kudoclient'];
                $kudoloc = $_POST['agentloc'];
                $kudoid = $_POST['kudoid'];

           $wpdb->update( 
	        $kudotable, 
            array(
                'kudomsg' => $kudomsg,
                'kudoagent' => $kudoagent,
                'kudoagid' => $kudoagid,
                'kudocust' => $kudocust,
                'kudoacct' => $kudoacct,
                'kudoclient' => $kudoclient,
                'kudoloc' => $kudoloc
                 ),
           array('kudoid' => $kudoid),
           array(
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s'
                 ),
            array( '%d' ) 
                 );     

$lastsql = $wpdb->last_query;
echo $lastsql;
echo "<br />Kudos Updated";
            
	} else {
		echo "Something went really wrong";
	}


//end of update


   
  
     $kudoacct = $_GET['acct'];
     $tablename=acikudos_table_name();
     $editkudos = $wpdb->get_results( "SELECT * FROM $tablename WHERE kudoacct = '$kudoacct'");
    
   foreach ( $editkudos as $editkudo )
	{ 
?> 
<form name="acikudos_form_update" id="updatekudos" method="post" action="" enctype="multipart/form-data">

	<table>
	<tr>
		<td>Agent Name</td>
		<td>     </td>
		<td><input name="kudoagent" type="text" value="<?php echo $editkudo->kudoagent; ?>" /></td>
	</tr>
	<tr>
		<td>Agent ID</td>
		<td></td>
		<td><input name="kudoagentid" type="text" value="<?php echo $editkudo->kudoagid; ?>" /></td>
	</tr>
	<tr>
		<td>Agent Location</td>
		<td></td>
		<td><select name="agentloc" size="1">
	<option value="<?php echo $editkudo->kudoloc; ?>"><?php echo $editkudo->kudoloc; ?></option>
	<option value="Amherst Center">Amherst Center</option>
	<option value="Charlottetown Center">Charlottetown Center</option>
	<option value="Kingston Center">Kingston Center</option>
	</select></td>
	</tr>
	<tr>
		<td>Agent Queue</td>
		<td></td>
		<td><select name="kudoclient" size="1">
	<option value="<?php echo $editkudo->kudoclient; ?>"><?php echo $editkudo->kudoclient; ?>"</option>
	<option value="AR">AR</option>
	<option value="Tech Support">Tech Support</option>
	<option value="PNI">PNI</option>
	</select></td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
	</tr>
		<tr>
		<td>Customer Name</td>
		<td></td>
		<td><input name="kudocust" type="text" value="<?php echo $editkudo->kudocust; ?>"/></td>
	</tr>
		<tr>
		<td>Customer Account #</td>
		<td></td>
		<td><input name="kudoacct" type="text" value=" <?php echo $kudoacct; ?>" /></td>
	</tr>
		<tr>
		<td>Kudos</td>
		<td></td>
		<td><textarea cols="50" name="kudomsg" rows="5"><?php echo $editkudo->kudomsg; ?></textarea></td>
	</tr>
		<tr>
		<td><input type="hidden" value="<?php echo $editkudo->kudoid; ?>" name="kudoid"></td>
		<td></td>
		<td></td>
	</tr>
		<tr>
		<td><button type="submit" value="submit" class="aciformbutton" name="submit" />Update Kudos</button>
		<td></td>
		<td><button type="reset" value="reset" class="aciformbutton">Start Over</button></td>
	</tr>

</table>

</form>

<?php  

 }
}

This is what I get when I submit:

SHOW FULL COLUMNS FROM `wpl2_acikudos`

Kudos Updated 

 

Edited by SkyRanger
privacy issue
Link to comment
https://forums.phpfreaks.com/topic/308758-update-to-database/#findComment-1567110
Share on other sites

After hours of beating head off desk and not being able to figure out what is going on I scrapped most of the code and expanded with more functions with doing some reason online with some examples and trying to implement them in to my own code.  So first sorry if this is the wrong area  for this, i am getting the error in the acikudos-update.php script and I can not figure out why:

acikudo-plugin.php

function acikudos_enqueue_script() {
    wp_enqueue_script(
        'acikudos',
        plugins_url( ACI_KUDOS_URL.'/js/kudoform.js', __FILE__ ), // URL path relative to the folder of the PHP script this code is placed in 
        array( 'jquery' ) // Requires jQuery, make sure it's loaded first
    );

    wp_localize_script(
        'acikudos', // Name of script, from above
        'acikudos', // Name of JavaScript variable
        array(
            'ajaxUrl' => admin_url( 'admin-ajax.php' ),
        )
    );
}

add_action( 'wp_enqueue_scripts', 'acikudos_enqueue_script' );

acikudos-edit.php (this works)

  $kudoacct = $_GET['acct'];
     $tablename=acikudos_table_name();
     $editkudos = $wpdb->get_results( "SELECT * FROM $tablename WHERE kudoacct = '$kudoacct'");
    
   foreach ( $editkudos as $editkudo )
	{ 
?> 
<form name="acikudos-update" id="acikudos-update" method="post" action="<?php echo plugins_url( 'advantage-kudos/include/acikudos-update.php' ); ?>" enctype="multipart/form-data">

	<table>
	<tr>
		<td>Agent Name</td>
		<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
		<td><input name="kudoagent" type="text" value="<?php echo $editkudo->kudoagent; ?>" /></td>
	</tr>
	<tr>
		<td>Agent ID</td>
		<td></td>
		<td><input name="kudoagentid" type="text" value="<?php echo $editkudo->kudoagid; ?>" /></td>
	</tr>
	<tr>
		<td>Agent Location</td>
		<td></td>
		<td><select name="agentloc" size="1">
	<option value="<?php echo $editkudo->kudoloc; ?>"><?php echo $editkudo->kudoloc; ?></option>
	<option value="Amherst Center">Amherst Center</option>
	<option value="Charlottetown Center">Charlottetown Center</option>
	<option value="Kingston Center">Kingston Center</option>
	</select></td>
	</tr>
	<tr>
		<td>Agent Queue</td>
		<td></td>
		<td><select name="kudoclient" size="1">
	<option value="<?php echo $editkudo->kudoclient; ?>"><?php echo $editkudo->kudoclient; ?>"</option>
	<option value="AR">AR</option>
	<option value="Eastlink Tech Support">Eastlink Tech Support</option>
	<option value="PNI">PNI</option>
	</select></td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
	</tr>
		<tr>
		<td>Customer Name</td>
		<td></td>
		<td><input name="kudocust" type="text" value="<?php echo $editkudo->kudocust; ?>"/></td>
	</tr>
		<tr>
		<td>Customer Account #</td>
		<td></td>
		<td><input name="kudoacct" type="text" value=" <?php echo $kudoacct; ?>" /></td>
	</tr>
		<tr>
		<td>Kudos</td>
		<td></td>
		<td><textarea cols="50" name="kudomsg" rows="5"><?php echo $editkudo->kudomsg; ?></textarea></td>
	</tr>
		<tr>
		<td><input type="hidden" value="<?php echo $editkudo->kudoid; ?>" name="kudoid"></td>
		<td></td>
		<td></td>
	</tr>
		<tr>
		<td><button type="submit" value="update" class="aciformbutton" name="submit" />Update Kudos</button>
		<td></td>
		<td><button type="reset" value="reset" class="aciformbutton">Start Over</button></td>
	</tr>

</table>

</form>

<?php  

 }

acikudos-update.php (here is the problem)
Fatal error: Uncaught Error: Call to undefined function add_action() in C:\Program Files (x86)\Ampps\www\wp-content\plugins\advantage-kudos\include\acikudos-update.php on line 112
Error: Call to undefined function add_action() in C:\Program Files (x86)\Ampps\www\wp-content\plugins\advantage-kudos\include\acikudos-update.php on line 112

/**
 * Handle the request and return the result.
 */
function acikudos_process_request() {
    global $wpdb;

$errors         = array();      // array to hold validation errors
$data           = array();      // array to pass back data

/*Ensure no fields are empty, and if they are, return $errors array encoded in JSON */
    if (empty($_POST['kudoagent']))
        $errors['kudoagent'] = 'Agent Name is required.';

        if (empty($_POST['kudoagentid']))
        $errors['kudoagentid'] = 'Agent ID is required.';

    if (empty($_POST['agentloc']))
        $errors['agentloc'] = 'Agent Location is required.';

    if (empty($_POST['kudoclient']))
        $errors['kudoclient'] = 'Agent Queue is required.';

    if (empty($_POST['kudocust']))
        $errors['kudocust'] = 'Customer Name is required.';

    if (empty($_POST['kudoacct']))
        $errors['kudoacct'] = 'Customer Account # is required';

    if (empty($_POST['kudomsg']))
        $errors['kudomsg'] = 'Kudo Messge is required';  

    if (empty($_POST['kudoid']))
        $errors['kudoid'] = 'If you see this message contact Intranet Admin or Supervisor with date of posting kudos, tech agents name and your name';

// return a response ===========================================================

    // if there are any errors in our errors array, return a success boolean of false
    if ( ! empty($errors)) {

        // if there are items in our errors array, return those errors
        $data['success'] = false;
        $data['errors']  = $errors;
    } else {

        // if there are no errors process our form, then return a message

            /* Set the proper table name to a variable */
                        $kudotable=acikudos_table_name();
                        #$table_name = acikudos_table_name(); 
                        
                        if (isset($_POST['create'])) {
                                $kudomsg = $_POST['kudomsg'];
              					$kudoagent = $_POST['kudoagent'];
                				$kudoagid = $_POST['kudoagentid'];
                				$kudocust = $_POST['kudocust'];
                				$kudoacct = $_POST['kudoacct'];
                				$kudoclient = $_POST['kudoclient'];
                				$kudoloc = $_POST['agentloc'];
                				$kudoid = $_POST['kudoid'];

           $wpdb->update( 
	        $kudotable, 
            array(
                'kudomsg' => $kudomsg,
                'kudoagent' => $kudoagent,
                'kudoagid' => $kudoagid,
                'kudocust' => $kudocust,
                'kudoacct' => $kudoacct,
                'kudoclient' => $kudoclient,
                'kudoloc' => $kudoloc
                 ),
           array('kudoid' => $kudoid),
           array(
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s'
                 ),
            array( '%d' ) 
                 );     
                            }

        // show a message of success and provide a true success variable
        $data['success'] = true;
        $data['message'] = 'Success!';
    }

    // return all our data to an AJAX call
    echo json_encode($data);   

    return $data;
}

/**
 * AJAX handler for the "ACIKudos" action.
 */
function acikudos_ajax_handler() {
    global $wpdb;

    $data = acikudos_process_request();

    header( 'Content-Type: application/json; charset=' . get_bloginfo( 'charset' ) );
    echo json_encode( $data );
    exit;
}

add_action( 'wp_ajax_nopriv_acikudos', 'acikudos_ajax_handler' );  <--- Line 112
add_action( 'wp_ajax_acikudos',        'acikudos_ajax_handler' );

kudoform.js file

!function( $ ) { // Self-executing function closure, will ensure $ is jQuery - might be running in noConflict mode

    $( document ).ready(
        function() {
            $( "#acikudos-update" ).submit( // Don't bind to all forms, just ours
                function ( event ) {
                    event.preventDefault();

                    $('.form-group').removeClass('has-error'); // remove the error class
                    $('.help-block').remove(); // remove the error text

                    var data = $( this ).serializeArray(); // Will automatically grab all form fields and data

                    data.push({
                        name : "create",
                        value: "1"                      
                    });

                    data.push({
                        name : "action",
                        value: "acikudos" // This parameter needs to match the wp_ajax_* hook.                        
                    });

                    $.post(
                        acikudos.ajaxUrl, // Use our JavaScript variable "acikudos" we defined in wp_localize_script()
                        data, // POST data
                        function ( data ) {
                            // Your form handling
                        }                   
                    );

                }
            )
        }
    );

}( jQuery );

 

Link to comment
https://forums.phpfreaks.com/topic/308758-update-to-database/#findComment-1567119
Share on other sites

Just a heads up this is my first try at a plugin for wordpress. I have done some research on the form ajax jquery and ended up changing the action to: <?php echo admin_url('admin-ajax.php'); ?> and adding <input type="hidden" name="action" value="acikudos_ajax_handler"> to my form but now instead of just a blank white page I get a blank white page with a 0 on it and not submitting form.

ps: if this is now suppose to be in javascript can a mod or admin please move it over.

Link to comment
https://forums.phpfreaks.com/topic/308758-update-to-database/#findComment-1567132
Share on other sites

First, if you're going to be handling form processing via AJAX you don't need an action attribute on the form. Your JavaScript preventDefault() on the submit event stops the form from submitting to the action target anyway.

Your AJAX handling functions are doing a few redundant things. For instance, the acikudos_ajax_handler() function is invoking the global $wpdb in order to ignore it completely before sending the program to acikudos_process_request(), where the global $wpdb is invoked again. Then acikudos_process_request() echos the JSON encoded data before returning the array data to acikudos_ajax_handler(), which prints the JSON encoded array. Again.

Beyond that, WordPress runs all AJAX calls through the wp_ajax_{action}() and wp_ajax_nopriv_{action}() hooks, so I'm a bit confused as to how the acikudos-update.php file is even getting called at this point if the hooks for those actions are actually in that file. It's pretty normal to put all the action and filter hooks into the plugin main (or 'functions') file (I assume at this point that is acikudos-plugin.php). You can always call to external classes from the functions file by using an array as the second parameter in the add_action() or add_filter() calls - for instance:

add_action('wp_ajax_acikudos', [myObject, 'acikudos_ajax_handler'], 0, 1); 

It's late so I hope I'm not missing something you've described in your code, but I would recommend doing some more searching into how WordPress handles AJAX in general - it can be a bit confusing at first.

Beyond that, you've got a random quotation mark in the acikudos-edit.php file that should be easy to see in any decent IDE or even here in your post. I'm assuming it's an issue in the copy/paste as the file wouldn't work at all if that were the verbatim code in development and testing.

Link to comment
https://forums.phpfreaks.com/topic/308758-update-to-database/#findComment-1567145
Share on other sites

omg maxxd no that is perfect you never missed anything.  After pulling my code apart (past 24 hrs) and starting to see some of the errors that might be happening.  I am running codelobster which i like and loading up the edit file guess I was a little tired when I coded that and found a few errors. Will post update once I do a little more studying on ajax and wordpress and try to figure out the error in my ways (Getting close to finally finishing this plugin at 80% mark) just need to sort out edit part and another section in the admin so thanks for your assistance so far. Had come in handy having another set of eyes.

Link to comment
https://forums.phpfreaks.com/topic/308758-update-to-database/#findComment-1567168
Share on other sites

The biggest thing about WordPress and AJAX is that WP routes all AJAX calls through the admin_ajax file, and what exactly it does at that point is dependent on the `action` parameter of the passed data - at that point it uses it's internal routing and the 'action' parameter to figure out what file and function/method to call via the add_action() call - however, it doesn't actually pass any parameters to the called function. So you need to pass all the data in the AJAX request but don't expect any of it to actually hit the handling function that you've written - you're still going to have to go to the $_POST or $_GET superglobals for that. Sure, you could use the $_REQUEST superglobal, but WP has enough security concerns without adding more so skip $_REQUEST entirely please.

Sorry, none of that is entirely unusual but IMO if you're going to force everything to go through a centralized location you should at least consolidate and pass data to the called target. It's just courtesy for making me jump through the extra hoop.

Anyway (long and random "it's my second glass of wine" rant aside), the important part is that you technically can - like any other application - send your AJAX request to any receptive PHP file you want. The problem is that if you don't use the admin_ajax link, the WordPress core won't be loaded. So any and all of the functions, objects, or hooks you want or need to use won't exist at the time of the AJAX call.

Link to comment
https://forums.phpfreaks.com/topic/308758-update-to-database/#findComment-1567169
Share on other sites

Thanks maxxd, yeah I heard a lot of nightmares about the $)_REQUEST plan on used the $_POST method. Thinking I will take another stab at it tomorrow with fresh eyes.  Been working on this plugin for the past 14hrs so far just today (going on 12 days straight) (honestly can't wait to be done...lol).  Tonight's mission is to finish reading how all this works and learning how I messed it up so I can fight with it another day.  You have informed me a lot on how it works which is greatly appreciated.  Go enjoy your wine...lol, I am going for another beer.

Link to comment
https://forums.phpfreaks.com/topic/308758-update-to-database/#findComment-1567170
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.