doutui8842
2016-05-17 07:57
浏览 27
已采纳

在另一个php文件中获取html标记的id

I have a database table Library and I have fetched image src from the table and gave the name of the image tag as id from the table as name=".$idRow.".

So what i want to do is that when the user clicks on an image id of the image from the table should be passed in the name of that image tag, which is successfully done.

But when i pass that id through a variable using session ($_SESSION['sessionBookId']=$idRow;) to another PHP file only the last id of the book is passed through that variable.

I want to pass the id of the image clicked but only the last id is passed. Is there a solution?

<?php    
    $sqlSelectImage = "SELECT * FROM Library WHERE 1";
    $queryExec = mysqli_query($link, $sqlSelectImage);

    while ($rs = mysqli_fetch_object($queryExec)) {
        $idRow=stripcslashes($rs->id);

?>
<tr>
    //image tag
    <td align="center">
    <?php 
        echo "<a href=".'book-view.php'." name=".$idRow.">";
        $_SESSION['sessionBookId']=$idRow;
    ?>
        <img src="<?php echo stripcslashes($rs->book_url); ?>" alt="" width="100" height="100" />
    <?php echo"</a>" ?>
    </td>
    <td align="center">
        <form action="" method="POST"><input type="submit" value="buy" name="bookView"/>
        </form>
    </td>
</tr>

<?php
    if (isset($_POST['bookView'])) {
        header("Location:book-view.php");
        exit();
    }
?>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douhuilin1152 2016-05-17 08:07
    已采纳

    Use this code

    <td align="center"><?php echo "<a href=\"book-view.php?id=$idRow\">";?><img src="<?php echo stripcslashes($rs->book_url); ?>" alt="" width="100" height="100" /></a></td>
    

    in the book-view.php you can access to passes value with

    $_GET['id']
    

    in the way you can pass variable with GET

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doufangzhang4454 2016-05-17 08:13
    <td align="center"><?php echo "<a href=".'book-view.php'." name=".$idRow.">"; $_SESSION['sessionBookId']=$idRow;?><img src="<?php echo stripcslashes($rs->book_url); ?>" alt="" width="100" height="100" /><?php echo"</a>" ?></td>
    

    In your pass url link the $idRow is assignd to $_SESSION['sessionBookId'] what you should do is you should equal the book id to some name to GET in the page of book-view.php

    right method is

    <td align="center"><?php echo "<a href=".'book-view.php?'."id=".$idRow.">"; ?><img src="<?php echo stripcslashes($rs->book_url); ?>" alt="" width="100" height="100" /><?php echo"</a>" ?></td>
    

    You can capture the value of id by $_GET['id'];

    Use mysql_real_escape_string() while capture the value for id mysql_real_escape_string($_GET['id']);

    check the manual for mysql_real_escape_string.

    Instead of id you can use whatever_name but when you GET the value please use like $_GET['whatever_name ']

    I just explain it will help some one in future.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题