echo ("<tr id='$a' bgcolor='B0C4DE'>"); 
		echo ('<td>'.$row['idbook_code'].'</td>');
		echo ('<td>'.$row['b_name'].'</td>');
		echo ('<td>'.$row['b_author'].'</td>');
		echo ('<td>'.$row['b_avail'].'</td>');
		if($row['b_avail']>0)
		{
				if (!$_SESSION["valid_user"])
        {
        // User not logged in, redirect to login page
		echo("<td>Available</td>");
		}else
echo("<td><INPUT TYPE=button  id=show  name=show value=add></td>");
		}else
		echo("<td>No stock of book</td>");
		echo("</tr>");
		
		?>

i have my search area when i search it output the total item will be in the table so when i click the button i should get the idbook of the clicked row i display it on the same page where what row was clicked

any snippet will do the help

Recommended Answers

All 29 Replies

Any punctuation marks will help, too.
Set the value of the button to the book id you want to retrieve.

echo("<td><INPUT TYPE=button  id=show  name=show value='" . $row['idbook_code'] . "'></td>");

Any punctuation marks will help, too.
Set the value of the button to the book id you want to retrieve.

echo("<td><INPUT TYPE=button  id=show  name=show value='" . $row['idbook_code'] . "'></td>");

then how can i know what row is click the code you post was only for showing the value of a row what i need is when i clicked the button it will add like add to cart program any idea

Do you mean you have a list of items, user selects and submit, page displays?

If this is the case, you can have a radio button with values that references to the item's id (or something unique).

Do you mean you have a list of items, user selects and submit, page displays?

If this is the case, you can have a radio button with values that references to the item's id (or something unique).

what do you mean about the radio button??? im using only button when users click the row it will add it on my add to cart but in my case i cant add the clicked row can you give me a snippet of a code

For a cart store the item IDs in an array as a session variable.

For a cart store the item IDs in an array as a session variable.

can you give me sir an example of what are you trying to say coz's i cant figured it out???

You mean that every row has a button?

In that case, every row can be a form on it's own.

An easy way out:

<table>
<tr><td>Item</td><td>Price</td><td></td></tr>
<form action="" method="POST"><input type="hidden" name="selection" value="001"><tr><td>item 1</td><td>$1.99</td><td><input type="submit" value="Add to Cart"></td></tr></form>
<form action="" method="POST"><input type="hidden" name="selection" value="002"><tr><td>item 2</td><td>$2.99</td><td><input type="submit" value="Add to Cart"></td></tr></form>
</table>

In this case, every SUBMIT has a SELECTION attached. This value should be something that uniquely identifies the product that the person wants to add to the cart.

On the process page, create a query and retrieves the information about the SELECTION that you will want to display.

An easy way out:

<table>
<tr><td>Item</td><td>Price</td><td></td></tr>
<form action="" method="POST"><input type="hidden" name="selection" value="001"><tr><td>item 1</td><td>$1.99</td><td><input type="submit" value="Add to Cart"></td></tr></form>
<form action="" method="POST"><input type="hidden" name="selection" value="002"><tr><td>item 2</td><td>$2.99</td><td><input type="submit" value="Add to Cart"></td></tr></form>
</table>

In this case, every SUBMIT has a SELECTION attached. This value should be something that uniquely identifies the product that the person wants to add to the cart.

On the process page, create a query and retrieves the information about the SELECTION that you will want to display.

your code is not the one im looking for please see how do i retrieve my data and how to output it

I don't have your DB, page flows.. It's just codes of concepts..

Basically if you submit the first row, you can get $_POST = 001.

With this you can query your DB/tables for '001' and retrieve any other related information.

I don't have your DB, page flows.. It's just codes of concepts..

Basically if you submit the first row, you can get $_POST = 001.

With this you can query your DB/tables for '001' and retrieve any other related information.

if i want to get the value of a row can you tell me or do you have any idea to retrieve data from db when button is clicked. thanks for your time..

1. change the value in the selection to the ID referencing to your item in your DB

