如何使名称变量可点击(mysqli)?

时间:2013-08-01 20:53:19

标签: php html mysql mysqli

我是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();
?>  

2 个答案:

答案 0 :(得分: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'])

答案 1 :(得分: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>";