如何将会话用户名插入 mySql 数据库
How to insert session username into mySql database
我正在尝试编写一个程序,用户可以在其中将数据添加到数据库中,并且还无需手动输入用户的用户名。
这是php代码
$user->createSession(array(
'title' => Input::get('title'),
'start_date' => date('Y-m-d H:i:s', strtotime(Input::get('start_date'))),
'end_date' => date('Y-m-d H:i:s', strtotime(Input::get('end_date'))),
'passcode' => (Input::get('passcode')),
'username' => (Input::get('username'))
));
这是 html
<form class="session" action="" method="POST" >
<div class="input-group">
<label for="title" ><span class="input-group-addon" id="basic-addon1">Title</span></label>
<input type="text" id="username" name="title" class="form-control" value = "<?php echo escape(input::get('title')); ?>" placeholder="Title" autocomplete="on" required>
</div>
<div id="datetimepicker" class="input-append date">
<input type="text" name="start_date" placeholder="start date"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
<div id="datetimepicker1" class="input-append date">
<input type="text" name="end_date" placeholder="end date"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
<label for="passcode" class="input-group">Pass-code (optional)</label>
<input type="passcode" id="passcode" name="passcode" class="form-control" placeholder="Passcode" autocomplete="off" >
<br>
<input type = "hidden" name = "username" value = "<?php echo escape($user->data()->username); ?>">
<button class="btn btn-lg btn-primary" type="submit">Create</button>
它将除用户名会话之外的所有内容添加到数据库中。我不知道该怎么办。我也试过删除:
<input type = "hidden" name = "username" value = "<?php echo escape($user->data()->username); ?>">
并且做了:
$user->createSession(array(
'title' => Input::get('title'),
'start_date' => date('Y-m-d H:i:s', strtotime(Input::get('start_date'))),
'end_date' => date('Y-m-d H:i:s', strtotime(Input::get('end_date'))),
'passcode' => (Input::get('passcode')),
'username' => $user->data()->username
));
这是请求的数据库代码
session_start();
$GLOBALS['config'] = array(
'mysql' => array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'db' => 'coursework'),
//remember me
'remember' => array('cookie_name' => 'hash', 'cookie_expiry' => '604800'),
'session' => array('session_name' => 'user', 'token_name' => 'token'));
spl_autoload_register(function($class) {
require_once 'classes/'.$class.'.php';
});
编辑
此代码将数据插入数据库
public function createSession($fields = array()){
if(!$this->_db->insert('sessions', $fields)){
throw new Exception ("there was a problem adding a session");
}
}
其他一切正常,数据库连接正常,数据输入数据库正常,但用户名字段为空。正在创建的会话是其他类似主题的东西。
连接到数据库
function __construct() {
try {
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') .';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
//echo 'connected';
} catch (PDOException $e) {
die($e->getMessage());
}
}
How to insert session username into mySql database
- 您需要 connect 使用您的配置信息到实际数据库。
PDO 很适合这个:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
- 你需要insert into the database, preferrably using prepared statements.
有很多 decent answers on how although I like mixing it up with the prepared statement article,您应该在准备好的查询中执行变量数组。
- 获取会话信息
你大概能想出怎么做。也许还有一些其他信息?
$someValue = ""; // Just some random value.
$someSessionValue = ""; // Extract from session. Probably $_SESSION['stuff'] or similar.
- 最后,插入提取的信息。
应该这样做,仔细查看链接或进行适当的搜索:
$sql = $dbh->prepare("INSERT INTO tablename(column1, column2) VALUES (?, ?)");
$sql->execute(array($someValue, $someSessionValue));
- 如果您希望它自动发生,那么您只需将它放入一个方法中,当某些操作被触发时,您的代码将自动调用。
我正在尝试编写一个程序,用户可以在其中将数据添加到数据库中,并且还无需手动输入用户的用户名。 这是php代码
$user->createSession(array(
'title' => Input::get('title'),
'start_date' => date('Y-m-d H:i:s', strtotime(Input::get('start_date'))),
'end_date' => date('Y-m-d H:i:s', strtotime(Input::get('end_date'))),
'passcode' => (Input::get('passcode')),
'username' => (Input::get('username'))
));
这是 html
<form class="session" action="" method="POST" >
<div class="input-group">
<label for="title" ><span class="input-group-addon" id="basic-addon1">Title</span></label>
<input type="text" id="username" name="title" class="form-control" value = "<?php echo escape(input::get('title')); ?>" placeholder="Title" autocomplete="on" required>
</div>
<div id="datetimepicker" class="input-append date">
<input type="text" name="start_date" placeholder="start date"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
<div id="datetimepicker1" class="input-append date">
<input type="text" name="end_date" placeholder="end date"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
<label for="passcode" class="input-group">Pass-code (optional)</label>
<input type="passcode" id="passcode" name="passcode" class="form-control" placeholder="Passcode" autocomplete="off" >
<br>
<input type = "hidden" name = "username" value = "<?php echo escape($user->data()->username); ?>">
<button class="btn btn-lg btn-primary" type="submit">Create</button>
它将除用户名会话之外的所有内容添加到数据库中。我不知道该怎么办。我也试过删除:
<input type = "hidden" name = "username" value = "<?php echo escape($user->data()->username); ?>">
并且做了:
$user->createSession(array(
'title' => Input::get('title'),
'start_date' => date('Y-m-d H:i:s', strtotime(Input::get('start_date'))),
'end_date' => date('Y-m-d H:i:s', strtotime(Input::get('end_date'))),
'passcode' => (Input::get('passcode')),
'username' => $user->data()->username
));
这是请求的数据库代码
session_start();
$GLOBALS['config'] = array(
'mysql' => array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'db' => 'coursework'),
//remember me
'remember' => array('cookie_name' => 'hash', 'cookie_expiry' => '604800'),
'session' => array('session_name' => 'user', 'token_name' => 'token'));
spl_autoload_register(function($class) {
require_once 'classes/'.$class.'.php';
});
编辑
此代码将数据插入数据库
public function createSession($fields = array()){
if(!$this->_db->insert('sessions', $fields)){
throw new Exception ("there was a problem adding a session");
}
}
其他一切正常,数据库连接正常,数据输入数据库正常,但用户名字段为空。正在创建的会话是其他类似主题的东西。
连接到数据库
function __construct() {
try {
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') .';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
//echo 'connected';
} catch (PDOException $e) {
die($e->getMessage());
}
}
How to insert session username into mySql database
- 您需要 connect 使用您的配置信息到实际数据库。
PDO 很适合这个:
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
- 你需要insert into the database, preferrably using prepared statements.
有很多 decent answers on how although I like mixing it up with the prepared statement article,您应该在准备好的查询中执行变量数组。
- 获取会话信息
你大概能想出怎么做。也许还有一些其他信息?
$someValue = ""; // Just some random value.
$someSessionValue = ""; // Extract from session. Probably $_SESSION['stuff'] or similar.
- 最后,插入提取的信息。
应该这样做,仔细查看链接或进行适当的搜索:
$sql = $dbh->prepare("INSERT INTO tablename(column1, column2) VALUES (?, ?)");
$sql->execute(array($someValue, $someSessionValue));
- 如果您希望它自动发生,那么您只需将它放入一个方法中,当某些操作被触发时,您的代码将自动调用。