我有一个php应用程序,用于显示存储在数据库中的一些图片。我想用javascript来改变照片顺序。 Index.php页面如下所示:
$photos = array();
$photos_query = "SELECT * FROM `photos` WHERE `id`=".$_SESSION['id']."ORDER BY `order`";
$data = mysqli_query($dbc, $photos_query);
while ($photos_row= mysqli_fetch_assoc($data)){
$photos[] = array(
'photo_id' => $photos_row['photo_id'],
'id' => $photos_row['id'],
'photo_name' => $photos_row['photo_name'],
'order' => $photos_row['photo_order']
);
} ...
<div id="message"></div>
<div id="showphotos">
if (empty($photos)) {
echo 'No photos';
} else {
echo '<ul>';
foreach ($photos as $photo) {
echo '<li id="photoid_'.$photo['photo_id'].'">
<img src="uploads/', $photo['id'], '/', $photo['photo_id'],'"' .$photo['photo_name']. 'height="150" width="150">
... </li>';
} echo '</ul>';
}
</div>
一旦用户尝试更改图像的位置,此页面将调用move.js:
$(document).ready(function(){
function slideOut(){
setTimeout(function(){
$("#message").slideUp("slow", function () {});
}, 2000);
};
$("#message").hide();
$(function() {
$('ul').sortable({
cursor: 'move',
opacity: 0.7,
revert: false,
update: function(){
var neworder = $(this).sortable("serialize") + '&update=update';
$.post("change_image_position.php", neworder, function (themessage) {
$("#message").html(themessage);
$("#message").slideDown('slow');
slideOut();
});
}
});
});
});
最后,change_image_position.php包含:
$array = $_POST['photoid'];
if ($_POST['update'] == "update"){
$count = 1;
foreach ($array as $idval) {
$query = "UPDATE photos SET order = " . $count . " WHERE photo_id = " . $idval;
mysqli_query($query) or die('Error, insert query failed');
$count ++;
}
echo 'All saved! refresh the page to see the changes';
}
我面临两个问题......首先,order by statement(index.php)不起作用,其次,通过数据库查询(change_image_position.php)更改照片的查询不会返回任何结果。提前感谢您的帮助。
答案 0 :(得分:0)
您的mySQL语句缺少空格 -
$photos_query = "SELECT * FROM `photos` WHERE `id`=".$_SESSION['id']."ORDER BY `order`";
应该是
$photos_query = "SELECT * FROM `photos` WHERE `id`=".$_SESSION['id']." ORDER BY `order`";
我认为您目前正在进行照片保存的方式在neworder
部分失败。
您目前正在传递$update
,然后拨打$_POST['update']
,这将只是update=update
。