Page 1 of 1

login issue

Posted: Fri May 01, 2009 4:32 pm
by sawi
Hi all , I'm trying to get this sorted last couple days and I'm a bit stuck here , I'm new to php so my code is simple enough..

After I have logged in successfully to my 'login page', the 'members page' appears
and displays information, data regarding that member but show up only one post instead of 10?I think I have session problem here and redirection do not working correctly, can somone help me ?

login.php

Code: Select all

 
<?php
session_start();
include "connect.php";
 
if (isset($_POST['txtusername']) && isset($_POST['txtpassword'])) {
// check if the user id and password combination is correct
if ($_POST['txtusername'] == 'admin' && $_POST['txtpassword'] == 'admin') {
 
//$_SESSION['login'] = "Login";
 
$txtusername = $_SESSION["txtusername"];
$txtpassword = $_SESSION["txtpassword"];
// after login we move to the main page
header('Location: firstprint.php');
exit;
} else {
        
        include "connect.php";
        
        mysql_select_db("post_db")
        or die ("Could not select database because ".mysql_error());
        
        $q = "SELECT id FROM $table1 WHERE txtusername='$txtusername' and txtpassword='$txtpassword'";
        $result = mysql_query($q)or die (mysql_error());
        
        if (mysql_num_rows($result) > 0) {
        $user = mysql_fetch_array($result);
        $_SESSION["ID"] = $user["id"];
        
        //echo "<p>The Current Session ID is:".$ID."</p>";
        //echo "<p>SQL IS:".$q."</p>";
        
        header("location:firstprint.php");
        
                                        }
        else {
        echo "Wrong Username or Password";
            }
 
 
firstprint.php
 
<?php 
// Connects to your Database 
 
 
session_start();
 
include "connect.php"; 
 
$ID = $_SESSION["ID"];
 
 
mysql_select_db("post_db")
or die ("Could not select database because ".mysql_error());
 
$data = mysql_query("SELECT * FROM textpost WHERE id='$ID'") 
or die(mysql_error()); 
echo "<h3>"."Your recent blog post's"."<br/>"."</h3>";
echo "<table align='right'>";
echo "<tr>";
echo "<td>";
echo "Login Page <a href='loginpage.html'>Click Here.</a>";
echo "</td>";
echo "<tr>"."<td> <br> </td>"."</tr>";
echo "<td>";
echo " Add New Post Here <a href='http://localhost/Project/add_post.php'>Click Here.</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "<table border='0' >"; 
 
if (mysql_num_rows($data) > 0){
while($info = mysql_fetch_array( $data )) 
{ 
echo "<tr>"."<td><b>ID:</b></td> <td>".$info['id'] . "</td>"."</tr>"; 
echo "<tr>"."<td><b>Title:</b></td> <td>".$info['title'] . "</td>"."</tr>"; 
echo "<tr>"."<td><b>Comment:</b></td> <td>".$info['comment'] . " </td>"."</tr>";
echo "<tr>"."<td><b>Authorname:</b></td> <td>".$info['authorname'] . " </td>"."</tr>";
echo "<tr>"."<td><b>Date:</b></td> <td>".$info['date'] . " </td>"."</tr>";
echo "<tr>"."<td> <br> </td>"."</tr>";
 
?>
<td bgcolor="#FFFFFF"><a href="delete_firstprint.php?id=<?PHP echo $info['id']; ?>">delete</a></td>
<?PHP
} 
echo "</table>"; 
}else{
echo "no results";
}
 
?> 
 
Any suggestions I appreciate it,

Re: login issue

Posted: Fri May 01, 2009 9:03 pm
by it2051229
i dont really understand what post you are talking about... But i think the post is coming from your database... sooo if there's only one post coming out instead of ten, you might check first your SQL statement for querying if it's correct by doing it manually on the phpMyAdmin (you query in manually there).. if it's working, then maybe the parameters you have provided on your SQL statement is incorrect.. to check the parameters, after the user has logged in and before you execute the SQL Statement, you ECHO it out to check if the SQL statement and along with the parameters provided is Correct.

Re: login issue

Posted: Sat May 02, 2009 4:03 am
by sawi
Hey , Thanks for that , it is definately something wrong here , it doesnt matter what username and password I'm using , SQL id is always 4,

The Current Session ID is:41

SQL IS:Resource id #4
ID: 41
Title: test
Comment: test
Authorname: test
Date: 2009-04-18 23:21:38


delete

.. different username and password then above , session different but resource id still the same..

The Current Session ID is:17

SQL IS:Resource id #4
ID: 17
Title: hello everyone
Comment: Hi My namie is Karl
Authorname: Zen
Date:


delete

I'll try to get this sorted myself if any suggestions please let me know ,thanks.

Re: login issue

Posted: Sat May 02, 2009 4:52 am
by sawi
Ok I found the mistake , i'm getting id of post not id of username and password but how would I input post belongs just to particula user?

Re: login issue

Posted: Sat May 02, 2009 5:22 am
by it2051229
you improve your SQL statement like

"select the posts from posts table WHERE post_owner=account_id"

the sql above is an informal statement but i think you get my point.
You have to create a new column on the POSTS TABLE. The new column(post_owner) is where to place the account_id of the owner so that the SQL above works.

Re: login issue

Posted: Sat May 02, 2009 5:44 am
by sawi
Thanks for reply , I added owner field to textpost table

Code: Select all

 
$ID = $_SESSION["ID"];
 
 
mysql_select_db("post_db")
or die ("Could not select database because ".mysql_error());
 
$data = mysql_query("SELECT * FROM textpost WHERE owner = "'.$ID.'"  ") 
or die(mysql_error()); 
 
that's what I should do? anyway I'm getting string error?!
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
ok my mistake: i fix it , ... owner = ' " . $ID . " ' .... but still the same as happen as before but no I'm getting no results , uff what's a day.

The Current Session ID is:41

SQL IS:Resource id #4

no results

OK , from my understanding is no results because I just addes new owner field ...

Now , I'm trying to insert new post but I'm getting error:

Could not insert data because Incorrect integer value: '' for column 'owner' at row 1

I've added owner into mysql insert code:


add_post.php

Code: Select all

 
<html>
<body>
      <br>
 
      <h3>Add Post</h3>
      <form method="post" action="insert.php">
      
      Title: <input name="title" size="20" maxlength="100">
 
      <br>
 
      Comment: <input name="comment" size="20" maxlength="100">
 
      <br>
        
      Authorname: <input name="authorname" size="20" maxlength="100">
      
      <br>
    
      
      <input name="date" type="hidden" value=NOW()> 
      
      <br>
      
      <input name="owner" type="hidden"> 
      
      <input type="submit" name="submit" value="Add Post">
 
      </form>
      
</body>
</html>
 
 
insert.php
 
?PHP
 
include "connect.php"; 
 
mysql_select_db("post_db")
or die ("Could not select database because ".mysql_error());
// insert the data
$insert=mysql_query("INSERT INTO $table values (NULL,'".$_POST['title']."','".$_POST['comment']."','".$_POST['authorname']."',now(),'".$_POST['owner']."')")
or die("Could not insert data because ".mysql_error());
// print a success message
echo "Your post has been added to database! ";
echo " Post added!<br><a href='http://localhost/Project/firstprint.php'>Click Here.</a>";
 
 
?> 
 
funny ;0)

Re: login issue

Posted: Sat May 02, 2009 6:46 am
by sawi
Any help please?
I cannot pass any value into owner when I'm inserting data into textpost table so how to avoid error?

Re: login issue

Posted: Sat May 02, 2009 12:09 pm
by Benjamin
sawi, I've added code tags for you twice now. Please read Posting code in the Forumes.

Re: login issue

Posted: Sat May 02, 2009 12:49 pm
by sawi
I'm sorry , I wont forget next time ..My code stil not working anyone?Thanks.

Re: login issue

Posted: Sat May 02, 2009 8:25 pm
by it2051229
I think the "$_POST['owner']" is always empty.. cause it's like saying that everytime the user hits the "submit" button, that the Owner's ID is inside the <form> tag.... When the user logs in, are you storing the owner's ID on your SESSION variable? cause if it's in there, you have to get the OWNER'S ID "in there" and not from $_POST.