1

我今天来这里是因为我遇到了一个我似乎无法解决的小问题,这就是它带来这里的原因。问题是我试图从 $_GET 变量中获取多个 ID 以打印出来。我尝试使用数组并且很好,它可以工作,但问题是它使用 [0] 制作单独的数组,我需要它作为同一个数组中的一个而不是 array() array(),这就是它的作用。

所以我使用 while() 和 mysql_fetch_array 从数据库中获取结果,它依赖于来自 url 的 $_GET 以获取正确的 id。因此,在我传入 api.php?do=remove&id=1,4,7 的 URL 中,它只会打印出第一个 id 而不会打印出其他名称。正如我所说,我尝试使用 array(),结果如下:

Array
(
    [0] => BzlXO.jpg
)
Array
(
    [0] => cHZTk.jpg
)
Array
(
    [0] => 9yset.jpg
)
Array
(
    [0] => Ss04V.jpg
)

我不明白为什么要这样做,因为我需要它在一个数组中

Array (

   [0] => BzlXO.jpg,
   [1] => cHZTk.jpg,
   [2] => 9yset.jpg,
   [3] => Ss04V.jpg
)

这样,当我将它们内爆时,它会显示为文本,如下所示:“您删除了图像“BzlXO.jpg,cHZTk.jpg,9yset.jpg,Ss04V.jpg”,类似于

        $iName[imagename] = array($iName[imagename]);
        $name = implode(",", $iName[imagename]);

这是我的代码:

这是 URL “api.php?do=remove&id=1,4,7”

      $ID = $_GET['id'];
    $query = "SELECT ID,imagename FROM uploads WHERE ID IN ({$ID}) AND username = '{$uploader}'";
    $result = mysql_query($query);

      while( $iName = mysql_fetch_array($result)){
          $querys = "DELETE FROM uploads WHERE ID IN ({$ID}) AND username = '{$uploader}'";
      $results = mysql_query($querys);

          if(!$results) {
       $api_message = 'Failed to get a Removal result';
       } else {

            $iName[imagename] = array($iName[imagename]);
            $name = implode(",", $iName[imagename]);

           $api_message = "You removed image(s) $name"; 

          }
}

输出 :

您删除了图片 BzlXO.jpg

但我需要它是:

输出:您删除了图像“BzlXO.jpg,cHZTk.jpg,9yset.jpg,Ss04V.jpg”

对此的任何帮助将不胜感激,如果需要更多信息,请告诉我,我将包括在内谢谢

4

3 回答 3

2

也许您需要填充循环之前声明的数组,如下所示:

$images = array();
while( $iName = mysql_fetch_array($result)) { 
  ...
  $images[] = $iName['imagename']; # pushing the deleted image into that array
}
...
$names = implode(', ', $images);

而且我强烈建议至少检查使用mysqli函数集的可能性(或PDO,在我看来这甚至更好,但对于您的代码库来说可能是一个太大的步骤)。)

于 2012-07-06T23:46:44.480 回答
2

除了raina77ow 发布的解决方案之外,控制流也存在问题,因为您正在DELETE FROM uploads WHERE ID IN (...)为循环的每次迭代执行语句while。这将删除第一次迭代的所有记录。您需要将其更改为:

$ID = $_GET['id'];
$names = array();
$query = "SELECT ID,imagename FROM uploads WHERE ID IN ({$ID}) AND username = '{$uploader}'";
$result = mysql_query($query);

  while( $iName = mysql_fetch_array($result)){
      $querys = "DELETE FROM uploads WHERE ID = {$iName['ID']} AND username = '{$uploader}'";
      $results = mysql_query($querys);
      if(!$results) {
       $api_message = 'Failed to get a Removal result';
      } else {
        $names[] = $iName['imagename']);
      }
  }
  $name = implode(",", $names);
  $api_message = "You removed image(s) $name"; 
于 2012-07-07T00:00:34.097 回答
1

您应该修改您的代码以保护它免受无效值的影响并减少失败的可能性。

至于您的具体问题,无需将一个数组中的值放在另一个数组中,只是为了将其内爆为字符串。您可以简单地使用字符串连接来继续添加所需的值。

这个建议解决了你的问题: 代码注释是为了解释发生了什么

// initialize the user message with the success string
$api_message = "You removed image(s): ";

// check if the URL variable exists and contains values
if (isset($_GET['id']) && $_GET['id']!='') {

    // clean the values a litle bit to prevent code injection
    $ID = strip_tags(trim($_GET['id']));

    // prepare the query (more compatible string concatenation)
    $query = "SELECT ID, imagename FROM uploads WHERE ID IN (".$ID.") AND username = '".$uploader."'";

    // query the databse
    $result = mysql_query($query);

    // check for results
    if (is_resource($result) && mysql_num_rows($result)>=1) {

        // get total records
        $counter = mysql_num_rows($result);

        // run by each result found
        while($iName = mysql_fetch_array($result)) {

            // delete all from the database
            $querys = "DELETE FROM uploads WHERE ID IN (".$ID.") AND username = '".$uploader."'";
            $results = mysql_query($querys);

            // check if the returned result is false
            if (!$results) {

                // replace the user message with a fail information
                $api_message = 'Failed to get a Removal result';

            } else {

                // decrease the counter
                $counter--;

                // update the user message and add a "," if this is not the last name
                $api_message.= $iName['imagename'] . (($counter==0)?(''):(', '));

            }
        }

    } else {
        $api_message = 'ups... failed to talk with the database';
    }

} else {
    $api_message = 'ups... no ids collected';
}

相关阅读:

  • PHP strip_tags

    strip_tags — 从字符串中去除 HTML 和 PHP 标记

  • PHP 修剪

    trim — 从字符串的开头和结尾去除空格(或其他字符)

  • PHP is_resource

    is_resource — 查找变量是否为资源

于 2012-07-07T00:15:44.017 回答