1

I am creating a custom joomla module based on form submission but i am having some problems in inserting the values I have a html form which has certain fieldsets which pass values to mod_score.php which checks for empty validation and proceeds for helper.php where i have the function for inserting the form values.

Testing it on my localhost (XAMPP )
Version 3.4
Template Protostar

  // This is the class

class modScoreCardHelper
        {
    function saveCard($params)
        {
        $input= new JInput;
        $match_id           =   $input->get('match_id',null);
        $match_time         =   $input->get('match_time',null);
        $match_venue        =   $input->get('match_venue',null);
        $match_team_one     =   $input->get('match_team_one',null);
        $match_team_two     =   $input->get('match_team_two',null);
        $match_result       =   $input->get('match_result',null);
        $match_umpire_one   =   $input->get('match_umpire_one',null);
        $match_umpire_two   =   $input->get('match_umpire_two',null);
        $match_mom          =   $input->get('match_mom',null);
        $match_toss         =   $input->get('match_toss',null);
        $match_fow          =   $input->get('match_fow',null);
        $match_comments     =   $input->get('match_comments',null);
        $match_debutants    =   $input->get('match_debutants',null);


        // Get a db connection.
        $db = JFactory::getDbo();
        echo 'starting to insert in db';
        // Create a new query object.
        $query = $db->getQuery(true);

        // Insert columns.
        $columns = array('match_id', 'match_time', 'match_venue', 'match_result','match_team_one',
                'match_team_two','match_umpire_one','match_mom','match_comments','match_debutants',
                'match_umpire_two','match_toss','match_fow');



        $values = array($match_id, $match_time, $match_venue,$match_result,$match_team_one,
                $match_team_two,$match_umpire_one,$match_mom,$match_comments,
                $match_debutants,$match_umpire_two,$match_toss,$match_fow);




        // Prepare the insert query.
        $query->insert($db->quoteName('#__match_record'))
          ->columns($db->quoteName($columns))
         ->values(implode(',', $values));

        // Set the query using our newly populated query object and execute it.
            $db->setQuery($query);
            $db->execute();


        }
    }

I simply copied the query in phpmyadmin and executed the code it gave me error 1064 as it is showing on the site.I searched a lot on google and added double quotes to the column values field like "MI "," RCB " in phpmyadmin and the query executed sucessfully .there but i do not know how to add double quotes.Also in the query it is deleting the spaces in the values
For example : actual value = MI won becomes MIwon.

how to remove those spaces and double quotes in my query and remove this error Thanks in advance

enter image description here

  • Are you sure all the columns you have defined such as match_id exist? Have you also ensured that all the values are being pulled correctly? Try var_dump($match_id); and see what the outcome is – Lodder May 15 '15 at 11:33
  • Yes I have checked all the columns.Where should i add this var_dump. In my helper.php or mod_score.php ! – Srinath Naidu May 15 '15 at 11:41
  • Add it before your database query – Lodder May 15 '15 at 11:54
1

Replace your $input with following:

$input = JFactory::getApplication()->input

Use $db->quote($name_of_your_value_var);

So your $values will be as follows:

$values = array($match_id, $db->quote($match_time), $db->quote($match_venue),$db->quote($match_result),$db->quote($match_team_one),
                $db->quote($match_team_two),$db->quote($match_umpire_one),$db->quote($match_mom),$db->quote($match_comments),
                $db->quote($match_debutants),$db->quote($match_umpire_two),$db->quote($match_toss),$db->quote($match_fow));

You can omit $db->quote if you are passing integer to the database. for e.g. I've not added $db->quote for $match_id. (assuming you are passing integer in that variable)

Also, I would suggest you to use JTable method for CRUD operations on database. It will surely make your life easier.

  • It worked but it is still storing values in the database like MIwon instead of MI won. Thank your for help ! – Srinath Naidu May 15 '15 at 11:52
  • What is the method of your form? GET or POST? You should first consider printing $values array with print_r($values) and provide the output here. – Sahil Purav May 15 '15 at 12:00
  • post ! okay i am going to try that ! – Srinath Naidu May 15 '15 at 12:03
2

Just a little to expand on @Sahil Purav's answer. The reason why your values might not be being stored as they should is because you need to define a filter in your inputs.

Try adding RAW to all of them, like so:

$match_id           =   $input->get('match_id', null, 'RAW');
$match_time         =   $input->get('match_time', null, 'RAW');
$match_venue        =   $input->get('match_venue', null, 'RAW');
$match_team_one     =   $input->get('match_team_one', null, 'RAW');
$match_team_two     =   $input->get('match_team_two' null, 'RAW');
$match_result       =   $input->get('match_result', null, 'RAW');
$match_umpire_one   =   $input->get('match_umpire_one', null, 'RAW');
$match_umpire_two   =   $input->get('match_umpire_two', null, 'RAW');
$match_mom          =   $input->get('match_mom', null, 'RAW');
$match_toss         =   $input->get('match_toss', null, 'RAW');
$match_fow          =   $input->get('match_fow', null, 'RAW');
$match_comments     =   $input->get('match_comments', null, 'RAW');
$match_debutants    =   $input->get('match_debutants', null, 'RAW');
  • Yes sir that worked ! – Srinath Naidu May 15 '15 at 12:02
  • Awesome, I missed that part @Lodder :) – Sahil Purav May 15 '15 at 12:09
  • Perfect answer, It worked like a charm – David Addoteye Oct 12 '15 at 21:15
  • @DavidAddoteye - glad it helped ;) – Lodder Oct 12 '15 at 22:42
  • $input->get('match_mom', null, 'RAW') is good but not safe. you can use $input->get('match_mom', null, 'STRING') if you are passing strings in a form name or sentence. $input->get('match_id', null, 'INT') if you are passing integer. You can get full details of the filters here docs.joomla.org/Retrieving_request_data_using_JInput – David Addoteye Nov 3 '15 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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