Php Joomla 3更新数据库

Php Joomla 3更新数据库,php,joomla,database-connection,Php,Joomla,Database Connection,使用拖放系统在joomla内部订购物品。 从数据库中获取项目不是问题。 我使用下面的代码来实现这一点 `<script type="text/javascript"> $(document).ready(function(){ $(function() { $("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() { var or

使用拖放系统在joomla内部订购物品。 从数据库中获取项目不是问题。 我使用下面的代码来实现这一点

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

    $(function() {
        $("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
            var order = $(this).sortable("serialize") + '&action=updateRecordsListings'; 
            $.post("templates/sorteren/test/updateDB.php", order, function(theResponse){
                $("#contentRight").html(theResponse);
            });                                                              
        }                                 
        });
    });
}); 
</script>

    <?php
        $db = & JFactory::getDBO();
    $query = $db->getQuery(true)
        ->select('a.title, a.id, a.sorteren')
        ->from('#__k2_items as a')
        ->where('a.created_by =' . $user->id . ' and a.trash = 0')
        ->order('a.sorteren');
    $db->setQuery($query);
    $items = $db->loadObjectList();
    foreach($items as $item) {
        echo '<li id="recordsArray_' . $item->id . '">' . $item->title . '';

    }?>`
但是问题出在下面的代码中,但我不知道问题出在哪里

    <?php defined('_JEXEC') or die;
$db =& JFactory::getDBO();
$query = $db->getQuery(true);


$action                 = mysql_real_escape_string($_POST['action']); 
$updateRecordsArray     = $_POST['recordsArray'];

if ($action == "updateRecordsListings"){

    $listingCounter = 1;
    foreach ($updateRecordsArray as $recordIDValue) {

$query = "UPDATE #__k2_items SET sorteren = " . $listingCounter . " WHERE id = " . $recordIDValue;
                    $db->setQuery($query);
                    $db->query();   


        $listingCounter = $listingCounter + 1;  
    }

    echo '<pre>';
    print_r($updateRecordsArray);
    echo '</pre>';
    echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
?>
更新:

我在Joomla外面用这个,效果很好

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

    $(function() {
        $("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
            var order = $(this).sortable("serialize") + '&action=updateRecordsListings'; 
            $.post("templates/sorteren/test/updateDB.php", order, function(theResponse){
                $("#contentRight").html(theResponse);
            });                                                              
        }                                 
        });
    });

}); 
</script>
<?php
        $db = & JFactory::getDBO();
    $query = $db->getQuery(true)
        ->select('a.title, a.id, a.sorteren')
        ->from('#__k2_items as a')
        ->where('a.created_by =' . $user->id . ' and a.trash = 0')
        ->order('a.sorteren');
    $db->setQuery($query);
    $items = $db->loadObjectList();
    foreach($items as $item) {
        echo '<li id="recordsArray_' . $item->id . '">' . $item->title . '';

    }?>
在joomla里面我用这个

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

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

$input = new JInput;
$post = $input->getArray($_POST);
$action = $post["action"];
$updateRecordsArray = $post["recordsArray"];

if ($action == "updateRecordsListings"){

    $listingCounter = 1;
    foreach ($updateRecordsArray as $recordIDValue) {

        $fields = array(
            $db->quoteName('sorteren') . '=' . $listingCounter
        );   
        $conditions = array(
            $db->quoteName('id') . ' = ' . $recordIDValue
        );

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

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

        $listingCounter = $listingCounter + 1;  
    }

    echo '<pre>';
    print_r($updateRecordsArray);
    echo '</pre>';
    echo 'If you refresh the page, you will see that records will stay just as you modified.';
}
?>

首先,您并不是对所有内容都使用Joomla编码标准,对于您使用的那些位,它们是旧标准

尝试使用以下方法:

$input = new JInput;
$post = $input->getArray($_POST);
$action = $post["action"];
$updateRecordsArray = $post["recordsArray"];
我已经用这些更改更新了完整查询

如果这不起作用,请尝试转储变量,以查看是否存在如下值:


希望这对房客有所帮助,感谢您的快速回复和解释,但这也不会更新数据库。如果我添加var_dump,它将返回null。在乔姆拉外面它在工作,但在乔姆拉里面它出了问题。给我更多的建议干杯\@user3021953-请参阅我答案中的更新2。你能在你的问题中加入表格html吗?请更新我上面的问题。谢谢你帮我搞定了。。!谢谢洛德的帮助
$input = new JInput;
$post = $input->getArray($_POST);
$action = $post["action"];
$updateRecordsArray = $post["recordsArray"];
var_dump($action);
var_dump($updateRecordsArray);