5

So I find this a little weird because the DB field was updating without much effort on my part, until I started writing in file upload functionality. Now the database doesn't seem to be updating, but the file upload is working. Everything seemed to be inserting, updating, etc. just fine simply by specifying proper names in the admin form's XML file:

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset>
        <field
            name="course_id"
            type="hidden" />
        <field
            name="course_name"
            type="text"
            label="Course Name: "
            description="Full name of the course (up to 255 characters)"
            default="" />
        <field
            name="course_dept"
            type="text"
            label="Course Department: "
            description="4 character department code. Examples: BIOL, CREL, CHEM"
            default="" />
        <field
            name="course_code"
            type="text"
            label="Course Code: "
            description="4 digit course code which immediately preceeds department code"
            default="" />
        <field
            name="course_desc"
            type="textarea"
            label="Course Description: "
            rows="10"
            description="As seen in the academic calendar"
            default="" />
        <field
            name="course_graphic_url"
            type="file"
            label="Course Graphic: "
            description="Graphical representation of the course"
            accept="image/*" />
    </fieldset>
</form>

From what I've read, I have to write the database update logic. It's just weird that it seemed to be working before. Now I am new to Joomla MVC component development, so maybe I copy/pasted a few lines of code that would have done it without my realization.. But I doubt it. Here's what I've attempted but I appear to be falling short somewhere. This is /admin/controllers/course.php:

<?php defined('_JEXEC') or die;

jimport('joomla.application.component.controllerform');

class CourseListControllerCourse extends JControllerForm {
protected $view_list = 'CourseList';

function save($key = null, $urlVar = null){
    // ---------------------------- Uploading the file ---------------------
    // Neccesary libraries and variables
    jimport( 'joomla.filesystem.folder' );
    jimport('joomla.filesystem.file');

    $jinput = JFactory::getApplication()->input;
    $files = $jinput->files->get('jform');
    $filename = $files['course_graphic_url']['name'];
    $folder = JPATH_SITE . "/" . "images" . "/" . "courselist";

    // Create the folder if not exists in images folder
    if ( !JFolder::exists( $folder ) ) {
        JFolder::create( $folder, 0777 );
    }

    $src = $files['course_graphic_url']['tmp_name'];
    $dest = JPATH_SITE . "/" . "images" . "/" . "courselist" . "/" . $filename;

    if (isset($files['course_graphic_url'])) {
        JFile::upload( $src, $dest );
    }

    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    $cid =  $jinput->get('course_id');

    $fields = array($db->quoteName('course_graphic_url') . " = " . $db->quote($dest));
    $conditions = array($db->quoteName('course_id') . " = " . $cid);

    $query
        ->update($db->quoteName('#__courselist'))->set($fields)->where($conditions);

    $db->execute($query);

    return parent::save($key = null, $urlVar = null);
   }
}

Hopefully I've provided enough information.. I'm still stuck in weekend mode so it's hard to say :P Thanks in advance!

EDIT: I realize that the course_id, a url variable that appears when I am in the edit page, was not being passed (as far as I can tell) through when the save button is clicked. I attempted to create a hidden input field with <?php echo $_GET['course_id']; ?> as the value, in hopes that I would fix all that is wrong in the world today but had no luck.

What is strange to me is that every other DB field is updating properly, with no additional SQL needed. The appropriate field was updated with the values in the corresponding form fields. I AM SO CONFUSE RIGHT NOW.

2 Answers 2

1

I'd suggest starting at the actual SQL that is being executed and working back from there.

Just before any query is run, add a line like this: echo $query->dump(); die;

It will show the SQL that is about to be executed. I add the die so that I can keep refreshing the page without having to continually complete forms, etc. to see the effects of my debugging.

6
  • THANK YOU. This is definitely a step in the right direction. it outputs what looks to be like a valid sql statement: UPDATE qd6q4_courselist SET course_graphic_url = 'dustin-old-tkd.jpg' WHERE course_id = 2... horray for progress!
    – dustin
    Dec 1, 2014 at 19:23
  • It's the exact SQL that is about to be executed - usually gives me the "aha" moment to see what I'm missing, or where I went wrong.
    – GDP
    Dec 1, 2014 at 19:29
  • it seems to be valid sql so now i am investigating why the query doesn't seem to be executing. thanks again for your assistance. error messages or just output in general is so nice to see...
    – dustin
    Dec 1, 2014 at 19:46
  • i assume you know about $db->getErrorMsg() :)
    – GDP
    Dec 1, 2014 at 19:54
  • i do now! i did not as i am a noob and a self-proclaimed script kiddie.
    – dustin
    Dec 1, 2014 at 20:13
1

First of all, big thank you to GDP for leading me in the right direction. I have to say that the problem was quite a stupid one. As you'll notice in my code, I get a query object:

$query = $db->getQuery(true);

I build the query:

$fields = array($db->quoteName('course_graphic_url') . " = " . $db->quote($dest));
$conditions = array($db->quoteName('course_id') . " = " . $cid);

$query
    ->update($db->quoteName('#__courselist'))->set($fields)->where($conditions);

I, however, do not set the query object!!!! Here is the newly added code. What a dummy :P

$db->setQuery($query);
$db->execute($query);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.