Postingan lainnya
Stuck pada saat ingin menampilkan data
Saya mengikuti kelas pembuatan form login php tapi stuck pada saat ingin menampilkan data yang telah dilogin adakah yang bisa membantu saya?
//untuk login.php
<?php
require_once 'core/init.php';
$errors = array();
if( Input::get('submit') ){
//1. mengambil objek validasi
$validation = new Validation();
//2. metode check
$validation = $validation->check(array(
'username' => array( 'required' => true ),
'password' => array( 'required' => true )
));
//3. lolos ujian
if ( $validation->passed() ) {
if( $user->login_user( Input::get('username'), Input::get('password') ) )
{
Session::set('username', Input::get('username'));
header('Location: profile.php');
}else{
$errors[] = 'login gagal';
}
}else{
$errors = $validation->errors();
}
}
require_once 'templates/header.php';
?>
<h2>Login Di Sini</h2>
<form action="login.php" method="post">
<label>Username</label>
<input type="text" name="username"> <br>
<label>Password</label>
<input type="password" name="password"> <br>
<input type="submit" name="submit" value="login sekarang">
<?php if(!empty($errors)){ ?>
<div id="errors">
<?php foreach ($errors as $error){ ?>
<li> <?php echo $error; ?> </li>
<?php } ?>
</div>
<?php } ?>
</form>
<?php require_once 'templates/footer.php'; ?>
//untuk login.php
untuk user.php
<?php
class User{
private $_db;
public function __construct()
{
$this->_db = Database::getInstance();
}
public function register_user($fields = array())
{
if( $this->_db->insert('users', $fields) ) return true;
else return false;
}
public function login_user($username, $password)
{
$data = $this->_db->get_info('users', 'username', $username);
if(password_verify($password, $data['password']) )
return true;
else return false;
}
}
?>
//untuk user.php
//untuk database.php
<?php
class Database{
private static $INSTANCE = null;
private $mysqli,
$HOST = 'localhost',
$USER = 'root',
$PASS = '',
$DBNAME = 'tutorial';
public function __construct()
{
$this->mysqli = new mysqli( $this->HOST, $this->USER, $this->PASS, $this->DBNAME );
if( mysqli_connect_error() ){
die('gagal koneksinya');
}
}
/*
singleton pattern,
menguji koneksi agar tidak double
*/
public static function getInstance()
{
if( !isset( self::$INSTANCE ) ){
self::$INSTANCE = new Database();
}
return self::$INSTANCE;
}
public function insert($table, $fields = array())
{
//mengambil kolom
$column = implode(",", array_keys($fields));
//mengambil nilai
$valueArray = array();
$i = 0;
foreach ($fields as $key=>$values){
if( is_int($values) ){
$valueArray[$i] = $this->escape($values) ;
}else{
$valueArray[$i] = "'" . $this->escape($values) . "'";
}
$i++;
}
$values = implode(", ", $valueArray);
$query = "INSERT INTO $table ($column) VALUES ($values)";
return $this->run_query($query, 'masalah saat memasukan data');
}
public function get_info($table, $column, $value)
{
if( !is_int($value) )
$value = "'" . $value . "'";
$query = "SELECT * FROM $table WHERE $column = $value";
$result = $this->mysqli->query($query);
while($row = $result->fetch_assoc()) {
return $row;
}
}
public function run_query($query, $msg)
{
if($this->mysqli->query($query)) return true;
else die($msg);
}
public function escape($name){
return $this->mysqli->real_escape_string($name);
}
}
?>
//untuk database.php
0
Tanggapan
errornya apa?
Belum ada Jawaban. Jadi yang pertama Jawaban
Login untuk ikut Jawaban