doubiankang2845 2013-02-13 23:15
浏览 37
已采纳

为什么这些简单的更新和插入sql函数与我的php查询无法正常工作?

I am working on a custom joomla component that simply adds a points table to the database with two fields (id,points)... id is the primary key and I am trying to set it to be the same as the user joomla user id...

I need one function that returns the number of points for a user & another that updates the users points to a given value... I successfully created the first one called:

getUserPoints()

It returns the number of points for a given user... Then I started the second update function called:

updateUserPoints()

This is the part that is being "inconsistent" at the moment and I think I have wrong... I am just looking to make a simple function that will update the points value for the current user... and also I was trying to handle the case of if there is no record for them... then create one!.... I did this by making a function within the updateUserPoints() function called:

newUserCheck()

It's supposed to check if there are points for the user and then reply true or false:

$db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
$db->query();
$points = $db->loadResult();
    if($points){
           return false;
       } else {
           return true;
     }

Then either update create a new record OR update the existing one... This code is almost working!... It worked for me in three tests... however when I add a second user it seems to get a little inconsistent... It would create a new user successfully and give them 5 points.... then when I run it again to update it says "Points Updated" but it doesn't seem to actually update the points value in the database?!

With one or no existing users it seems to work fine! Anyway... Open to suggestions... I am sure there is a bunch I am doing wrong here but I would like to get it working properly! I am new to MVC programming as you can probably tell :P

Here is the joomla controller in its entirety:

<?php defined('_JEXEC') or die('');
// userprofilepoints CONTROLLER

    jimport('joomla.application.component.controller');
    jimport('joomla.user.helper');

    class userprofilepointsController extends JController {

        function getUserPoints(){

            $values = JRequest::get("post");
            $userIdNumber = $values['userIdNumber'];
            $user = & JFactory::getUser();

            $db = JFactory::getDBO();
            $db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
            $db->query();
            $points = $db->loadResult();

            echo $points;
        }

        function updateUserPoints(){

            function newUserCheck(){

                $values = JRequest::get("post");
                $userIdNumber = $values['userIdNumber'];
                $user = & JFactory::getUser();
                $db = JFactory::getDBO();
                $db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
                $db->query();
                $points = $db->loadResult();

                    if($points){
                        return false;
                    } else {
                        return true;
                    }
            }

            if(newUserCheck() == false){

                $values = JRequest::get("post");
                $userIdNumber = $values['userIdNumber'];
                $newPointsAmount = $values['newPointsAmount'];
                $user = & JFactory::getUser();
                $db = JFactory::getDBO();               
                $db->setQuery("UPDATE #__mytable SET points = '".$newPointsAmount."' WHERE id = '".$user->id."'");
                $db->query();

                //echo getUserPoints();
                //$points = $db->loadResult();
                echo "Points Updated";

            } else if(newUserCheck() == true) {

                $values = JRequest::get("post");
                $userIdNumber = $values['userIdNumber'];
                $user = & JFactory::getUser();
                $db = JFactory::getDBO();               
                $db->setQuery("INSERT INTO #__mytable SET id = $user->id, points = 5");
                $db->query();

                echo "New User Created!";

            }

        }
}

And finally my html and javascript from the view!.... Thank you!!! :)

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

// MAIN PAGE TEMPLATE
    jimport('joomla.user.helper');
    $user = & JFactory::getUser();
    $profile = JUserHelper::getProfile($user->id);
?>

<script type="text/javascript">
jQuery(document).ready(function($) {

$('#updateBTN').click(function(){

    updateUserPoints();
});


function getUserPoints(){

    //Post the following to the controller
    $.post('http://www.mysite.com/index.php?format=raw&option=com_userprofilepoints&task=getUserPoints', {

        userIdNumber:   <?php echo $user->id; ?>

    }, function(data) {

        alert(data);                        

    }); 
}

function updateUserPoints(){

    //Post the following to the controller
    $.post('http://www.mysite.com/index.php?format=raw&option=com_userprofilepoints&task=updateUserPoints', {

        userIdNumber:   <?php echo $user->id; ?>,
        newPointsAmount:    555444  //random hardcoded points amount to test with

    }, function(data) {

        alert(data);                        

    }); 
}

});

</script>

<div id="main_block">
    <p>Points Page:</p>
    <br />
    <button id="updateBTN">Trigger Update</button>
</div>
  • 写回答

1条回答 默认 最新

  • dpmwy80068 2013-02-14 00:04
    关注

    Remove the query line from the newUserCheck()

    $db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
    $db->query();
    $points = $db->loadResult();
    

    should actually be:

    $db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
    $points = $db->loadResult();
    

    loadResult() will run the query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探