使用生成的按钮从数据库输出所有图像

时间:2014-10-21 13:42:44

标签: php html mysql

自从我开始自学php,html和mysql以来已经有一个月了。我正在为我学校的某个学生组织建立一个简单的php网站。 首先,我已在这里搜索过,这就是我得到的:Delete button for each table row

这个问题对我来说是完全一样的,但我真的被困在这里。我想要的是我桌上的所有图片都会显示在我的页面中。列然后会有一个'生成'内部有查询的按钮。这不完全是我在代码中要做的事情,但这对我来说是理解所有过程的一个很好的开始。

以下是我在页面中的代码,其中所有这些都将发生: 注意:我正在使用twitter bootstrap。我不知道引导程序是如何工作的,但我只知道如何编辑基本的php,html,jquery,javascript,css。只是基础:)

<?php
    include 'img2db_pdo.class.php';
    require("common.php");

    if(empty($_SESSION['user']))
    {
        header("Location: http://localhost/se/index.php");
        die("Redirecting to login.php");
    }
    else
    {
        //This following codes are for checking the session in DB
        $query = "
            SELECT
                id,
                password,
                emailAddress,
                membership
            FROM memberlist
            WHERE
                session = :var_val
        ";
        $query_params = array(
           ':var_val' => 'True'
        );      

        try
        {

            $stmt = $db->prepare($query);
            $result = $stmt->execute($query_params);

        }
        catch(PDOException $ex)
        {
            die("Failed to run query: " . $ex->getMessage());
        }
        $row = $stmt->fetch();



        if ( $row['membership'] == 'Officer'  || $row['membership'] == 'Member' )
        {
            header("Location: http://localhost/memberdir/index.php");
        }

    }      
?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset = "UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Annyeong Tomasino</title>
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <link href="custom.css" rel="stylesheet">
        <link href="jumbotron.css" rel="stylesheet">
        <link href="carousel.css" rel="stylesheet">
        <link rel="stylesheet" href="css/pikaday.css"> 
    </head>

    <body>

    <?php
    if (isset($_GET['message'])){
    $message2 = $_SESSION[$_GET['message']];
    echo "<script type='text/javascript'>alert(message2);</script>";
    }
    ?>
        <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <div class="container">
            <div class="menubar">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#"><font color="#ffffff">Annyeong Tomasino</font></a>
                </div>
                </div>

                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="index.php">Home</a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Admin<b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    <li><a href="#myModal" data-toggle="modal">Add a Member</a></li>
                                    <li><a href="memberlist.php">Member List</a></li>
                                    <li><a href="event.php">Create Event</a></li>
                                </ul>
                        </li>

                        <li> <a href="http://hellotomasino.weebly.com/">AT Website</a></li>

                        <li><a href="http://osa.ust.edu.ph/" target="_blank">Office for Student Affairs</a></li>
                        <form class="navbar-form navbar-right" role="form" name="form1" method="post" action="logout.php">

                        <button type="submit" class="btn btn-danger" name = "logout" >Log out</button>
                        </form>




                </div><!--/.navbar-collapse -->
            </div>
        </div>

        <!-- Main jumbotron for a primary marketing message or call to action -->
        <div class="jumbotron">
            <div class="container text-center">
                <h2>Events List Page</h2>
                <p>See all the events you have created.</p>

            </div>
        </div>


            <!-- Marketing messaging and featurettes
    ================================================== -->
    <!-- Wrap the rest of the page in another container to center all the content. -->


            <div class="container marketing">
            <!-- Three columns of text below the carousel -->

            <div class="row text-center">
                <div class="col-lg-4">
                    <p>
                        <?php
                            $query = "SELECT * FROM ImageTable WHERE ImageId = :id";
                            $query_params = array( ':id' => 1 );        
                            $stmt = $db->prepare($query);
                            $result = $stmt->execute($query_params);
                            $row = $stmt->fetch();  
                            $img = $row['ImageFile'];
                            echo '<img height="200" width="200" src="data:image/jpeg;base64,'.base64_encode( $img ).'"/>';
                        ?>
                    </p>
                </div>

            <div class="row text-center">
                <div class="col-lg-4">
                    <p>
                        <?php
                            $query = "SELECT * FROM ImageTable WHERE ImageId = :id";
                            $query_params = array( ':id' => 2 );        
                            $stmt = $db->prepare($query);
                            $result = $stmt->execute($query_params);
                            $row = $stmt->fetch();  
                            $img = $row['ImageFile'];
                            echo '<img height="200" width="200" src="data:image/jpeg;base64,'.base64_encode( $img ).'"/>';
                        ?>
                    </p>
                </div>


            <div class="row text-center">
                <div class="col-lg-4">
                    <p>
                        <?php
                            $query = "SELECT * FROM ImageTable WHERE ImageId = :id";
                            $query_params = array( ':id' => 3 );        
                            $stmt = $db->prepare($query);
                            $result = $stmt->execute($query_params);
                            $row = $stmt->fetch();  
                            $img = $row['ImageFile'];
                            echo '<img height="200" width="200" src="data:image/jpeg;base64,'.base64_encode( $img ).'"/>';
                        ?>
                    </p>
                </div>

            </div>


        </div> <!-- /container -->


        <!-- Bootstrap core JavaScript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script src="../../dist/js/bootstrap.min.js"></script>
        <script src="../../assets/js/docs.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
        <link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

    </body>
