Taking a variable from the login form and using it in another form and store

Taking a variable from the login form and using it in another form and store the same variable in the database table, and retrieve the same values when that person logs in.
For example if there are two users - user1 and user2
when user1 log in only the values entered by user1 should be visible to him.
when user2 log in only the values entered by user1 should be visible to him.

My source code would be

Table source code -

CREATE TABLE IF NOT EXISTS books (
BookID int(11) NOT NULL AUTO_INCREMENT,
Title varchar(150) NOT NULL,
Author varchar(150) NOT NULL,
PublisherName varchar(150) NOT NULL,
CopyrightYear year(4) NOT NULL,
PRIMARY KEY (BookID)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;


– Dumping data for table books

INSERT INTO books (BookID, Title, Author, PublisherName, CopyrightYear) VALUES
(1, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(2, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(3, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(4, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(5, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(6, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(7, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007),
(8, ‘Algebra’, ‘Almel’, ‘Chun Sa’, 2007);

Database Connection code-

<?php $conn = mysql_connect('localhost', 'root', ''); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db("adddel", $conn); ?>

Delete Source Code -

<?php include("db.php"); $id =$_REQUEST['BookID']; // sending query mysql_query("DELETE FROM books WHERE BookID = '$id'") or die(mysql_error()); header("Location: index.php"); ?>

My index page Source code -

Books
<tr>
	<td>Title:</td>
	<td><input type="text" name="title" /></td>
</tr>
<tr>
	<td>Author</td>
	<td><input type="text" name="author" /></td>
</tr>
<tr>
	<td>Publisher Name</td>
	<td><input type="text" name="name" /></td>
</tr>
<tr>
	<td>Copyright Year</td>
	<td><input type="text" name="copy" /></td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><input type="submit" name="submit" value="add" /></td>
</tr>
<?php if (isset($_POST['submit'])) { include 'db.php';
		 		$title=$_POST['title'] ;
				$author= $_POST['author'] ;					
				$name=$_POST['name'] ;
				$copy=$_POST['copy'] ;
											
	 mysql_query("INSERT INTO `books`(Title,Author,PublisherName,CopyrightYear) 
	 VALUES ('$title','$author','$name','$copy')"); 
			
			
        }

?>

		<?php
		include("db.php");
		
			
		$result=mysql_query("SELECT * FROM books");
		
		while($test = mysql_fetch_array($result))
		{
			$id = $test['BookID'];	
			echo "<tr align='center'>";	
			echo"<td><font color='black'>" .$test['BookID']."</font></td>";
			echo"<td><font color='black'>" .$test['Title']."</font></td>";
			echo"<td><font color='black'>". $test['Author']. "</font></td>";
			echo"<td><font color='black'>". $test['PublisherName']. "</font></td>";
			echo"<td><font color='black'>". $test['CopyrightYear']. "</font></td>";	
			echo"<td> <a href ='view.php?BookID=$id'>Edit</a>";
			echo"<td> <a href ='del.php?BookID=$id'><center>Delete</center></a>";
								
			echo "</tr>";
		}
		mysql_close($conn);
		?>

Page View Source code

<?php require("db.php"); $id =$_REQUEST['BookID']; $result = mysql_query("SELECT * FROM books WHERE BookID = '$id'"); $test = mysql_fetch_array($result); if (!$result) { die("Error: Data not found.."); } $Title=$test['Title'] ; $Author= $test['Author'] ; $PublisherName=$test['PublisherName'] ; $CopyrightYear=$test['CopyrightYear'] ; if(isset($_POST['save'])) { $title_save = $_POST['title']; $author_save = $_POST['author']; $name_save = $_POST['name']; $copy_save = $_POST['copy']; mysql_query("UPDATE books SET Title ='$title_save', Author ='$author_save', PublisherName ='$name_save',CopyrightYear ='$copy_save' WHERE BookID = '$id'") or die(mysql_error()); echo "Saved!"; header("Location: index.php"); } mysql_close($conn); ?> Untitled Document
Title:
Author
Publisher Name
Copyright Year
 

Can anyone help me by creating a simple login page and adding the value of that person who is logged on into the books table for which ever record he creates. Please not multiple user would access the same database at the same time…

Can anyone please help me…

Not sure what help you are asking for…

But, if you are talking about entering data from forms UNIQUE to the user, that is simple…
In your database where you have all of your data stored, just add a UserName field.

So, let’s say you have input fields for a book that has been read, you might save
Bookname, Dateread, whateverElseYouNeed…

Just make it,
Bookname, Dateread, whateverElseYouNeed, Username…

when a user logs in, keep his username in a SESSION variable and pull the data to be displayed only for the records that contain that username. The only data pulled would be those records that were created by that username.

Is that what you wanted? If not, give us a better idea of what you need.

Tats right wat u said. Since i am just a beginner i am not able to figure it out properly. So can you help me by creating a login form and the seesion where the user name can also be stored in the database

Well, there would be several steps and sections to accomplish this project. Here are some of them to get you started. Once you have most of it coded, ask for further help… Here are the steps in general…

  1. Add a new field to your books table, let’s call it CurrentUser.
    This field will have the UserName in it if the book is being used or blank if book is free for use.

  2. Create a log-in page. A log-in page is two simple text fields and a submit button. These are placed
    inside a and with an action pointing to a validation page that verifies the user info.

  3. Create a log-in validation page, let’s call it Login_Process.php. This file takes the $_POST[] info
    from the log-in page, reads the user info from the User table in the database and checks the
    password. If password is okay, then, it redirects to your viewing page to view the books.
    The userID is stored inside a session variable like this: $_SESSION[‘userid’]=$_POST[‘userid’];
    Then, on your book viewing page, when you select the books available, you add a “where” clause
    at the end of your query. ("WHERE CurrentUser = ‘’ ") This would select books not being used.

  4. The list that the user now sees is all books not being used. Once they select one, the viewing page
    calls a select-book-page that updates the books table with CurrentUser equal to the userid.
    When the user “returns” the book, you would have to set the CurrentUser for that book to “”…

Well… That’s one possible layout for this project. When you get all of this figured out and have problems, just post your questions and new code. I suggest doing one page at a time. Lay out all of the needed pages on paper first so you can see their flow. Then, program one at a time and then move onto the next one. Hope this helps and does not confuse you… Good luck…

Hi,

I am still not able to execute it properly. If you don’t mind can you help me out by sending me the source code.

I think i am not able to figure it out properly in the code. So can you help me out with the coding. Please…

I am not able to figure it out properly just because i am a beginner… So can you help me out with the source code please…

Did you add the “CurrentUser” to your table and set them all to “”?

Did you create a login page with passwords yet?

Did you alter the database request query to show only the books with the “CurrentUser” set to “”?

You are trying to create a project with users, show the user login page and validation PHP page is the first step. Get this working and when done, we can go to next step. If you have a certain question, ask away…
(Please show code segments of the parts you need help with!)

Sponsor our Newsletter | Privacy Policy | Terms of Service