好的,这是我的问题,我试图在网页上测试用户身份验证(注册和登录),并且成功,用户登录并输入一个事物/主题,然后显示在 html 页面上,两个表涉及。一个用户有三列(id、用户名、密码)第二个“列表”有 7 列(id、时间、time_eidited)等。它看起来像下图。 现在我想要的是在网络表中显示另一列,该列显示输入该帖子/主题的用户名..就像我(Hassan)输入一个主题让我说 war 我希望它在最后也显示我的名字一个名为“作者”的列。我一直在想,但我并没有想到我应该如何做到这一点。是否在“列表”表中创建一列或什么???任何帮助将不胜感激。
好的,这是页面代码:
home.php
<html>
<head>
<title>My first PHP website</title>
</head>
<?php
session_start(); //starts the session
if($_SESSION['user']){ //checks if user is logged in
}
else{
header("location:index.php"); // redirects if user is not logged in
}
$user = $_SESSION['user']; //assigns user value
?>
<body>
<h2>Home Page</h2>
<p>Hello <?php Print "$user"?>!</p> <!--Displays user's name-->
<a href="logout.php">Click here to logout</a><br/><br/>
<form action="add.php" method="POST">
Add more to list: <input type="text" name="details"/><br/>
public post? <input type="checkbox" name="public[]" value="yes"/><br/>
<input type="submit" value="Add to list"/>
</form>
<h2 align="center">My list</h2>
<table border="1px" width="100%">
<tr>
<th>Id</th>
<th>Details</th>
<th>Post Time</th>
<th>Edit Time</th>
<th>Edit</th>
<th>Delete</th>
<th>Public Post</th>
</tr>
<?php
mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
mysql_select_db("first_db") or die("Cannot connect to database"); //connect to database
$query = mysql_query("Select * from list"); // SQL Query
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print '<td align="center">'. $row['id'] . "</td>";
Print '<td align="center">'. $row['details'] . "</td>";
Print '<td align="center">'. $row['date_posted']. " - ". $row['time_posted']."</td>";
Print '<td align="center">'. $row['date_edited']. " - ". $row['time_edited']. "</td>";
Print '<td align="center"><a href="edit.php?id='. $row['id'] .'">edit</a> </td>';
Print '<td align="center"><a href="#" onclick="myFunction('.$row['id'].')">delete</a> </td>';
Print '<td align="center">'. $row['public']. "</td>";
Print "</tr>";
}
?>
</table>
<script>
function myFunction(id)
{
var r=confirm("Are you sure you want to delete this record?");
if (r==true)
{
window.location.assign("delete.php?id=" + id);
}
}
</script>
</body>
add.php
<?php
session_start();
if($_SESSION['user']){
}
else{
header("location:index.php");
}
if($_SERVER['REQUEST_METHOD'] = "POST") //Added an if to keep the page secured
{
$details = mysql_real_escape_string($_POST['details']);
$time = strftime("%X");//time
$date = strftime("%B %d, %Y");//date
$decision ="no";
mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
foreach($_POST['public'] as $each_check) //gets the data from the checkbox
{
if($each_check !=null ){ //checks if the checkbox is checked
$decision = "yes"; //sets teh value
}
}
mysql_query("INSERT INTO list (details, date_posted, time_posted, public) VALUES ('$details','$date','$time','$decision')"); //SQL query
header("location: home.php");
}
else
{
header("location:home.php"); //redirects back to hom
}
?>
最佳答案
在你的“列表”表中添加一列名为“作者”并使用以下代码: 在这里,我从 session 中获取作者姓名。
<?php
session_start();
if($_SESSION['user']){
}
else{
header("location:index.php");
}
if($_SERVER['REQUEST_METHOD'] = "POST") //Added an if to keep the page secured
{
$author=$_SESSION['user'];// here you are getting the author . as the person who is logged will add a row to the list
$details = mysql_real_escape_string($_POST['details']);
$time = strftime("%X");//time
$date = strftime("%B %d, %Y");//date
$decision ="no";
mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
foreach($_POST['public'] as $each_check) //gets the data from the checkbox
{
if($each_check !=null ){ //checks if the checkbox is checked
$decision = "yes"; //sets teh value
}
}
mysql_query("INSERT INTO list (details, date_posted, time_posted, public,author) VALUES ('$details','$date','$time','$decision','$author')"); //SQL query
header("location: home.php");
}
else
{
header("location:home.php"); //redirects back to hom
}
?>
关于php - MySQL 查询,它可以让我在我的 html 表中显示列表的作者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31775322/