Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/SQL存储表单完成时当前登录用户的名称_Php_Mysql_Sql_Database_Forms - Fatal编程技术网

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

PHP/SQL存储表单完成时当前登录用户的名称,php,mysql,sql,database,forms,Php,Mysql,Sql,Database,Forms,一旦用户登录到我的网站,他们可以填写一个表格,其中有两个字段,“项目名称”和“项目说明” 我需要帮助存储填写该表单的人的用户名 例如,如果我要以管理员身份登录并填写表单,那么在数据库中,表单信息旁边应该显示用户名Admin 非常感谢您的帮助,并提前向您表示感谢 表格DB: 数据库名称:formsystem 表格名称:表格 我想将用户名保存到的列:form_user 我的代码(groupForm.php): 创建一个组 请填写以下部分。 创造 后端代码(form_process.php):

一旦用户登录到我的网站,他们可以填写一个表格,其中有两个字段,“项目名称”和“项目说明”

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

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

非常感谢您的帮助,并提前向您表示感谢

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

我的代码(groupForm.php):


  • 创建一个组 请填写以下部分。 创造
    后端代码(form_process.php):


    当用户登录您的系统时,将该用户的ID和用户名存储在会话中,并在需要保存用户名时检索该用户名,并用该会话值替换您的表单用户值。查看下面的代码以了解更多说明

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

    不要使用
    mysql\u查询
    mysqli\u查询
    。在任何地方使用
    mysqli\u查询
    。还可以使用参数化查询。
    $u\u id
    未设置在插入中引用它的位置。您可以使用存储
    u\u id
    的方法在会话中存储用户名。或者,您可以单独查询并通过该id获取用户名。@chris85谢谢您的建议,我知道已贬值的代码,并将很快修复它!:)@KirillSimin我是这方面的初学者,你能帮我弄一下语法吗?我不太确定怎么做,伙计。我已经更新了上面的帖子,也加入了我的登录代码。你能为我提供一些如何编写代码的帮助吗?我被困在这一点上,已经做了好几天了(我是初学者)你几乎已经完成了,别担心。检查我的最新答案。非常感谢你,伙计,非常感谢你的帮助。您的代码起初不起作用,但在注销网站并重新登录以提交新表单后,它起作用了!:D
    <?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();
    }
    
    <?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();
    }
    
    $username = $_SESSION['u_first']. ' '.$_SESSION['u_last'];
    $sql = "INSERT INTO form (form_user, form_name, form_description) VALUES ($username, $name, $message)";