1

我有一个名为 projects 的 SQL 数据库,其中包含 post_id、feature_image、order 和 Landscape 字段。景观是所有密集目的的一个类别。

作为我要达到的功能的广泛概述;

页面加载 1 个记录集获取所有分类的 post_id#(1、2、4、5、8、12...等)并重新编号,具体为 1、2、3、4、5...等每个 post_id# 的重新编号将然后在一个名为 order 的字段中向 SQL 发送更新,该字段将采用这些值。然后 2 个记录集(一个奇数,一个偶数)加载不是 post_id,而是 order# 和所有相关信息(在本例中为特色图像),并在屏幕上输出两列图片,这些图片基于任何类别的正确顺序加载。

我知道我可以通过 post_id 订购,这不是问题。我特别希望能够在页面加载时将所有数字重新排序为数字 1、2、3、4、5、6 等。

我只发布了 if 语句的第一部分,因为我知道不是它的那部分搞砸了。一切都适用于变量等,唯一的问题是我无法让该死的变量在字段顺序下发送到数据库。我在这方面工作了太久,现在不知道我做错了什么。没有错误,只是订单字段在数据库中保持为 NULL。

我将为任何可以修复此代码或告诉我为什么我在做所有事情时都做低音的人举办一场纸牌游行,并且有更好的方法可以做到这一点。

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;    
case "long":
case "int":
  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  break;
case "double":
  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  break;
case "date":
  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  break;
case "defined":
  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  break;
}
return $theValue;
}
}

mysql_select_db($database_connTest, $connTest) or die("cannot select database");
$query_RS_Odd = "SELECT projects.post_id, projects.feature_image, projects.`order` FROM projects WHERE projects.landscape = 'landscape'";
$RS_Odd = mysql_query($query_RS_Odd, $connTest) or die(mysql_error());
$row_RS_Odd = mysql_fetch_assoc($RS_Odd);
$totalRows_RS_Odd = mysql_num_rows($RS_Odd);
?>

<?php do {
if ($row_RS_Odd['post_id'] == 1){ 
    $resort_next = 1;
    mysql_query("UPDATE projects SET order=".$resort_next." WHERE post_id=".$row_RS_Odd['post_id']."");
    echo $row_RS_Odd['post_id'];
    echo $resort_next;
}
else {
echo 'No records found';
}
 } while ($row_RS_Odd = mysql_fetch_assoc($RS_Odd)); ?>
4

1 回答 1

0

order尽管您在语句中正确引用了 mysql 保留字select,但您忘记在update语句中这样做:

mysql_query("UPDATE projects SET order=".$resort_next." WHERE post_id=".$row_RS_Odd['post_id']."");

应该:

mysql_query("UPDATE projects SET `order`=".$resort_next." WHERE post_id=".$row_RS_Odd['post_id']."");

请注意,这些mysql_*函数已弃用,您应该真正切换到 PDO / mysqli 和准备好的语句。

于 2012-10-01T17:29:43.500 回答