-1

good evening,i'm doing college work and i'm having trouble showing a list only show item related to session id

i think i'm done wrong select

 $query = $db->prepare("SELECT * FROM tbBiodata  where idfunc  like '%$currentUser['id']%' ");

this variable has the id: $currentUser['id']

I use this code at the beginning of the page to feed the variable:

 $currentUser = $user->getUser();

.....

        public function getUser(){
        //Check whether it's logged in
        if(!$this->isLoggedIn()){
            return false;
        }

        try {
            //Retrieve user data from the database
            $query = $this->db->prepare("SELECT * FROM tbLogin WHERE id = :id");
            $query->bindParam(":id", $_SESSION['user_session']);
            $query->execute();
            return $query->fetch();
        } catch (PDOException $e) {
            echo $e->getMessage();
            return false;
        }
    }

and for show list

<?php
require_once "db.php";
require_once "User.php";

// Create a user object
$user = new User($db);
// If not logged in
if(!$user->isLoggedIn()){
    header("location: login.php"); //Redirect to the login page
}
// Retrieve current user data
$currentUser = $user->getUser();

//===============

// Create a prepared statement to retrieve all data from tbBiodata
$query = $db->prepare("SELECT * FROM tbBiodata  where idfunc  like '%$currentUser['id']%' ");


// Run the SQL command
$query->execute();
// Take all data and enter variable $ data
$data = $query->fetchAll();
?>

to show list html

 <table border="1">
        <tr>
            <th>
                1
            </th>
            <th>
                2
            </th>
            <th>
                3
            </th>
            <th>
                4
            </th>
                <th>
                5
            </th>

            <th>
                6
            </th>
            <th>
                7
            </th>
        </tr>
        <?php $no=1; ?>
        <! - Repetition To Display All Data in Variable Data ->
        <?php foreach ($data as $value): ?>                
            <tr>
                <td>
                    <?php echo $no ?>
                </td>
                <td>
                    <?php echo $value['id'] ?>
                </td>
                <td>
                    <?php echo $value['nama'] ?>
                </td>
                <td>
                    <?php echo $value['alamat'] ?>
                </td>
                <td>
                    <?php echo $value['hp'] ?>
                </td>
                     <td>
                    <?php echo $value['idfunc'] ?>
                </td>
                <td>
                    <a href="edit.php?id=<?php echo $value['id']?>"><button type="button">Edit</button></a>                        
                    <a href="delete.php?id=<?php echo $value['id']?>" onclick="return confirm('Sure to delete data !'); " ><button type="button">Delete</button></a>
                </td>
            </tr>
            <?php $no++; ?>
        <?php endforeach; ?>

Finally I would like to add that this function it uses same variable in question is working $currentUser['nama']

 <h3>hi<font color="red"><?php echo $currentUser['nama'] ?></font>, <a href="logout.php">Logout</a></h3>

I apologize if I could not be clear but this mistake in question:("Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\test\index.php on line 17")

i don't know if i'm doing the select right or have an easier way to accomplish this result but would like help with that

1 Answers1

0

You need to put the variable inside {} when it contains a quoted array index:

$query = $db->prepare("SELECT * FROM tbBiodata  where idfunc  like '%{$currentUser['id']}%' ");

However, it would be better to use a parameter instead of substituting a variable.

$query = $db->prepare("SELECT * FROM tbBiodata  where idfunc  like CONCAT('%', :id, '%') ");
$query->bindParam(":id", $currentUser['id']);
Barmar
  • 741,623
  • 53
  • 500
  • 612