簡體   English   中英

Joomla 3更新數據庫

[英]Joomla 3 update database

在拖放系統上工作以在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" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.7.1.custom.min.js"></script>

<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("updateDB.php", order, function(theResponse){
                $("#contentRight").html(theResponse);
            });                                                              
        }                                 
        });
    });

}); 
</script>



<div id="contentLeft">
            <ul>

<?php
                $query  = "SELECT * FROM kxmkw_k2_items WHERE created_by = 1000 AND trash = 0 ORDER BY sorteren ASC";
                $result = mysql_query($query);

                while($row = mysql_fetch_array($result, MYSQL_ASSOC))
                {
                ?>
                    <li id="recordsArray_<?php echo $row['id']; ?>"><?php echo $row['title'] . "<br> " . $row['id']; ?></li>
                <?php } ?>
            </ul>
        </div>

在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.';
}
?>

如您所見,我進行了一些更改:

  • $_POST替換$_POST JInput方法。
  • 更換$db->query()$db->execute()$db->query()已過時。
  • 使用最新方法來更新數據庫中的數據
  • 將字段和條件設置為數組,以防您將來希望添加更多。
  • 在實際的查詢中,使用quoteName()替換了escape_string函數,該功能可以quoteName()此問題。

有關如何在Joomla中插入或更新數據的更多信息,請閱讀以下內容:

http://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase

更新2:

嘗試使用此代替:

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

我已經使用這些更改更新了完整的查詢。

如果這不起作用,請嘗試轉儲變量以查看是否有類似這樣的值:

var_dump($action);
var_dump($updateRecordsArray);

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM