gpt4 book ai didi

php - 将用户名转换为用户 ID 以输入评论框

转载 作者:行者123 更新时间:2023-11-29 19:05:41 24 4
gpt4 key购买 nike

<?php
include("connection.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form

$myusername = mysqli_real_escape_string($conn,$_POST['username']);
$mypassword = mysqli_real_escape_string($conn,$_POST['password']);
$row['userID'] = $myuserid;


$sql = "SELECT * FROM u803621131_login.users WHERE username = '$myusername' and password = '$mypassword'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['active'];

$count = mysqli_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count == 1) {
session_start("myuserid");


$_SESSION['login_user'] = $myusername;
$_SESSION['login_id'] = $myuserid;


header("location: welcome.php");
}else {
$error = "Your Login Name or Password is invalid";
}
}
?>
<html>

<head>
<title>Login Page</title>

<style type = "text/css">
body {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}

label {
font-weight:bold;
width:100px;
font-size:14px;
}

.box {
border:#666666 solid 1px;
}
</style>

</head>

<body bgcolor = "#FFFFFF">

<div align = "center">
<div style = "width:300px; border: solid 1px #333333; " align = "left">
<div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>

<div style = "margin:30px">

<form action = "" method = "post">
<label>UserName :</label><input type = "text" name = "username" class = "box"/><br /><br />
<label>Password :</label><input type = "password" name = "password" class = "box" /><br/><br />
<input type = "submit" value = " Submit "/><br />
</form>

<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>

</div>

</div>

</div>

</body>
</html>

Login.php - 包含所有更改部分的登录页面,实际登录正常工作。虽然很难判断是否还有其他问题

<?php session_start();
include'../../connection.php';?>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="keywords" content="">
<link rel="stylesheet" type="text/css" href=".../../../../style.css">
<title>Home</title>

<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<?php include('../../main/main.php');?>
</head>
<body>



<div class=containermain>
<h1>I5-6600k.php</h1>
<form action="ratepost.php" method="post">

<label for="rating">rating:</label>
<select name="rating" id="rating" value="rating" >
<option>
<option value="1">1 </option>
<option value="2">2</option>
<option value="3">3 </option>
<option value="4">4</option>
<option value="5">5</option>
</option>
</select>
<input type="submit" value="Submit">
</form>



<h2>graphics card write up................</h2>
<?php echo "Hello " . $_SESSION['user']; ?>
<p>&nbsp;</p>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>

<div
class="fb-like"
data-share="true"
data-width="450"
data-show-faces="true">
</div>

<!---------------------------------------COMMENT BOX---------------------------------------------------->

<div class="comments" align="center">
<form action="" method="post" >
<textarea rows="4" cols="50" name="comment">
Please type a comment if you are logged in....


</textarea>
<input type="submit" value="Submit">
</form>


<?php

if (isset($_SESSION['login_id']) && !empty($_SESSION['login_id'])) {
$id = $_SESSION['login_id'];
$sqlinsert = "INSERT INTO comment (userID, comment, dCpuID) VALUES ('$id', '$comment', '1')";
if(mysqli_query($conn, $sqlinsert)){


header("Location: i5-6600k");
} else {
echo "ERROR: Could not able to execute $sqlinsert. " . mysqli_error($conn);
}
}

// close connection










$sql = "SELECT `users`.`username`, `comment`.`comment`, `comment`.`timestamp`\n"

. "FROM `users`\n"

. "LEFT JOIN `comment` ON `users`.`userID` = `comment`.`userID` \n"

. "where dCpuID = 1";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table><tr><th>Username</th><th>Comment</th><th>Timestamp</th>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["username"]. "</td><td>" . $row["comment"]."</td><td>" . $row["timestamp"]. "</td>";
}
echo "</table>";
} else {
echo "0 results";
}


?>
</div>
<?php include('../../assets/footer.php');?>

<div class="fb-comments" data-href="http://www.computercomparison.tk/#home" data-numposts="5"></div>
</body>
</html>

已包含第二页的全部内容,以防可能与网站中可能指出的其他代码部分发生冲突。

此外,您还会在奇怪的地方发现很多代码,仅在 mo 上测试位。

<?php
include('connection.php');

session_start();

$user_check = $_SESSION['login_user'];

$ses_sql = mysqli_query($conn,"select username, from users where username = '$user_check' ");


$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

$login_session = $row['username'];


if(!isset($_SESSION['login_user'])){
header("location:login.php");


}
?>

有这个 session.php 文件,不认为它太相关,但是改变它确实影响了登录和其他东西,它在这里状况良好,想知道这里是否还有什么我需要改变的?它链接到welcome.php

最佳答案

出现错误消息后,您使用外键将评论作者 ID 的列连接到帐户表中的列。

如你的图片所示,他们都是 INT。但您正尝试将 VARCHAR(用户名)插入此列。

我的方法是通过 sql 查询获取用户的 ID,或者更好地将用户 ID 保存到 session 中:

session_start();
$_SESSION['login_user'] = $usernameFromFormOrWhatever;
$_SESSION['login_id'] = $usersID;

因此您可以用它填充您的 userID 列:

$id = $_SESSION['login_id'];
$sqlinsert = "INSERT INTO comment (userID, comment, dCpuID) VALUES ('$id', '$comment', '1')";

此外,在评论表中输入的 ID 还必须作为用户 ID 显示在帐户表的一行中。否则您将收到像现在一样的错误消息。

关于php - 将用户名转换为用户 ID 以输入评论框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43532180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com