lmcm2008 Posted January 8, 2012 Share Posted January 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254603-variables-and-forms-why-doesn%C2%B4t-work/ Share on other sites More sharing options...
litebearer Posted January 8, 2012 Share Posted January 8, 2012 what error are you experiencing? Quote Link to comment https://forums.phpfreaks.com/topic/254603-variables-and-forms-why-doesn%C2%B4t-work/#findComment-1305565 Share on other sites More sharing options...
lmcm2008 Posted January 8, 2012 Author Share Posted January 8, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254603-variables-and-forms-why-doesn%C2%B4t-work/#findComment-1305567 Share on other sites More sharing options...
litebearer Posted January 8, 2012 Share Posted January 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254603-variables-and-forms-why-doesn%C2%B4t-work/#findComment-1305569 Share on other sites More sharing options...
lmcm2008 Posted January 8, 2012 Author Share Posted January 8, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/254603-variables-and-forms-why-doesn%C2%B4t-work/#findComment-1305572 Share on other sites More sharing options...
litebearer Posted January 8, 2012 Share Posted January 8, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254603-variables-and-forms-why-doesn%C2%B4t-work/#findComment-1305573 Share on other sites More sharing options...
lmcm2008 Posted January 8, 2012 Author Share Posted January 8, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/254603-variables-and-forms-why-doesn%C2%B4t-work/#findComment-1305575 Share on other sites More sharing options...
litebearer Posted January 8, 2012 Share Posted January 8, 2012 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 } Quote Link to comment https://forums.phpfreaks.com/topic/254603-variables-and-forms-why-doesn%C2%B4t-work/#findComment-1305577 Share on other sites More sharing options...
lmcm2008 Posted January 8, 2012 Author Share Posted January 8, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/254603-variables-and-forms-why-doesn%C2%B4t-work/#findComment-1305602 Share on other sites More sharing options...
litebearer Posted January 8, 2012 Share Posted January 8, 2012 show us your 2nd page Quote Link to comment https://forums.phpfreaks.com/topic/254603-variables-and-forms-why-doesn%C2%B4t-work/#findComment-1305608 Share on other sites More sharing options...
litebearer Posted January 8, 2012 Share Posted January 8, 2012 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254603-variables-and-forms-why-doesn%C2%B4t-work/#findComment-1305609 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.