</html>

这是我到目前为止的输出: enter image description here

3 个答案:

答案 0 :(得分:0)

我将在这里解释一个非常简单的技巧:

  1. 你需要创建一个单独的页面,可以接受来自数据库的记录id,然后它可以进行MYSQL查询从数据库中删除该id,假设这个文件名为“process.php”
  2. 您需要为每个图片添加一个带href的链接,如下所示:

    HREF = “process.php ID =:ID”

  3. 顺便说一下,我觉得你学习编码真的很棒,虽然你的代码需要很多好的练习,但这需要时间,不断滚动:)

答案 1 :(得分:0)

我以某种方式通过使用MANUAL输出lol设法显示所有图像。我认为这是手动的。 我计算了图像表中的行数。 将该计数用于我的while循环。

现在,我只需要一个按钮,我相信这对我来说是下一个重大挑战。哈哈。

<?php
    include 'img2db_pdo.class.php';
    require("common.php");

    if(empty($_SESSION['user']))
    {
        header("Location: http://localhost/se/index.php");
        die("Redirecting to login.php");
    }
    else
    {
        //This following codes are for checking the session in DB
        $query = "
            SELECT
                id,
                password,
                emailAddress,
                membership
            FROM memberlist
            WHERE
                session = :var_val
        ";
        $query_params = array(
           ':var_val' => 'True'
        );      

        try
        {

            $stmt = $db->prepare($query);
            $result = $stmt->execute($query_params);

        }
        catch(PDOException $ex)
        {
            die("Failed to run query: " . $ex->getMessage());
        }
        $row = $stmt->fetch();



        if ( $row['membership'] == 'Officer'  || $row['membership'] == 'Member' )
        {
            header("Location: http://localhost/memberdir/index.php");
        }

    }      