<table>
<tr><td>Item</td><td>Price</td><td></td></tr>
<form action="" method="POST"><input type="hidden" name="selection" value="001"><tr><td>item 1</td><td>$1.99</td><td><input type="submit" value="Add to Cart"></td></tr></form>
<form action="" method="POST"><input type="hidden" name="selection" value="002"><tr><td>item 2</td><td>$2.99</td><td><input type="submit" value="Add to Cart"></td></tr></form>
</table>

2. you can retrieve the selection by

$selection = $_POST['selection']

3. you can use this to query your database for the values you want to display

$query = "SELECT column1, column2 FROM table_name WHERE id = '$selection'";
$result = mysql_query($query);

These are the steps but I can't help you with a WORKING one without knowing your DB structure etc. You need to try and apply these to your project.

1. change the value in the selection to the ID referencing to your item in your DB

<table>
<tr><td>Item</td><td>Price</td><td></td></tr>
<form action="" method="POST"><input type="hidden" name="selection" value="001"><tr><td>item 1</td><td>$1.99</td><td><input type="submit" value="Add to Cart"></td></tr></form>
<form action="" method="POST"><input type="hidden" name="selection" value="002"><tr><td>item 2</td><td>$2.99</td><td><input type="submit" value="Add to Cart"></td></tr></form>
</table>

2. you can retrieve the selection by

$selection = $_POST['selection']

3. you can use this to query your database for the values you want to display

$query = "SELECT column1, column2 FROM table_name WHERE id = '$selection'";
$result = mysql_query($query);

These are the steps but I can't help you with a WORKING one without knowing your DB structure etc. You need to try and apply these to your project.

my database is compose of student table with user_id and pass and book table with idbook
title , author ,book_avail that all in my database what i need is when student click the button it will add to cart and they will see how many they clicked and retrieve what button they clicked

Ok. Do you know how to connect to DB? And can you provide two idbook values.

Ok. Do you know how to connect to DB? And can you provide two idbook values.

yes i know how to connect the only problem is retrieving values example of idbook is 5678902 and 1234780

This is something simple but it should work. Change accordingly.

<?php
//connect to DB

//check for $_POST
if ($_POST) {
	$idbook = $_POST['idbook'];
	
	$query = "SELECT * FROM book WHERE idbook = '$idbook'";
	$result = mysql_query($query);
	
	if (mysql_num_rows($result) == 1) {
		while ($row = mysql_fetch_assoc($result)) {
			echo "You have selected " .$row['title']. "(by " .$row['author']. ") - " .$row['book_avail'];
		}
	}
	else {
		//some error
	}
}
?>

<table>
<form action="" method="POST"><input type="hidden" name="idbook" value="5678902"><tr><td>This is for IDBOOK 5678902</td><td><input type="submit" value="Add to Cart"></td></tr></form>
<form action="" method="POST"><input type="hidden" name="idbook" value="1234780"><tr><td>This is for IDBOOK 1234780<td><input type="submit" value="Add to Cart"></td></tr></form>
</table>

<?php
//close DB
?>

This is something simple but it should work. Change accordingly.

<?php
//connect to DB

//check for $_POST
if ($_POST) {
	$idbook = $_POST['idbook'];
	
	$query = "SELECT * FROM book WHERE idbook = '$idbook'";
	$result = mysql_query($query);
	
	if (mysql_num_rows($result) == 1) {
		while ($row = mysql_fetch_assoc($result)) {
			echo "You have selected " .$row['title']. "(by " .$row['author']. ") - " .$row['book_avail'];
		}
	}
	else {
		//some error
	}
}
?>

<table>
<form action="" method="POST"><input type="hidden" name="idbook" value="5678902"><tr><td>This is for IDBOOK 5678902</td><td><input type="submit" value="Add to Cart"></td></tr></form>
<form action="" method="POST"><input type="hidden" name="idbook" value="1234780"><tr><td>This is for IDBOOK 1234780<td><input type="submit" value="Add to Cart"></td></tr></form>
</table>

<?php
//close DB
?>

thanks for the code sir but i think this is not what im trying to do my button is equal to item that users search but i put a limit in my query so it only retrieve 10 items per search what im trying to do is the how do i know what button is clicked ("Thanks for your time sir") ^_^ hope you can help me

try:

<?php
//connect to DB

