0

I want to display form data on another page so that when I click "More View" button and the inputed data of each user will display on another page. This is the code of the HTML form:

registerbooks.php
<!DOCTYPE Html>
<<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Register books</title>

    <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
</head>
<body>


 

<div id='main-content'>

    <form action="connect.php" method="POST">
         <input type="text" name="tittle" placeholder="Book Tittle"></br>
         <input type="text" name="author" placeholder="Author"></br>
          
         <input type="text" name="copies" placeholder="Copies Available"></br>
        <button type="submit"  name="submit">submit</button>
        <button type="reset" value="Reset">Reset</button>
    </form>

</div> 
</body>
</html>

This is the connection.php

<?php



$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'list';


// Create connection
$connect = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$connect) {
  die("Connection failed: " . mysqli_connect_error());
}

if (isset($_POST['submit'])){

   $Tittle = $_POST['tittle'];
     $Author = $_POST['author'];
     $Copies = $_POST['copies'];

     $query = "INSERT INTO books(Tittle,Author,Copies) VALUES('$Tittle' , '$Author' , '$Copies')";
   $result=mysqli_query($connect, $query); 
    if($result){
        echo 'Available Books updated';
    }
    else{
        echo "Failed to update";
    }
}

?>

The fom is displayed in this page:

Availablebooks.php
<!DOCTYPE HTML>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Available books</title>
</head>
<body>
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'list';
$connect = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$connect) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM books";
$result=mysqli_query($connect, $sql);


?>

      <table align="centre" border="1px" width="600px" line-height="30px" >
            <tr>
                <th colspan="4">Available books</th>
            </tr>

            <tr>
                <th>ID</th>
                <th>Book tittle</th>
                <th>Author</th>
                 
                <th>More Details</th>
            </tr>;
         <?php
         
         while ($rows = $result->fetch_assoc())
          {
        
         ?>   
            <tr>
                <td><?php echo $rows['ID']; ?> </td>
                <td> <?php echo $rows['Tittle']; ?>   </td>
                <td> <?php echo $rows['Author']; ?>  </td>
                 <td> <button type="submit"><a href="http://localhost:80/orders.php/?id=<?php echo $rows['ID']; ?>">More Views</a></button> </td>
            </tr>
        <?php
         }
        ?> 
          
    </table>    

</body>
</html>

When I click "more views" button, I want each user data to be displayed in "orders.php"

<!DOCTYPE HTML>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Available books</title>
</head>
<body>
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'list';
$connect = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$connect) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM books WHERE id=(Tittle,Author,Copies )";
$result=mysqli_query($connect, $sql);


?>

 

<table align="left" border="1px" width="600px" line-height="30px" >
            <tr>
                <th colspan="4">Ordered  books</th>
            </tr>

            <tr>
                <th>ID</th>
                <th>Book tittle</th>
                <th>Author</th>
                <th>Number of copies</th>
                 
            </tr>
          
           <?php
         
         while ($rows = $result->fetch_assoc())
          {
        
         ?>  

            <tr>
                <td><?php echo $rows['ID']; ?> </td>
                <td><?php echo $rows['id']; ?> </td>
                <td> <?php echo $rows['id']; ?>  </td>
                <td> <?php echo $rows['id']; ?>  </td>
                 

      </tr>
          
            <?php
         }
        ?> 
          
    </table>    

    </body>
     </html>

But the code is not working. Please help me to resolve this issue. Thanks

  • Welcome. What is `SELECT * FROM books WHERE id=(Tittle,Author,Copies )` supposed to select? Have you tried this query in phpMyAdmin or similar? Be a bit more specific, "_But the code is not working_" doesn't really help. What doesn't work? Blank page? Do you get an error, if so which error? – brombeer Sep 04 '21 at 07:15
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 04 '21 at 15:57
  • Thanks. I expect order.php to display the form data that each user sends when "more views" is clicked – Ovie Peter Sep 05 '21 at 00:05

0 Answers0