?>
<!doctype html>
<html lang="en">
    <head>
        <meta charset = "UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Annyeong Tomasino</title>
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <link href="custom.css" rel="stylesheet">
        <link href="jumbotron.css" rel="stylesheet">
        <link href="carousel.css" rel="stylesheet">
        <link rel="stylesheet" href="css/pikaday.css"> 
    </head>

    <body>
    <style>
        th.padding {
        padding-right: 300px;
        padding-left: 300px;
        }
        td.padding {
        padding-right: 300px;
        padding-left: 300px;
        }
    </style>
    <?php
    if (isset($_GET['message'])){
    $message2 = $_SESSION[$_GET['message']];
    echo "<script type='text/javascript'>alert(message2);</script>";
    }
    ?>
        <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <div class="container">
            <div class="menubar">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#"><font color="#ffffff">Annyeong Tomasino</font></a>
                </div>
                </div>

                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="index.php">Home</a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Admin<b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    <li><a href="#myModal" data-toggle="modal">Add a Member</a></li>
                                    <li><a href="memberlist.php">Member List</a></li>
                                    <li><a href="event.php">Create Event</a></li>
                                </ul>
                        </li>

                        <li> <a href="http://hellotomasino.weebly.com/">AT Website</a></li>

                        <li><a href="http://osa.ust.edu.ph/" target="_blank">Office for Student Affairs</a></li>
                        <form class="navbar-form navbar-right" role="form" name="form1" method="post" action="logout.php">

                        <button type="submit" class="btn btn-danger" name = "logout" >Log out</button>
                        </form>




                </div><!--/.navbar-collapse -->
            </div>
        </div>

        <!-- Main jumbotron for a primary marketing message or call to action -->
        <div class="jumbotron">
            <div class="container text-center">
                <h2>Events List Page</h2>
                <p>See all the events you have created.</p>

            </div>
        </div>


            <!-- Marketing messaging and featurettes
    ================================================== -->
    <!-- Wrap the rest of the page in another container to center all the content. -->


            <div class="container marketing">
            <!-- Three columns of text below the carousel -->
            <!-- LALALA
            <div class="row text-center">
                <div class="col-lg-4">
                    <p>
                        <?php
                            $query = "SELECT * FROM ImageTable WHERE ImageId = :id";
                            $query_params = array( ':id' => 1 );        
                            $stmt = $db->prepare($query);
                            $result = $stmt->execute($query_params);
                            $row = $stmt->fetch();  
                            $img = $row['ImageFile'];
                            echo '<img height="200" width="200" src="data:image/jpeg;base64,'.base64_encode( $img ).'"/>';
                        ?>
                    </p>
                </div>


            </div>

            -->
            <?php
                $query = "select count(ImageId) as count from ImageTable";
                $query_params = array( );        
                $stmt = $db->prepare($query);
                $result = $stmt->execute($query_params);
                $row = $stmt->fetch();              
                $numOfRows = $row['count'];
                //echo $row['count'];   

                $space = 0;
                $inc = 0;
            ?>
        <center>
            <?php
                while($numOfRows != 0)
                {
                    if ( $space == 4)
                    {
                        $space = 0;
                        echo '<br><br><br><br><br><br><br><br>';
                    }
                    $inc++;
                    $query = "SELECT * FROM ImageTable WHERE ImageId = :id";
                    $query_params = array( 'id' => $inc);        
                    $stmt = $db->prepare($query);
                    $result = $stmt->execute($query_params);
                    $row = $stmt->fetch();  
                    $img = $row['ImageFile'];
                    echo '&nbsp&nbsp&nbsp'.'<img height="200" width="200" src="data:image/jpeg;base64,'.base64_encode( $img ).'"/>'.'&nbsp&nbsp&nbsp';
                    $space++;
                    $numOfRows--;
                }
            ?>
        </center>

        </div> <!-- /container -->


        <!-- Bootstrap core JavaScript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script src="../../dist/js/bootstrap.min.js"></script>
        <script src="../../assets/js/docs.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
        <link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

    </body>
</html>

答案 2 :(得分:0)

你必须有一个重定向到php文件的按钮,该文件从数据库中删除特定的id ...

<button href="delete.php?id=imageid"></button>

你的delete.php页面应该是

<?php

//Execute the Query
$query = "DELETE FROM table WHERE id = '".$_GET['id']."' ";
$stmt = $db->prepare($query);
$result = $stmt->execute();

//Go back to the original page
@header('Location : home.php');