1

我想获取登录用户的用户名,以便我可以将其与我的表单一起插入数据库,以便我可以将插入的数据与登录用户相关联..任何想法?

    <?php 
require_once("models/config.php");
securePage($_SERVER['PHP_SELF']);
?>

<form action="" method="post" enctype="multipart/form-data">
<label  for="file"><font  size="5"><b>Choose Photo:</b></font></label>
<input type="file" name="file" onchange="file_selected = true;" required><br>
Last Name:<input  type="text" name="fname" required><br> 
Last Name:<input  type="text" name="lname" required><br> 
Age:<input type="text" name="age" required><br>
<input id="shiny" type="submit" value="Submit" name="submit">
</form>

这是我的插入查询..如您所见,我没有指定任何值,$username这是我的问题,我应该放什么?

$photo= "pictures/" . $_FILES["file"]["name"];
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$age   =$_POST["age"];
$username = ?
$stmt = $mysqli->prepare("INSERT INTO photos (Firstname, Lastname, Age, author) VALUES (?, ?, ?, ?, ?)");

// TODO check that $stmt creation succeeded

// "s" means the database expects a string
$stmt->bind_param("ssss", $photo, $fname, $lname, $age, $username);

$stmt->execute();


$stmt->close();

$mysqli->close();

这就是我在 config.php 中的内容...

<?php
require_once("db-settings.php"); //Require DB connection

//Retrieve settings
$stmt = $mysqli->prepare("SELECT id, name, value
FROM ".$db_table_prefix."configuration");   
$stmt->execute();
$stmt->bind_result($id, $name, $value);

while ($stmt->fetch()){
$settings[$name] = array('id' => $id, 'name' => $name, 'value' => $value);
}
$stmt->close();

//Set Settings
$emailActivation = $settings['activation']['value'];;
$mail_templates_dir = "models/mail-templates/";
$websiteName = $settings['website_name']['value'];
$websiteUrl = $settings['website_url']['value'];
$emailAddress = $settings['email']['value'];
$resend_activation_threshold = $settings['resend_activation_threshold']['value'];
$emailDate = date('dmy');
$language = $settings['language']['value'];
$template = $settings['template']['value'];

$default_hooks = array("#WEBSITENAME#","#WEBSITEURL#","#DATE#");
$default_replace = array($websiteName,$websiteUrl,$emailDate);

if (!file_exists($language)) {
$language = "models/languages/en.php";
}

if(!isset($language)) $langauge = "models/languages/en.php";

//Pages to require
require_once($language);
require_once("class.mail.php");
require_once("class.user.php");
require_once("class.newuser.php");
require_once("funcs.php");

session_start();

//Global User Object Var
//loggedInUser can be used globally if constructed
if(isset($_SESSION["userCakeUser"]) && is_object($_SESSION["userCakeUser"]))
{
$loggedInUser = $_SESSION["userCakeUser"];
}

?>
4

3 回答 3

2

当用户登录时,为什么不将其保存为 Session 变量,然后访问它?

例如:

//Login page
//After user is authenticated

session_start();
$_SESSION['username'] = $username 

//$username is whatever you grabbed as the supplied login details
于 2013-01-26T19:18:00.790 回答
0

我认为从您的代码中您会使用

$loggedInUser = $_SESSION["userCakeUser"];
于 2013-01-26T19:19:54.720 回答
0

据我了解:您需要在表“照片”中添加其他列,列名将是:用户 ID 而不是用户名,因为最好依赖于用户的键 ID 而不是名称。

然后,像任何其他插入一样,将新列添加到查询中。

现在,您可以尝试使用当前选项,将用户名字段添加到表中,值为:$_SESSION["userCakeUser"];

于 2013-01-26T19:21:35.263 回答