• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

wordpress plugin sql problem

I am really stuck here, and I think I need an extra set of eyes to help me out.  

I am developing a Wordpress plugin that uses it's own table in the db.  I currently am able to add a record and delete a record and need to be able to update a record.  

i have successfully loaded the record into a form, but I can't get it to actually update.  

This is my main panel function:
function ir_assign_mainpanel() {

	if ($_REQUEST['mode']=='assignedit') {
		ir_assign_editform($_REQUEST['assignid']);
		exit;
	}

	if (isset($_REQUEST['irassigneditdo'])) {
		ir_assign_editdo($_REQUEST['assignid']);
		?><div id="message" class="updated fade"><p><strong><?php _e('Assignment Updated'); ?>.</strong></p></div><?php
	}
	
	if ($_REQUEST['mode']=='assigndelete') {
		ir_assign_delete($_REQUEST['assignid']);
		?><div id="message" class="updated fade"><p><strong><?php _e('Assignment Deleted'); ?>.</strong></p></div><?php
	}
	
	ir_assign_listall(); 

}

Open in new window


When the user clicks 'edit', it passes the 'assignid' and a 'mode' of "assignedit" to back to the same page we are on (the main panel) like this:
<a href="admin.php?page=ir_assign_mainpanel&amp;mode=assignedit&amp;assignid='.$assignlisthw2->assignid.'">

Open in new window


Then the ir_assign_editform() function loads:
function ir_assign_editform ($assignid) {
	global $wpdb;
	$table_name = $wpdb->prefix . "ir_assignments";
	$getassign3 = $wpdb->get_row("SELECT * FROM $table_name WHERE assignid = $assignid");

		echo '<div class="wrap">';
		echo '<h2>Edit Assignment</h2>';
		echo '<br />';
		echo '<div id="ir_assign_form">';
		echo '<form name="editassign" method="post" action="admin.php?page=ir_assign_mainpanel">';
		echo '<label for="assignment_type">Assignment Type</label>
			  <select name="assignment_type">
				  <option>Homework</option>
				  <option>Test</option>
				  <option>Announcement</option>
			  </select><span class="tip">Be sure this setting is correct, even if you aren\'t changing it!</span><br/>
			  
			  <label for="assignment_text">Assignment Description</label>
			  <textarea name="assignment_text" cols="45" rows="5">'.stripslashes($getassign3->assignment_text).'</textarea><br/>
			  
			  <label for="date_due">Due Date:</label>
			  <input name="date_due" value="'.stripslashes($getassign3->date_due).'"id="datepicker" type="text" /><br/>
			  
			  <label for="submit">&nbsp;</label>
			  <input type="hidden" name="assignid" value="'.$getassign3->assignid.'">
			  <input name="irassigneditdo" type="submit" value="Update Assignment">';
		echo '</form></div></div>';
}

Open in new window


Then this form submits back to the same page yet again, and sends "irassigneditdo" with the submit button, which fires off the ir_assign_editdo() function:
function ir_assign_editdo($assignid) {
	global $wpdb;
	$table_name = $wpdb->prefix . "ir_assignments";

	echo $assignid; 
	echo '<br />' . $_POST['assignment_type'];
	echo '<br />' . $_POST['assignment_text'];
	echo '<br />' . $_POST['date_due'];
	
	$assignid = $_POST['assignid']; 
	$assignment_type = $wpdb->escape($_POST['assignment_type']);
	$assignment_text = $wpdb->escape($_POST['assignment_text']);
	$date_due = $wpdb->escape($_POST['date_due']);
	
	$wpdb->query("UPDATE " . $table_name .
	" SET assignment_type = '$assignment_type', ".
	" assignment_text = '$assignment_text', ".
	" date_due = '$date_due', ".
	" WHERE assignid = '$assignid'");

}

Open in new window


This is where I am totally stuck.  You can see that I am echoing all of the values that this function receives from POST, and all 4 of these 'echo' lines work.  They display the value of what is in the form that was loaded with data to edit.  What I see displayed should be the new values in the database, however the database just simply never gets updated.  

I'm not sure what I've done wrong, really.  I've been staring at this for hours.  Any help is appreciated.  

By the way - my delete function works the same way, I pass a 'mode' of "assigndelete", and my delete function works beautifully!

function ir_assign_delete($assignid) {
	global $wpdb;
	$table_name = $wpdb->prefix . "ir_assignments";
	
	$insert = "DELETE FROM " . $table_name .
	" WHERE assignid = ".$assignid ."";
	
	$results = $wpdb->query( $insert );
}

Open in new window

0
drumichael87
Asked:
drumichael87
  • 3
  • 3
1 Solution
 
kumaranmcaCommented:
Hi,

Please change your link the below format and check the function is call

<a href="admin.php?page=ir_assign_mainpanel&mode=assignedit&assignid='.$assignlisthw2->assignid.'">
0
 
drumichael87Author Commented:
It took me a while to figure out that you were suggesting I change the "&amp;" to "&", however the link works fine because all of my functions execute as expected, except for the SQL statement.
0
 
kumaranmcaCommented:
Hi,

I have checked your SQL Quries all are fine. Please check your database and table details. Please error_report is on then find the mistake. Thanks

 ini_set('display_errors', 1);

Regards,
Kumaran Paranthaman
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
gwkgCommented:
instead of using $wpdb->query, try using $wpdb->update as detailed here

http://codex.wordpress.org/Function_Reference/wpdb_Class#UPDATE_rows
0
 
kumaranmcaCommented:
Hi,

Are you find the error?
0
 
drumichael87Author Commented:
Thank you for all of your help - as it turns out, I had a comma after the last field to be updated.  I had more update fields listed and deleted the one off of the end of the query, but forgot to remove that comma.  

I will, however probably start using $wpdb->update from now on though.  Looks like it's easier, and less room for errors like these!
0
 
drumichael87Author Commented:
The actual problem wasn't solved, but an alternative solution was provided.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now