简体   繁体   中英

Joomla inserting data with JDatabase where table name is stored in a php variable

Description: I'm creating the database name automatically consisting of main table name and appending the full name relative to what is selected in dropdown menus.

Code:

       if ($leagueSelect === $result['leagueName'] && $divisionSelect === $result['divisionID'] && $tourType === 'robin') {

            $tableName = "footleague_".$leagueSelect.$divisionSelect;
            /****CREATE TABLE****/

            $db = JFactory::getDBO();
            $query = $db->getQuery(true);

            $query = "CREATE TABLE IF NOT EXISTS `".( $tableName )."` (
                        `id` INT(11) NOT NULL AUTO_INCREMENT,
                        `round` TEXT NULL,
                        `logoHome` TEXT NULL,
                        `home` TEXT NULL,
                        `usrHome` TEXT NULL,
                        `scoreHome` INT(11) NULL,
                        `scoreAway` INT(11) NULL,
                        `logoAway` TEXT NULL,
                        `away` TEXT NULL,
                        `usrAway` TEXT NULL,
                        `confirm` INT(11),
                      PRIMARY KEY (`id`)
                )";

            $db->setQuery($query);

            $result = $db->execute();

            if ($result == true) {
                echo 'Table created successfully!';    
            }else{
                echo "Something went wrong with table creation. Please try again.";
            } 
    }

This works perfectly and it creates a new table and all of the columns. The problem comes when storing data into this table. I need to be able to take the table name which is stored in a variable and store the data into it. Same as in CREATE TABLE.

I tried it like this:

    $query = "INSERT INTO ".$tableName." (round, logoHome, home, usrHome, logoAway, away, usrAway),
                              VALUES ('".$round."' , '".$logoHome."' , '".$home."' , '".$usrHome."' , '".$logoAway."' , '".$away."' , '".$usrAway."')";

                    $db->setQuery($query);

                    $result = $db->execute();

...but it doesn't give me any errors and it doesn't store the data.

I tried also with stdClass object but I don't know how to get the table name in there:

    $data = new stdClass();
    $data->round = $round;
    $data->home = $home;
    $data->usrHome = $usrHome;
    $data->logoHome = $logoHome;
    $data->away = $away;
    $data->usrAway = $usrAway;
    $data->logoAway = $logoAway;

    $db = JFactory::getDBO();
    $db->insertObject($tableName, $data);

How can I get this to work?

Thank you.

I solved it like this:

    $data = new stdClass();
    $data->round = $round;
    $data->home = $home;
    $data->usrHome = $usrHome;
    $data->logoHome = $logoHome;
    $data->away = $away;
    $data->usrAway = $usrAway;
    $data->logoAway = $logoAway;

    $db = JFactory::getDBO();
    $db->insertObject($tableName, $data);

    if ($result == true) {
         echo 'Tournament was created successfully!';
    }else{
         echo 'ERROR!';
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM