PHP / SQL在表单完成时存储当前登录用户的名称

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP / SQL在表单完成时存储当前登录用户的名称相关的知识,希望对你有一定的参考价值。

一旦用户登录我的网站,他们就可以填写一个表格,其中包含两个字段,“项目名称”和“项目描述”。

我需要帮助存储填写该表格的人的用户名。

例如,如果我以管理员身份登录并填写表单,则在数据库中应显示表单信息旁边的用户名Admin。

非常感谢帮助,并提前感谢您!

表格DB: 数据库名称:formystem 表名:表格 列我想将用户名保存到:form_user

我的代码(groupForm.php):

<?php
    session_start();
?>

<!DOCTYPE html>
<html>
  <head>
  <meta charset="UTF-8">
    <title></title>
     <link rel="stylesheet" href="./css/form.css">
     <link rel="stylesheet" href="./css/style.css">
  </head>
  <body>
  <header>
    <nav>
        <div class="main-wrapper">
        <div id="branding">
        <li><h1><span><a href="homepage.php">ProjectNet</a></span></li>
        </div>
            <div class="nav-login">
                <?php
                    if (isset($_SESSION['u_id'])) {
                        echo '<form action="includes/logout.inc.php" method="POST">
                              <button type="submit" name="submit">Logout</button>
                              </form>';
                    } else {
                        echo '<form action="includes/login.inc.php" method="POST"> 
                              <input type="text" name="uid" placeholder="Username/Email">
                              <input type="password" name="pwd" placeholder="Password">
                              <button type="submit" name="submit">Login</button>
                              </form>
                              <a href="signup.php">Sign up</a>';
                    }
                ?>
        </div>
    </nav>
    </header>
    <section id="showcase1">
<div class="container">  
  <form id="contact" action="includes/form_process.php" method="POST">
    <h3>Creating a Group</h3>
    <h4>Please fill out the sections below.</h4>
    <fieldset>
      <input placeholder="Project title" type="text" name="name">
    </fieldset>
    <fieldset>
      <textarea placeholder="Description of the project...." type="text" name="message" ></textarea>
    </fieldset>
    <fieldset>
    <button name="submit" type="submit">Create</button>
    </fieldset>
  </form>
</div>
</section>
  </body> 
</html>

后端代码(form_process.php):

<?php

session_start();

if (isset($_POST['submit'])) {

    function fetch_user_info($u_id){
    $u_id = (int)$u_id;

    $sql = "SELECT `user_uid` AS `username` FROM `users` WHERE `user_id` = {$u_id}";

    $result = mysql_query($sql);

    return mysql_fetch_assoc($result);
}

    include_once 'formDatabaseConnection.php';

    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);

    //Check for empty fields
    if (empty($name) || empty($message)) {
        header("Location: ../groupForm.php?signup=empty");
        exit();
    } else {
        //Insert the user into the database
                $sql = "INSERT INTO form (form_user, form_name, form_description) VALUES ('$u_id', '$name', '$message');";

                mysqli_query($conn, $sql);
                header("Location: ../findGroup.php");
                exit();
    }


} else {
    header("Location: ../groupForm.php");
    exit();
}

更新: 登录代码(login.inc.php):

<?php

session_start();

if (isset($_POST['submit'])) {

    include 'dbh.inc.php';

    $uid = mysqli_real_escape_string($conn, $_POST['uid']);
    $pwd = mysqli_real_escape_string($conn, $_POST['pwd']);

    //Error handlers
    //Check if inputs are empty
    if (empty($uid) || empty($pwd)) {
        header("Location: ../index.php?login=empty");
        exit();
    } else {
        $sql = "SELECT * FROM users WHERE user_uid='$uid' OR user_email='$uid'";
        $result = mysqli_query($conn, $sql);
        $resultCheck = mysqli_num_rows($result);
        if ($resultCheck < 1) {
            header("Location: ../index.php?login=error");
            exit();
        } else {
            if ($row = mysqli_fetch_assoc($result)) {
                //De-hashing the password
                $hashedPwdCheck = password_verify($pwd, $row['user_pwd']);
                if ($hashedPwdCheck == false) {
                    header("Location: ../index.php?login=error");
                    exit();
                } elseif ($hashedPwdCheck == true) {
                    //log in the user here
                    $_SESSION['u_id'] = $row['user_id'];
                    $_SESSION['u_first'] = $row['user_first'];
                    $_SESSION['u_last'] = $row['user_last'];
                    $_SESSION['u_email'] = $row['user_email'];
                    $_SESSION['u_uid'] = $row['user_uid'];

                    header("Location: ../homepage.php");
                    exit();
                }
            }
        }
    }

} else {
    header("Location: ../index.php?login=error");
    exit();
}
答案

当用户登录系统时,将该用户的ID和用户名存储在会话中并在要保存时检索该用户名,并将该form_user值替换为该会话值。请查看以下代码以获得更多说明。

$username = $_SESSION['u_first']. ' '.$_SESSION['u_last'];
$sql = "INSERT INTO form (form_user, form_name, form_description) VALUES ($username, $name, $message)";

以上是关于PHP / SQL在表单完成时存储当前登录用户的名称的主要内容,如果未能解决你的问题,请参考以下文章

PHP安全之web攻击

为啥用户输入不附加到我的 SQL 数据库?

使用 ASP.NET 表单身份验证时如何获取当前登录用户的用户 ID?

PHP/SQL 登录表单 - 如何检查数据库中的密码

PHP / Mysql 无法从 sql 数据库中检索当前登录的 first_name

PHP网站常见的几种攻击方式