简体   繁体   English

添加注释时显示用户名我的SQL无法记录用户名,只显示当前登录用户

[英]Display username when adding comments My SQL cannot record the username, only display the current login user

I am having the problem of displaying the username. 我有显示用户名的问题。 My SQL cannot record the username who added the comments....only display the current login username...what should I do? 我的SQL无法记录添加评论的用户名....只显示当前的登录用户名...我该怎么办? I don't want customers to enter their name again when they insert comments.... 我不希望客户在插入评论时再次输入他们的名字....

<?php

if ($_POST) { 
    $name   = mysqli_escape_string($conn, $_POST['name']);
    $supplier   = mysqli_escape_string($conn, $_POST['supplier']);
    $description = mysqli_escape_string($conn, $_POST['description']);
    $remark  = mysqli_escape_string($conn, $_POST['remark']);
    $price     = mysqli_escape_string($conn, $_POST['price']);
    $image    = mysqli_escape_string($conn, $_POST['image']);
    $createdate   = mysqli_escape_string($conn, $_POST['createdate']);  
    $author   = ( isset( $_SESSION["username"] ) ? $_SESSION["username"] : "" );



    $result = mysqli_query($conn, "INSERT INTO products (name, supplier, description, remark, price, image, createdate,author) VALUES('$name', '$supplier', '$description', '$remark', '$price', '$image', '$createdate','$author')");

    if (!$result) {
        exit(mysqli_error($conn)); 
    }

    redirect('comment.php'); } else { ?>

<?php } ?>

SUBMIT.PHP SUBMIT.PHP

<?php
    include("common.php");
    logincheck();

    $action = (isset($_GET["action"]) ? $_GET["action"] : "");

    $link = mysql_connect("localhost", "XX", "XX");
    mysql_select_db("XX");

    $name = $_POST["name"];
    $supplier = $_POST["supplier"];
    $description = $_POST["description"];
    $remark = $_POST["remark"];
    $price = $_POST["price"];

    $image = '';
    if ( isset( $_FILES['image']['name'] ) && trim( $_FILES['image']['name'] ) != "" )
    {
        $uploaddir = "Products/";
        $image = $uploaddir . basename($_FILES['image']['name']);

        if ( ! move_uploaded_file( $_FILES['image']['tmp_name'], $image ) ) {
          die('Image cannot be uploaded');
        }
    }

    switch($action) {
        case "insert": 
            $sql = "insert into products (name, supplier, description, remark, price, image, createdate) values ('$name', '$supplier', '$description', '$remark', '$price', '$image', NOW())";
            mysql_query($sql, $link); 
            break;
        case "save": 
            $id = $_POST["id"];
            $image2 = $_POST["image2"];
            if ($image == '')
                $image = $image2;
            $sql = "update products set name='$name', supplier='$supplier', description='$description', remark='$remark', price='$price', image='$image' where id='$id'";
            mysql_query($sql, $link);
            break;
        case "delete": 
            $id = $_GET["id"];
            $sql = "delete from products where id=$id";
            mysql_query($sql, $link);
            break;
    }
Header( "Location: comment.php" );  
?>

You can use put the username in the $_SESSION object once they're logged in. Then you can just echo the username; 您可以使用在登录后将用户名放在$ _SESSION对象中。然后您可以回显用户名; for example: echo $_SESSION['username']; 例如:echo $ _SESSION ['username'];

This way you can set the session. 这样您就可以设置会话。

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';


?>

You can see the session like this echo $_SESSION['favolor']; 你可以看到这样的会话,如echo $_SESSION['favolor']; and it will show green 它会显示green

see http://php.net/manual/en/function.session-start.php http://php.net/manual/en/function.session-start.php

In your code the $conn is not initialized anywhere. 在你的代码中, $conn没有在任何地方初始化。 Initialize it like this before trying the query. 在尝试查询之前将其初始化为这样。

$conn = mysqli_connect("localhost", "my_user", "my_password", "world");

Check the php manual for proper usage. 检查php手册以确保正确使用。

Hope that helps :) 希望有帮助:)

my suggestion is session , when a user logs in, set a user_login session to 1 , 我的建议是session ,当用户登录时,将user_login会话设置为1,

and user_name session to the user name. user_name session到用户名。

before every comment,check if user is logged in or not, 在每个评论之前,检查用户是否登录,

if he is not get the name from form, 如果他没有从表格中获取名称,

if he is, hide the name input and save the user_name session in the database. 如果是,则隐藏名称输入并将user_name session保存在数据库中。

if you have any problem using session comment on this answer, i will explain , if you want 如果您对此答案使用session评论有任何问题,我会解释,如果您愿意

in the first line of your file(even before any spaces or new lines) you must add this: 在文件的第一行(甚至在任何空格或新行之前),您必须添加:

<?php session_start(); ?>

after the user enters his username and password, you check if they match or not, you have : 在用户输入用户名和密码后,检查它们是否匹配,您有:

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

if username and password matched with username and password stored in database, do this : 如果用户名和密码与存储在数据库中的用户名和密码相匹配,请执

$_SESSION['username']=$username;
$_SESSION['log']=1;

and then , about your comment page : 然后,关于你的评论页面:

<?php if(!isset($_SESSION['log']) || $_SESSION['log']!=1) { ?>
Enter your name : <input type='text' name='user_name'>
<?php } ?>

now, if user is logged in, he cant enter his name. 现在,如果用户登录,他就无法输入他的名字。

i assume your form posts data to post.php , now in post.php you must do this : 我假设你的表单将数据发布到post.php ,现在在post.php你必须这样做:

<?php
 if($_POST['user_name']) $name = $_POST['username']
else $name = $_SESSION['username']
 ?>

and then, Note: put <?php session_start() ?> in your first line of post.php 然后,注意:将<?php session_start() ?>放在post.php第一行

after all that, you can insert the comment and name of the user in the database. 毕竟,您可以在数据库中插入用户的注释和名称。 good luck with that, if my answer helped you, mark my question as accepted. 祝你好运,如果我的回答对你有所帮助,请将我的问题标记为已接受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM