[英]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.