简体   繁体   English

PHP / MySQL如何插入当前用户

[英]Php / Mysql how to insert current user with post

I'm rather new to php and mysql, but I've managed to achieve a lot over the past weeks. 我是php和mysql的新手,但过去几周我取得了很多成就。 I currently have a form that users fill out, and it stores the category and contents they provide. 我目前有一个用户填写的表格,它存储他们提供的类别和内容。

Additionally, in order to create users I've followed this tutorial ( http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL ) and have working user accounts and security linked with the page. 此外,为了创建用户,我遵循了本教程( http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL ),并已将有效的用户帐户和安全性关联在一起与页面。 This may be a simple question but I'm a bit stumped with how to link up a feature I need. 这可能是一个简单的问题,但是我对如何链接所需的功能有些困惑。

In short I want the insert line from my send_post.php to detect the currently logged in user and store it in a row called "contributor" or the like. 简而言之,我希望send_post.php中的插入行能够检测到当前登录的用户,并将其存储在名为“ contributor”等的行中。

mysqli_query($connect,"INSERT INTO mytable (category, contents, date)
VALUES ('$_POST[category]', '$_POST[contents]', NOW())");

So it obviously needs to be (category, contents, date, contributor) but I'm not sure how to pull the currently logged in username and define it as 'contributor' in my send_post.php 因此,显然需要(类别,内容,日期,贡献者),但是我不确定如何提取当前登录的用户名并将其定义为send_post.php中的“贡献者”

[Via the tutorial, usernames are stored in a separate table named 'members' along with their numerical ID, email, hashed password, and salt. [通过本教程,用户名及其数字ID,电子邮件,哈希密码和盐存储在单独的名为“成员”的表中。 I know how to call that information but no idea how to get the current user.] 我知道该如何调用该信息,但不知道如何获取当前用户。

Here is the full send_post.php 这是完整的send_post.php

<?php
include 'db_connect.php';
include 'functions.php';
sec_session_start();
if(login_check($mysqli) == true) {

//Connecting to sql db.
$connect=mysqli_connect("localhost","mydbusername","mydbpass","mysqldb");

header("Location: http://mysite.com/1.php");

if (mysqli_connect_errno()) { echo "Fail"; } else { echo "Success"; }

//Sending form data to sql db.
mysqli_query($connect,"INSERT INTO mytable (category, contents, date)
VALUES ('$_POST[category]', '$_POST[contents]', NOW())");

} else {
   echo 'Access denied. <br/>';
}

?>

I would be very grateful and upvote for any help. 如有任何帮助,我将非常感谢和支持。 Thanks 谢谢

Edit, Current send_post.php 编辑,当前send_post.php

<?php
include 'db_connect.php';
include 'functions.php';
sec_session_start();
if(login_check($mysqli) == true) {

//Connecting to sql db.
$connect=mysqli_connect("localhost","myusername","mypassword","mysqldb");

header("Location: http://mysite.com/1.php");

if (mysqli_connect_errno()) { echo "Fail"; } else { echo "Success"; }

$stmt = $mysqli -> prepare('INSERT INTO mytable (category, contents, date, userid) 
                            VALUES (?,?,NOW(),?)');
$stmt -> bind_params('ssi', $_POST['category'], $_POST['contents'], $_SESSION['user_id']);
$stmt -> execute();


} else {
   echo 'Access denied. <br/>';
}

?>

EDIT 2, Errors: (actually I got 3) 编辑2,错误:(实际上我有3个)

[07-Oct-2013 21:05:54] PHP Fatal error: Call to undefined method mysqli_stmt::bind_params() in /home2/mememe/public_html/mysite/send_post.php on line 22 [2013年10月7日21:05:54] PHP致命错误:在第22行的/home2/mememe/public_html/mysite/send_post.php中调用未定义方法mysqli_stmt :: bind_params()

[07-Oct-2013 21:05:54] PHP Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home2/mememe/public_html/mysite/index_3.php:7) in /home2/mememe/public_html/mysite/functions.php on line 12 [2013年10月7日21:05:54] PHP警告:session_start()[function.session-start]:无法发送会话缓存限制器-标头已发送(输出从/ home2 / mememe / public_html / mysite / index_3开始。 php:7)在第12行的/home2/mememe/public_html/mysite/functions.php中

[07-Oct-2013 21:05:54] PHP Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in /home2/mememe/public_html/mysite/functions.php on line 13 [2013年10月7日21:05:54] PHP警告:session_regenerate_id()[function.session-regenerate-id]:无法重新生成会话ID-头文件已在/home2/mememe/public_html/mysite/functions.php中发送13行

Edit 3: echo returns 编辑3:回声返回

Array ( [user_id] => 3 [username] => THEUSERNAME [login_string] => tons of hash infor like 4eb86947c8007ef1d0bb658168a76affa5c666518d3b58d76bf3040dc36d7a399c6c110a8c7f0d9f03d2b4d63271bd1335c61311edb152670f010f04583e7578 ) 数组([user_id] => 3 [username] => THEUSERNAME [login_string] =>大量的哈希信息,例如4eb86947c8007ef1d0bb658168a76affa5c666518d3b58d76bf3040dc36d7a399c6c110a8c7f0d9f03d2b4d63271bd13156701316523

By following that tutorial your current user id is stored in 通过遵循该教程,您当前的用户ID将存储在

$_SESSION['user_id']

The tutorial's login functions are here 本教程的登录功能在这里

You need to add a userid field in you MySQL table called "mytable" 您需要在MySQL表中添加一个名为“ mytable”的userid字段

then execute an SQL statement like this. 然后执行这样的SQL语句。

$stmt = $mysqli -> prepare('INSERT INTO mytable (category, contents, date, userid) 
                            VALUES (?,?,NOW(),?)');
$stmt -> bind_param('ssi', $_POST['category'], $_POST['contents'], $_SESSION['user_id']);
$stmt -> execute();

EDIT 编辑

After reading the tutorial more they cleared the $_SESSION['user_id'] when executing login_check($mysqli) so before you call that insert 阅读完本教程后,他们在执行login_check($mysqli)时清除了$_SESSION['user_id'] ,因此在调用该插入之前

$userId = $_SESSION['user_id'];

Your code should be like this 您的代码应如下所示

<?php
include 'db_connect.php';
include 'functions.php';
sec_session_start();

$userId = $_SESSION['user_id'];
if(login_check($mysqli) == true) {

//Connecting to sql db.
$connect=mysqli_connect("localhost","mydbusername","mydbpass","mysqldb");

header("Location: http://mysite.com/1.php");

if (mysqli_connect_errno()) { echo "Fail"; } else { echo "Success"; }

//Sending form data to sql db.
$stmt = $mysqli -> prepare('INSERT INTO mytable (category, contents, date, userid) 
                            VALUES (?,?,NOW(),?)');
$stmt -> bind_param('ssi', $_POST['category'], $_POST['contents'], $userId);
$stmt -> execute();
$stmt -> close();

} else {
   echo 'Access denied. <br/>';
}

?>

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

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