我正在嘗試編寫一個程序,其中用戶將數據添加到數據庫中,而用戶的用戶名也是在沒有手動輸入的情況下輸入的。 這是PHP代碼如何將會話用戶名插入到mySql數據庫中
$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'))
));
這是htlml
<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());
}
}
你需要一些數據庫交互,這將是一個好的開始。連接不僅會自動發生,而且您還沒有做出任何努力。 – Jonast92 2015-02-11 14:05:16
@ Jonast92我已經使用「require_once'core/init.php';」數據確實進入數據庫,會話變量不會。 – Love 2015-02-11 14:10:04
顯示您的數據庫代碼,然後.. – 2015-02-11 14:12:47