Jump to content

Variables and forms... why doesn´t work??


lmcm2008

Recommended Posts

Hi: I´m creating a library code for myself, and I´m trying to create a page that shows all the books that I have in my database, and when I make click in one of them, go to another page and show all the details of that book that I have in my data base...

But I don´t know what I´m doing wrong... Can you help me??

 

My first page code:

 

<form method="POST" action="abrelibro.php">

<table border=0>
<tr>

<?php

// search all the books in the data base...

$cadena="SELECT * FROM libros";
$result=mysql_query($cadena) or die(mysql_error());

$totalreg=mysql_num_rows($result);
//echo "$totalreg";
$i=0;

while ($fila=mysql_fetch_array($result))
{
$iden=$fila['ID'];
$titulo=$fila['TITULO'];
$autor=$fila['AUTOR'];
$editorial=$fila['EDITORIAL'];
$anio=$fila['AÑO'];
$npaginas=$fila['NPAGINAS'];
$tipo=$fila['TIPO'];
$desc=$fila['DESCRIPCION'];
$alta=$fila['FECHA'];
$nuevo=$fila['NUEVO'];
$imagen=$fila['IMAGEN'];



	 if ( $i % 5 == 0 ) 
	 {
	 if ( $i == 1 )
	 {
	 echo "<tr>";
	 }
	 else
	 {
	 echo "</tr><tr>";
	 }
	 }

    // book image
    $libro="libros/$imagen.jpg";

echo "<td width=200>";
echo "<input type=hidden name=id_libro id=id_libro value=$iden>";
echo "<img src=$libro border=0><br>$desc";
echo "<input type=submit value=Submit>";

echo "</td>";


$i++;


} // del while


?>

</tr>
</table>
</form>

 

And the page that receive the code from the first one...

 

<?php
$milibro = $_POST["id_libro"];

echo "Received book: $milibro";

?>

 

Can you help me, please???

 

All the codes are when I have connection to the database done and not problem at all !!!!...

 

Thanks a lot.

 

Regards

Link to comment
Share on other sites

I´m receiving allways the number of books in my table... I mean.. If I have 3 books, I receive allways 3.. if I have 30 I receive 30... I know how many books I have...

 

BUT  I want to receive the ID of the book where I select to open the file of that book(ID) in the second file...

 

Thanks.

 

Link to comment
Share on other sites

Well...

IF you only want to 'see' 1 book on the 2nd page, you should either (A) use radio buttons on the form; OR (B) don't use a form, just simply display each book making the title a link to Page 2 placing the book id into the url.

 

AND on Page 2, you need to query the database using the id passed from page 1

Link to comment
Share on other sites

Yeah, you´re right...

But the thing that I´m trying to do is, to click in a book, and then with the $iden "(ID)" go to the second file and search all the details of that book in the database...

 

The first page shows all the books and a button for each book.... and clicking in that buttons then go to the second file... I dont like the get version with the url and i´m trying to do it using post and hiddens fields...

 

Maybe using sessions??

I really don´t know and I´m sure is easy but I´m block...

 

More suggestions?

 

Thanks...

 

Link to comment
Share on other sites

is there a particular reason you don't want to use GET? Seeing as it should simply be an integer, there would be no 'secrets' revealed.

 

As to using POST, you would need to 'encapsulate' each book in its own form so that the resulting POST would have the id for that book. As it is you have multiple submits for ONE form with ALL your form fields having the same name

Link to comment
Share on other sites

I´m really sorry, but I don´t understand what are you telling me...

 

You say something like this???

 

echo "<td width=200>";
echo "<form method=POST action=abrelibro.php>";
echo "<input type=hidden name=id_libro id=id_libro value=$iden>";
echo "<img src=$libro border=0>";
echo "<input type=submit value=Submit>";

 

thanks...

 

 

 

Link to comment
Share on other sites

Almost there. You need to close each form. Example without the use of table...

while($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$author = $row['author'];
?>
<form action="page2.php" method="post">
	<input type="hidden" name="bookid" value="<?PHP echo $id; ?>"><br />
	<?PHP
	echo $title . " - " . $author . "<br />";
	?>
	<input type="submit" value="submit">
</form>
<?PHP
}

Link to comment
Share on other sites

Hi, great, but I do this:

?>

<td width=200>
<form method=POST action=abrelibro.php>
<input type="hidden" name="id_libro" id="id_libro" value="<?PHP echo $iden; ?>">
<img src=<?php echo $libro ?> border=0>
<input type=submit value=Submit>
</td>

<?php

But same result... doesn´t work as I want... I don´t send the $iden to the second page that is the value that I want to receive there...

More ideas??

 

Thanks a lot for your help...

 

Link to comment
Share on other sites

In your browser, look at the source for page 1. Make sure that the values for the fields are as you expect them to be. If so then use the following (temporarily) for your page 2.

 

<?php
$id = (int) $_POST['id_libro'];
$query = "SELECT * FROM libros WHERE ID = '$id'";
$result=mysql_query($query) or die(mysql_error());
$fila=mysql_fetch_array($result);
echo "<PRE>";
print_r($fila);
echo "</pre>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.