Reputation: 137
I want to get the username of logged in user so that I can insert it to database along with my form so that I can associate the data being inserted with the user logged in.. any idea?
<?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>
Here is my Insert query..as you can see, I didn't specifiy any value for $username
that's my problem what should I put there?
$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();
and here is what I have inside 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"];
}
?>
Upvotes: 1
Views: 5996
Reputation:
As I understand: You need to add additional column to your table 'photos', the column name will be: userid not username, because its better to depend on the key-id for the user not the name.
then, like any other insert, add the new column to the query.
For now, you can try with your current option, add username field to the table, and the value is: $_SESSION["userCakeUser"];
Upvotes: 0
Reputation: 518
I think from your code you would use
$loggedInUser = $_SESSION["userCakeUser"];
Upvotes: 0
Reputation: 1472
When the user logs in, why not save it as a Session variable, then access that?
For example:
//Login page
//After user is authenticated
session_start();
$_SESSION['username'] = $username
//$username is whatever you grabbed as the supplied login details
Upvotes: 2