gpt4 book ai didi

javascript - 使用 JavaScript 更新数据库值

转载 作者:行者123 更新时间:2023-11-30 00:49:28 25 4
gpt4 key购买 nike

我有一个 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 语句(index.php)不起作用,其次,通过数据库查询(change_image_position.php)更改照片的查询不返回任何结果。预先感谢您的帮助。

最佳答案

您的 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

关于javascript - 使用 JavaScript 更新数据库值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21102819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com