//check for $_POST
if ($_POST) {
	$idbook = $_POST['idbook'];
	
	$query = "SELECT * FROM book WHERE idbook = '$idbook'";
	$result = mysql_query($query);
	
	if (mysql_num_rows($result) == 1) {
		while ($row = mysql_fetch_assoc($result)) {
			echo "You have selected " .$row['title']. "(by " .$row['author']. ") - " .$row['book_avail'];
		}
	}
	else {
		//some error
	}
}

$query = "SELECT id_book, title FROM book LIMIT 0, 10;";
$result = mysql_query($query);

echo "<table>";

while ($row = mysql_fetch_assoc($result)) {
	$idbook = $row['idbook'];
	$title = $row['title'];
	echo "<form action='' method='POST'><input type='hidden' name='idbook' value='$idbook'><input type='submit' value='$title'></form>";
}

echo "</table>";
?>

<?php
//close DB
?>

what if i clicked 3 button how do i store 3 idbook??

Line 20: Queries DB for 10 books.
Line 26-30: Display all the 10 books, dynamically setting the values in the form/button.

Maybe you want to describe the flow of your application so that we can visualise it.

try:

<?php
//connect to DB

//check for $_POST
if ($_POST) {
	$idbook = $_POST['idbook'];
	
	$query = "SELECT * FROM book WHERE idbook = '$idbook'";
	$result = mysql_query($query);
	
	if (mysql_num_rows($result) == 1) {
		while ($row = mysql_fetch_assoc($result)) {
			echo "You have selected " .$row['title']. "(by " .$row['author']. ") - " .$row['book_avail'];








		}
	}
	else {
		//some error
	}
}

$query = "SELECT id_book, title FROM book LIMIT 0, 10;";
$result = mysql_query($query);

echo "<table>";

while ($row = mysql_fetch_assoc($result)) {
	$idbook = $row['idbook'];
	$title = $row['title'];
	echo "<form action='' method='POST'><input type='hidden' name='idbook' value='$idbook'><input type='submit' value='$title'></form>";
}

echo "</table>";
?>

<?php
//close DB
?>

this is the code im looking for the problem is how do i store 3 idbook when user clicked 3 button

store where my friend? which table? and I don't see any column meant for storing the selection.

idbook in student table perhaps?

store where my friend? which table? and I don't see any column meant for storing the selection.

idbook in student table perhaps?

perhaps make another table to store all reserve books that user clicked oh by the way im making online library when users clicked button it will add to cart and when they checkout the cart it will store in a database only problem is how can i store 3 or more values when user clicked

within the if($_POST) { } that is where the selection is read. Thereafter, you can insert new data into the new table, or update existing data.

3 or more values refers to..?
- 3 or more books per student?
- 3 values of the same book (but store where?)?

You need to consider your DB structure carefully.

within the if($_POST) { } that is where the selection is read. Thereafter, you can insert new data into the new table, or update existing data.

3 or more values refers to..?
- 3 or more books per student?
- 3 values of the same book (but store where?)?

You need to consider your DB structure carefully.

3 or more button and maximum reservation of books is 3

In that case I will suggest that in book table, there is a column which references the user_id.

So you update the column whenever a book is selected.
1. set the user_id in book
2. update book_avail

You will also need to do checking on the number of books borrowed.

In that case I will suggest that in book table, there is a column which references the user_id.

So you update the column whenever a book is selected.
1. set the user_id in book
2. update book_avail

You will also need to do checking on the number of books borrowed.

what do you mean in book should have user_id??
or should i have another table ???

I would make a third table but just to keep it simple and workable, what I'm suggesting is to add a column in BOOK that references to the STUDENT.

For example when the student checks out, this COLUMN will be updated with the user_id. This simply means the book is taken and not available.

To check if the student can check out another book, you need to query for the number of books borrowed (which is also the number of rows with his user_id).

Remember to remove his user_id once the book is returned.

I would make a third table but just to keep it simple and workable, what I'm suggesting is to add a column in BOOK that references to the STUDENT.

For example when the student checks out, this COLUMN will be updated with the user_id. This simply means the book is taken and not available.

To check if the student can check out another book, you need to query for the number of books borrowed (which is also the number of rows with his user_id).

Remember to remove his user_id once the book is returned.

thanks for the idea but can you show me what if i want the user view how many books they reserve in the same page where they add reserve books

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.