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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题