【问题标题】:PHP/MySQL Image array title changePHP/MySQL 图像数组标题更改
【发布时间】:2010-10-05 01:04:06
【问题描述】:

如果我有一些 php 代码,如下:

while($galleryResult=mysql_fetch_array($galleryQuery)) {                              
                echo '<li><img src="../../images/properties/gallery/'.$galleryResult['imagename'].'" width="120" height="120" class="image" /><br />                                                 
                      <input type="text" style="width:120px;" name="newGalleryTitle" value="'.$galleryResult['galleryTitle'].'"><br />
                      <label for="updateTitle'.$galleryResult['id'].'"><input id="updateTitle'.$galleryResult['id'].'" type="checkbox" name="title_ids[]" value="'.$galleryResult['id'].'" /> Update title?</label><br />
                      </li>    
                '. PHP_EOL;                        
            }

下面会输出这个html;

<li><img src="../../images/properties/gallery/image-name.jpg" width="120" height="120" class="image" /><br />                        
<input type="text" style="width:120px;" name="newGalleryTitle" value="Test No2"><br />
<label for="updateTitle1"><input id="updateTitle1" type="checkbox" name="title_ids[]" value="1" /> Update title?</label><br /></li>    

<li><img src="../../images/properties/gallery/image-name-02-jpg.jpg" width="120" height="120" class="image" /><br />                        
<input type="text" style="width:120px;" name="newGalleryTitle" value="Test No2"><br />
<label for="updateTitle2"><input id="updateTitle2" type="checkbox" name="title_ids[]" value="2" /> Update title?</label><br /></li>  

等等等等

这是我用来尝试更新图像“标题”的代码,如下所示:

if (!empty($_POST['title_ids'])) {  
        foreach ($_POST['title_ids'] as $titleId) {                    
            $error=$_POST['newGalleryTitle'];
            $gtsql=mysql_query("UPDATE isgallery SET galleryTitle = '".$_POST['newGalleryTitle']."' WHERE id = ".mysql_real_escape_string($titleId));                
              mysql_query($gtsql);                
         }
    }    

现在,如果我选择数组中的最后一个图像,这一切都很好,但是如果我选择任何其他或多个图像,它将从最后一个图像中获取图像标题并将其写入所选图像标题。

我的问题是:如何“获取”正确的图片标题?我知道它在数组中选择了正确的项目/图像,而不是图像标题。

希望这是有道理的,任何帮助将不胜感激。在此先感谢,S

【问题讨论】:

  • 你需要删除这个mysql_query($gtsql); (双重查询!) - 请告诉我 print_r($_POST['title_ids']);
  • 我认为他在那里不小心有第二个查询..

标签: php


【解决方案1】:

更换你的

while($galleryResult=mysql_fetch_array($galleryQuery)) {                              
    echo '<li><img src="../../images/properties/gallery/'.$galleryResult['imagename'].'" width="120" height="120" class="image" /><br />                                                 
        <input type="text" style="width:120px;" name="newGalleryTitle" 

用这个:

while($galleryResult=mysql_fetch_array($galleryQuery)) {                              
    echo '<li><img src="../../images/properties/gallery/'.$galleryResult['imagename'].'" width="120" height="120" class="image" />';
    echo '<input type="text" style="width:120px;" name="newGalleryTitle['. $galleryResult['id'] .']" value="'.$galleryResult['galleryTitle'].'">';
    echo '<label for="updateTitle'.$galleryResult['id'].'"><input id="updateTitle'.$galleryResult['id'].'" type="checkbox" name="title_ids[]" value="'.$galleryResult['id'].'" /> Update title?</label>';
    echo '</li>'. PHP_EOL;
}

然后,当您处理数据时,只需访问所需的值,如下所示:

foreach ($_POST['title_ids'] as $titleId) {                    
            $error=$_POST['newGalleryTitle'][$titleId];

是的 - 从循环中删除第二个 mysql_query,因为您已经拥有它一次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2015-09-03
    • 2022-12-26
    • 2013-09-14
    • 1970-01-01
    相关资源
    最近更新 更多