我是PHP MVC的新手,真的開始理解這個概念。我有一個問題在文本框中顯示當前用戶名。PHP/MYSQL/MVC如何獲取當前登錄的用戶名
請檢查下面我的代碼:
<?php
//require_once (Path::controllerClassPath(). "Summarycontroller.php");
require_once (Path::controllerClassPath(). "CalendarController.php");
//$summary = new SummaryController($this->sessionHandler->getUserId());
$calendar = new CalendarController($this->sessionHandler);
$userId = $this->sessionHandler->getUserId();
?>
<div id="fileleaverequestholiday">
<?php
echo '<form name="fileleaverequestholidayform" id="fileleaverequestholidayform" action="" method="post">';
echo '<fieldset class="fileleavetables"><legend>Request Leave</legend>';
echo '<table id="fileleaverequesttable">';
echo '<tr>';
//echo '<td class="fileleavetd">Name:</td><td class="fileleavetd"><input id="fileleaveemployeename" type="text" name="fileleaveemployeename"/></td>';
echo '<td class="fileleavetd">Name:</td><td class="fileleavetd"><input id="fileleaveemployeename" type="text" name="fileleaveemployeename" value="'.$retval.'" /> </td>'; ?>
我註釋掉的代碼,該片段:
$userId = $this->sessionHandler->getUsername();
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn)
{
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT employee_name FROM employee WHERE employee_username = $userId" ;
mysql_select_db('test');
$retval = mysql_query($sql, $conn);
print($retval);
if(! $retval)
{
die('Could not update data: ' . mysql_error());
}
更新:
<div id="fileleaveinsertholiday">
<?php
if(isset($_POST['fileleaveemployeename'],
$_POST['leavetype'],
$_POST['filerequestreason'],
$_POST['filerequeststartdate'])){
if(isset($_POST['filerequestenddate'])){
if($_POST['filerequestenddate'] > $_POST['filerequeststartdate']){
$result = $calendar->manageFileHoliday(
$_POST['fileleaveemployeename'],
$_POST['leavetype'],
$_POST['filerequestreason'],
$_POST['filerequeststartdate'],
'1',
$_POST['filerequestenddate']
);
if($result == Status::INSERT_SUCCESS){
echo '<b>Entry for '.$_POST['fileleaveemployeename'].' from '.$_POST['filerequeststartdate'].' to '.$_POST['filerequestenddate'].' submitted.</b>';
}
else{
echo '<b>Entry was not submitted. No such name exists in the list.</b>';
}
}
else{
echo '<b>Entry was not submitted. Invalid Date Range.</b>';
}
}
else{
$result = $calendar->manageFileHoliday(
$_POST['fileleaveemployeename'],
$_POST['leavetype'],
$_POST['filerequestreason'],
$_POST['filerequeststartdate'],
$_POST['requestduration']
);
if($result == Status::INSERT_SUCCESS){
echo '<b>Entry for '.$_POST['fileleaveemployeename'].' on '.$_POST['filerequeststartdate'].' submitted.</b>';
}
else{
echo '<b>Entry was not submitted. No such name exists in the list.</b>';
}
}
}
?>
</div>
因爲當我說這個,我頁面顯示不正確。任何幫助?謝謝。
您是否嘗試過打印'$ retval'?使用'print_r'來打印它。如果是的話,它打印什麼結果? –
@ganeshrj我會在哪裏放置print_r?對不起,我是新手 –
'$ retval = mysql_query($ sql,$ conn);''print_r($ retval)' –