douchu5131 2014-05-02 13:21
浏览 26
已采纳

添加MySQL查询的链接[关闭]

Hi I'm new to PHP & MySQL and I am trying to write a journal page for an assignment, but I am not sure how to link a query to another page that shows the full query that has been clicked. Basically so the user can access their previous journal entries. below is the code I have written so far:

<?php
session_start();
if(isset($_SESSION['uname'])){
echo "Welcome " . $_SESSION['uname'];
}
require_once 'PHP/Constants.php';
$conn = new MySQLi(DB_SERVER, DB_USER, DB_PASSWORD,DB_NAME) or die ('There was a problem connecting to the database');
$uname = $_SESSION['uname'];
$query="SELECT Username, Date_Created, Title FROM journal";
$result=mysqli_query($conn, $query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Journal</title>
</head>

<body>
<p>
<h1>My Journal</h1>
</p>
    <?php
        echo "<table width='400' border='1' cellspacing='0' cellpadding='3'>
            <tr>
            <th>Date</th>
            <th>Title</th>
            </tr>";
            while ($rows=mysqli_fetch_array($result)){
                if($rows['Username']=$uname) {
                    echo "<tr>";
                    echo "<td>" . $rows['Date_Created'] . "</td>";
                    echo "<td>" . $rows['Title'] . "</td>";
                    echo "</tr>"; 
                } else echo "No Journal Entries Found";
            }
        echo "</table>";
    ?>
    </table>    
<form method="post" action="new_entry.php">
    <p>
        <input type="submit" id="new_entry" value="New Entry" name="new_entry" />
    </p>
</form>
<form method="post" action="choices.php">
    <p>
        <input type="submit" id="back" value="Back" name="back" />
    </p>
</form>
</body>
</html>

Thank you in advance for any help - Nick

  • 写回答

1条回答 默认 最新

  • doufei4418 2014-05-02 13:52
    关注

    First of all you have to have an ID or an unique column in your table, in this case let's say that UserName is unique. So you print your link to another php file and send that parameter by GET:

    while ($rows=mysqli_fetch_array($result)){
        if($rows['Username']==$uname) {
            echo "<tr>";
            echo "<td>" . $rows['Date_Created'] . "</td>";
            echo "<td>" . $rows['Title'] . "</td>";
            echo "<td><a href='info.php?username=".$rows['Username']."'>Link</a></td>";
            echo "</tr>"; 
        } else 
             echo "No Journal Entries Found";
        }
    }
    

    Now in info.php, you recive the value and query the database using the given username:

    <?php
    $username = $_GET['username'];
    // Escape value to prevent SQL injection
    ...
    $query="SELECT * FROM journal WHERE UserName = '".$username."'";
    // And then display the info as you want
    ...
    

    Hope this help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题