[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

wordpress plugin sql problem

Posted on 2011-04-21
7
Medium Priority
?
356 Views
Last Modified: 2012-05-11
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
Comment
Question by:drumichael87
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:kumaranmca
ID: 35439555
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
 
LVL 1

Author Comment

by:drumichael87
ID: 35441998
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
 
LVL 8

Expert Comment

by:kumaranmca
ID: 35442461
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 31

Accepted Solution

by:
gwkg earned 1500 total points
ID: 35444438
instead of using $wpdb->query, try using $wpdb->update as detailed here

http://codex.wordpress.org/Function_Reference/wpdb_Class#UPDATE_rows
0
 
LVL 8

Expert Comment

by:kumaranmca
ID: 35447410
Hi,

Are you find the error?
0
 
LVL 1

Author Comment

by:drumichael87
ID: 35493566
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
 
LVL 1

Author Closing Comment

by:drumichael87
ID: 35493573
The actual problem wasn't solved, but an alternative solution was provided.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 19 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question