我今天在这里是因为我处于一个小问题上,我似乎无法解决,这就是为什么它会带来这里。 问题是我正在尝试从$ _GET变量中获取多个ID来打印出来。我试图使用数组和伟大,它的工作,但事情是它使[0]单独的数组,我需要它作为一个在同一个数组而不是array()array()这就是它的作用。
所以我在mysql_fetch_array中使用while()来从数据库中获取结果,它依赖于url中的$ _GET来获取正确的id。因此,在我传递给api.php?do = remove& id = 1,4,7的URL中,它只打印出第一个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]);
这是我的代码:
这是网址“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";
}
}
OUTPUT:
您删除了图片BzlXO.jpg
但我需要它:
输出: 您删除了图片“BzlXO.jpg,cHZTk.jpg,9yset.jpg,Ss04V.jpg”
如果需要更多信息,我们将非常感谢您的帮助,请告知我们,我将包括在内 谢谢
答案 0 :(得分:2)
也许您需要填充在循环之前声明的数组,如:
$images = array();
while( $iName = mysql_fetch_array($result)) {
...
$images[] = $iName['imagename']; # pushing the deleted image into that array
}
...
$names = implode(', ', $images);
答案 1 :(得分: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";
答案 2 :(得分:1)
您应该修改代码,以保护代码免受无效值的影响,并降低失败的可能性。
至于你的具体问题,没有必要将一个数组中的值放在另一个数组中,只是为了将其插入到字符串中。您只需使用string concatenation即可添加所需的值。
此建议解决了您的问题: 代码注释用于解释发生的事情
// 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';
}
相关阅读:
strip_tags - 从字符串中删除HTML和PHP标记
trim - 从字符串的开头和结尾去掉空格(或其他字符)
is_resource - 查找变量是否为资源