我今天来这里是因为我遇到了一个似乎无法解决的小问题,这就是它带来的原因。问题是,我试图从$_get变量中获取多个ID以打印出来。我试着使用array,它很好,但问题是它用[0]制作单独的数组,我需要它在同一个数组中,而不是array()array(,这就是它的作用。
因此,我将while()与mysql_fetch_array一起使用以从数据库中获取结果,这取决于url中的$_get,以便获取正确的id。那么在我传入的api.php的URL中呢?do=删除&id=1,4,7它将只打印第一个id,而不打印其他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"
如果需要任何帮助,我们将不胜感激,如果需要更多信息,请告诉我,我会包括感谢
除了raina77ow发布的解决方案外,控制流还有一个问题,即您正在为while
循环的每个迭代执行DELETE FROM uploads WHERE ID IN (...)
语句。这将删除第一次迭代中的所有记录。您需要将其更改为类似以下内容:
$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";
也许您需要填充在循环之前声明的数组,比如:
$images = array();
while( $iName = mysql_fetch_array($result)) {
...
$images[] = $iName['imagename']; # pushing the deleted image into that array
}
...
$names = implode(', ', $images);
我强烈建议至少检查一下使用mysqli函数集的可能性(或者PDO,在我看来这更好,但对你的代码库来说可能太大了)
您应该修改您的代码,以保护它不受无效值的影响,并降低失败的可能性。
对于您的特定问题,不需要将一个数组中的值放在另一个数组内,只需要将其分解为字符串即可。您可以简单地使用字符串串联来不断添加所需的值。
此建议修复了您的问题: 代码注释用于解释发生了什么
// 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——从字符串中剥离HTML和PHP标记
PHP微调
trim——从字符串的开头和结尾去掉空白(或其他字符)
PHP is_resource
is_resource—查找变量是否为资源