douqiao4450 2012-03-11 15:34
浏览 23
已采纳

将ID发送到PHP脚本并从DB文件名返回img src标记

I am trying to do something easy I am sure but I have looked and tested and I am not doing something right. I have a DB that stores the image file name, I need to get the file name based on the ID in the HTML , let me explain:

 <div class="slide">
                    <div class="image-holder">
                       <img src="img/asoft_table.jpg" alt="" /> 
                    </div>
                    <div class="info">

                        <p>Morbi a tellus lorem, id scelerisque ligula. Maecenas vitae turpis et.</p>
                    </div>
                </div>
                <div class="slide">
                    <div class="image-holder">
                        <img src="img/soft_table.jpg" alt="" />
                    </div>
                    <div class="info">

                        <p>Sed semper, lorem ac lobortis bibendum, magna neque varius augue, vel accumsan.</p>
                    </div>
                </div>
                <div class="slide">
                    <div class="image-holder">
                        <img src="img/living_room2.jpg" alt="" />
                    </div>

in each instance of an img tag, I need to insert the filename from the DB. so, first image tag would be primary key 1, second primary key 2 and so forth. Here is the PHP script I am using to retrieve the filename, which works, but I am unsure how to send the ID of the image to the script and then return it properly.

 <?php
 $hote = 'localhost';
 $base = '*****';
 $user = '*****';
 $pass = '*****';
 $cnx = mysql_connect ($hote, $user, $pass) or die(mysql_error ());
 $ret = mysql_select_db ($base) or die (mysql_error ());
 $image_id = mysql_real_escape_string($_GET['ID']);
 $sql = "SELECT image FROM image_upload WHERE ID ='$image_id'";
 $result = mysql_query($sql);
 $image = mysql_result($result, 0);

 header('Content-Type: text/html');
 echo '<img src="' . $image . '"/>';
 exit;


 ?>

any help would be appreciated, thanks a heap

  • 写回答

3条回答 默认 最新

  • doushou1298 2012-03-11 15:48
    关注

    From what it looks your trying too hard to separate the PHP from HTML.

    // File: index.php
    <?php
        $hote = 'localhost';
        $base = '*****';
        $user = '*****';
        $pass = '*****';
        $cnx = mysql_connect ($hote, $user, $pass) or die(mysql_error ());
        $ret = mysql_select_db ($base) or die (mysql_error ());
        $image_id = mysql_real_escape_string($_GET['ID']);
        $sql = "SELECT image FROM image_upload WHERE ID ='$image_id'";
        $result = mysql_query($sql);
        //$image = mysql_result($result, 0);
    
        $image = array();
    
        while ($row = mysql_fetch_assoc($result)) {
            $image[] = $row["image"];
        }
    ?>
    <html>
        <head>
        </head>
        <body>
            <div class="slide">
                <div class="image-holder">
                    <img src="<?php echo $image[0];?>" alt="" /> 
                </div>
                <div class="info">
                    <p>Morbi a tellus lorem, id scelerisque ligula. Maecenas vitae turpis et.</p>
                </div>
            </div>
            <div class="slide">
                <div class="image-holder">
                    <img src="<?php echo $image[1];?>" alt="" />
                </div>
            <div class="info">
                <p>Sed semper, lorem ac lobortis bibendum, magna neque varius augue, vel accumsan.</p>
            </div>
        </div>
    <div class="slide">
        <div class="image-holder">
            <img src="<?php echo $image[2];?>" alt="" />
        </div>
    </body>
    </html>
    
    
    // This is the magic code to get all the rows out of the database :)
    // $row[ field_name ];
    while ($row = mysql_fetch_assoc($result)) {
        $image[]  $row["image"];
    }
    



    Edit:
    I'm not sure if this is what your trying to accomplish, but thought I'd share anyway.

    $imageID1 = $_GET['id1'];
    $imageID2 = $_GET['id2'];
    $imageID3 = $_GET['id3'];
    
    $sql = "SELECT image FROM image_upload ";
    $sql = "WHERE ID = $imageID1 OR ID = $imageID2 OR ID = $imageID3";
    
    //The rest of your code can remain the same.
    

    Or if one id relates to 3 images.

        $sql = "SELECT * FROM image_upload WHERE ID ='$image_id'";
        $result = mysql_query($sql);
    
        $image = array();
    
        $row = mysql_fetch_assoc($result);
    
        $image1 =  $row["image1"];
        $image2 =  $row["image2"];
        $image3 =  $row["image3"];
    

    I've you give me more info on what your trying to do, I'd be happy to give you a better example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)