简体   繁体   中英

Specific page for each user, php and mysql

I am building an application. What I am tryinf to do is that when I user log in to have a specific page for each user. I have realised this for the first page. So when the user log in, the next page is a page that contains his name, and a calendar. What I want to do now is that when the user click in a date of the calendar, only the activities of this user are called from the database. My problem is that after I ma loged in whith a user profile, and when I click on one date of the calendar, it shows me not only the activities of this user but also the activities of other user...How can I solve this? Please help me... Below is my code:

Here is the part of index page where user log in:

<form method="GET" action="signin.php" >  
    <table class="formtable">
        <tr>
            <td>
                <input class="text" type="text" id="email_id" name="email" value="Adresa emailit" required="required" onfocus="changefocus(this,'Adresa emailit')" onblur="fillemptyform(this,'Adresa emailit')">
            </td>
        </tr>
        <tr>
            <td>
                <input class="text" type="password" id="pass_id" name="pass" value="Password" required="required" onfocus="changefocus(this,'Password')" onblur="fillemptyform(this,'Password')">
            </td>
        </tr>
    </table>
    <input class="button" name="submit" type="submit" value="Sign in">
 </form>

Here is the sign in.php page that opens as sonn as the user log in: ( I have shown only the part where php is used)

<?php 
    //kontrollo per submitin e formes
    if(isset($_GET['email']) AND isset($_GET['pass'])) {
        $Emaili=$_GET['email'];
        $Fjalekalimi=$_GET['pass'];
        mysql_connect("127.0.0.1","root","") or die("Smund te lidhet me serverin");
        mysql_select_db("axhenda") or die("Kjo databaze nuk u gjet");
        $userquery = mysql_query("SELECT *  FROM perdoruesi where Emaili = '$Emaili' AND  Fjalekalimi = '$Fjalekalimi'") or die("Kjo query nuk mund te plotesohet");
        if(mysql_num_rows($userquery) !=1) { 
            die ("Ky perdorues nuk u gjet");
        }

        while($row=mysql_fetch_array($userquery, MYSQL_ASSOC)) {
            $Emri =$row['Emri'];
            $Mbiemri =$row['Mbiemri'];
            $Emaili =$row['Emaili'];
            $Fjalekalimi =$row['Fjalekalimi'];
            $Aksesi =$row['Aksesi'];
        }

?>

<?php
    } else die("Bosh");
?> 

<div class="miresevini">
    <span style="color: rgb(4, 38, 73); font-weight: bold;"> Welcome(</span>
    <span style="color: #b2d7fd"><?php echo $Emri;?>, <?php echo $Mbiemri;?></span>
    <span style="color: rgb(4, 38, 73);font-weight: bold;">)</span>
</div>

And here is the kalendar.php file, which is called when I click to a date , and it shows all the activities of that date

<body>      
<?php
    $con = mysql_connect('127.0.0.1','root','');
    if (!$con) {
        die('Lidhja me databazen nuk mund te kryhet' .mysql_error(). ' </body></html>');
    }

    if(!mysql_select_db("Axhenda",$con))
        die('Nuk mund te hapet databaza Axhenda'.mysql_error(). '</body></html>');

    $result= mysql_query("SELECT * FROM Aktiviteti where Data= '$_POST[dataoutput]'");

    $count=mysql_num_rows($result);
?>

<div class="title"> Aktivitetet per daten <?php print ("$_POST[dataoutput]"); ?></div>
<form name="form1" method="post" action="delete.php">
    <table >
        <th>
            <th ><strong>Emri </strong></th>
            <th ><strong>Pershkrimi </strong></th>
            <th><strong>Ora</strong></th>
        </th>
<?php
while ($row=mysql_fetch_array($result)) {
?>

<tr>
<td ><input name="checkbox[]" type="checkbox" value="<?php echo $row['Id_Akt']; ?>"></td>
<td style="font-size:0.9em"><?php echo $row['Emri']; ?></td>
<td ><?php echo $row['Pershkrimi']; ?></td>
<td><?php echo $row['Ora']; ?></td>
</tr>

<?php
}
?>


</table>
<input class="button" name="delete" type="submit" value="Delete" style="margin-left:40%; margin-top:100px; width:15%">

    </form>

</body>

Please help me. What should I do in order to show only the activitiies of log in user ... The tables that are used in mysql are two:

Perdoruesi(Id_Per, Emri, Mbiemri, Emaili,Pass, Akses)   saves information about the user

and

Aktiviteti(Id_Akt, Perd_Id, Emri, Pershkrimi, Date, Time), saves the information about the activity.

and Perd_Id is a Foreign key of Id_Per in the table Perdoruesi. I have also build the relation between foreign key and primary key, but I still does not get only the activities of the specific user....Please help me. Thanks in advance!

I modified the signin.php file like this:( I add the SESSION part)

<?php 

//kontrollo per submitin e formes
if(isset($_GET['email']) AND isset($_GET['pass']))
{$Emaili=$_GET['email'];
 $Fjalekalimi=$_GET['pass'];
mysql_connect("127.0.0.1","root","") or die("Smund te lidhet me serverin");
mysql_select_db("axhenda") or die("Kjo databaze nuk u gjet");
$userquery = mysql_query("SELECT *  FROM perdoruesi where Emaili = '$Emaili' AND  Fjalekalimi = '$Fjalekalimi'") or die("Kjo query nuk mund te plotesohet");
if(mysql_num_rows($userquery) !=1)
{ die ("Ky perdorues nuk u gjet");
   }

while($row=mysql_fetch_array($userquery, MYSQL_ASSOC))
   {$Id_Per=$row['Id_Per'];
   $Emri =$row['Emri'];
   $Mbiemri =$row['Mbiemri'];
   $Emaili =$row['Emaili'];
   $Fjalekalimi =$row['Fjalekalimi'];
   $Aksesi =$row['Aksesi'];
   }



?>



<?php
}
  else die("Bosh");

?>

<?php
session_start();
// store session data
 $_SESSION['Id_Per'] = 1;
?>

and modified the kalendar.php like this:

$result= mysql_query("SELECT * FROM Aktiviteti where Data= '$_POST[dataoutput]' and Perd_Id= $_SESSION['Id_Per']");

But it still gives me this error:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\agenda\kalendar.php on line 63

What can I do?

It looks like when you are loading the data with the query

$result= mysql_query("SELECT * FROM Aktiviteti where Data= '$_POST[dataoutput]'");

You are selecting all the data for that date. You will need todo something like

$result= mysql_query("SELECT * FROM Aktiviteti where Data= '$_POST[dataoutput]' and Email = '$_SESSION['email']'");

In the above query I am assuming you are storing the users email in session. If not you will need to be able to have access to it some how. That way you are only selecting the records that have the current users email address associated with it. This is also assuming you are storing the email address of each user with their event for the day.


Update

Example Login

<?php 
     if($_SERVER['REQUEST_METHOD'] == 'POST') {
           if(isset($_POST['password']) && isset($_POST['username']) {
               // Check to see if username and password is correct
               // Should return user if correct else false
               $user = authenticate($_POST['username'], $_POST['password']);
               if($user){
                  $_SESSION['user'] = $user['username'];
                  $_SESSION['user_id'] = $user['id'];
                  $_SESSION['email'] = $user['email'];
               } else {
                    // Handel user not being authenticated
           } else {
               // Handle the username and password not being present
           }
      }
?>

Example get information for db.

<?php
         $q = "SELECT * FROM table_name WHERE Data = " . $_POST['item'] . " and user_id = " . $_SESSION['user_id'];
         $result = mysql_query($q);
         // Do something with the result.
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM