0

我是 php 和 mysqli 的初学者。我想编写一个程序,显示数据库表中的名称列表。允许用户单击名称,单击名称应显示该人的图片。

我的代码如下。我可以向数据库添加名称和人员并检索数据库中的所有名称,但我不知道如何使名称“可点击”,以便用户可以单击它们并显示图片。有人可以看看并给我一些提示吗?谢谢你的帮助!

请参阅下面的部分代码 - 这是我的主要 php 文件:

    //Turn on error reporting
    ini_set('display_errors', 'On');
    //Connects to the database
    $mysqli = new mysqli("blah blah", "username","pw", "blah");
    if($mysqli->connect_errno){
        echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }
?>  


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
        <script>$(document).ready(function() {$("form").validate();});</script>
        <title>CS302</title>
        <meta charset="UTF-8">
        <p></p>
    </head>
    <body>
        <div>This is a homework</div>
        <p></p>
        <div>
            <form method="post" action="index.php" enctype="multipart/form-data">
                Celebrity Name: <input type="text" name="c_name">
                Celebrity Photo: <input type="file" name="c_picture">
                <input type="submit" name="add" value="Upload">
            </form>
        </div>
        <br>
        <div>
        <table>
        <tr>
            <td>See below for a list of celebrities in our database</td>
        </tr>
        <?php
            //Display names in the celebrity database
            if(!($stmt = $mysqli->prepare("SELECT c_name FROM celebrity"))){
                echo "Prepare failed: "  . $stmt->errno . " " . $stmt->error;
            }

            if(!$stmt->execute()){
                echo "Execute failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
            }

            if(!$stmt->bind_result($c_name)){
                echo "Bind failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
            }

            while($stmt->fetch()){
                echo "<tr>\n<td>\n" . $c_name . "\n</td>\n</tr>";
            }

            $stmt->close();
        ?>
        </table>
        </div>
    </body>
</html>

这是用于检索图像的 php 文件:

    //Turn on error reporting
    ini_set('display_errors', 'On');
    //Connects to the database
    $mysqli = new mysqli("blah", "blah","blah", "blah");
    if($mysqli->connect_errno){
        echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }


    //retrieve the blob
    if(!($stmt = $mysqli->prepare("SELECT c_picture FROM celebrity where c_name = ?"))){
        echo "Prepare failed: "  . $stmt->errno . " " . $stmt->error;
    }

    if(!($stmt->bind_param("i",$_POST['c_id']))){
        echo "Bind failed: "  . $stmt->errno . " " . $stmt->error;
    }

    if(!$stmt->execute()){
        echo "Execute failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
    }

    if(!$stmt->bind_result($c_picture)){
        echo "Bind failed: "  . $mysqli->connect_errno . " " . $mysqli->connect_error;
    }

    $stmt->store_result();

    while($stmt->fetch()){
        echo "<tr>\n<td>\n" . $c_picture . "\n</td>\n</tr>";
    }

    $stmt->close();
?>  
4

2 回答 2

1

试试这个

$mysqli->prepare("SELECT c_id,c_name FROM celebrity")
// make sure your $c_id contains the id of that name record
while($stmt->fetch()){
                echo "<tr><td><a href='your_picture.php?c_id=".$c_id."'>" . $c_name . "</a></td></tr>";
            }

并在 your_picture.php 中使用$_GET['c_id']

$stmt->bind_param("i",$_GET['c_id'])
于 2013-08-01T21:02:12.167 回答
0

这取决于您在 c_picture 中存储的内容。如果要存储图像的 Web 位置字符串,则只需更改查询以返回 c_name 和 c_picture。

那么你的 php 可以使用这个:

echo "<tr>\n<td>\n<a href=\"".$c_picture."\">".$c_name."</a>\n</td>\n</tr>";
于 2013-08-01T21:01:56.243 回答