1

Right now i'm trying to build an online book store and for selling a book the user has to search by the Book title, ISBN and Author name , My Question is after the search result appears which is generated by a while loop how can i get the books Detail after the user clicked on Buy Button and Sale it?

Here is The SearchResult Code

if($searchID == 2)
    {
            $searchResult = mysqli_query($bookConn , "SELECT * FROM bookt WHERE request = 'Approved' AND bookTitle LIKE '%".$searchtext."%'" ) OR die();
        while($row=mysqli_fetch_array($searchResult))
         {
                ?>


        <!-- Page Content -->
        <div class="container" style="display:block;">
        <hr>


            <!-- /.row -->

            <!-- Page Features -->
            <div class="row text-center">

                <div class="col-sm-4 col-lg-4 col-md-4">
                    <div class="thumbnail">
                        <img src="http://plnami.blob.core.windows.net/media/2015/02/books021715-800x500.jpg" alt="">
                        <div class="caption">
                          <h3>Book Title:<?php  echo $row['bookTitle']; ?></h3>
                            <p>ISBN: <?php  echo $row['ISBN'];          ?></p>
                            <p>Publication Year<?php    echo $row['bookPublicationYear'];  ?> </p>
                            <p>Book Status:<?php echo $row['bookStatus']; ?></p>
                            <p>Book Price: <?php echo  $row['bookPrice']; ?></p>
                            <p>
                                <a href="buyingProcess.php" class="btn btn-primary">Buy Now!</a> 
                            </p>
                        </div>
                    </div>
                </div>

            </div>
            <!-- /.row -->



        </div>
        <!-- /.container -->

            <?php



        }
    }
hurricane
  • 6,521
  • 2
  • 34
  • 44
ShkarM
  • 43
  • 1
  • 7
  • Hellow - U could generate a form during the loop of SearchResult assigining the primary key as value of submit button. Then in buyprocess.php u could know which one user clicked. If anyone got a smoother solution ? Otherwise i would like to see ur loop for SearchResult – Falt4rm May 24 '15 at 08:32

1 Answers1

1

In the href of the link put identifying information that you can use on the sell page to look it up using SQL. For example:

<a href="buyingProcess.php?isbn=<?php echo row['ISBN'] ?>" class="btn btn-primary">Buy Now!</a> 

Then on the buyingProcess.php page, you can run something like

    if(isset($_GET['isbn'])){
        $isbn = mysqli_real_escape_string($bookConn , $_GET['isbn']) ;
    }

    $book = mysqli_query($bookConn , "SELECT * FROM bookt WHERE isbn='".$isbn."') OR die(); 

And do with that what you will. $book is an object containing all of the information. If you have a cart, you can add it to the cart array. Or you could go straight to the buying process.

EDIT

So you want to pass user input as well, which means you'll need a form. The reason your form isn't working is likely because you don't have a submit button. So your form should look like

<form action='order.php' method='post'>
    <input type='text' name='orderQuantity' id='orderQuantity' size='10' required placeholder='Order Quantity'>
    <button type='submit' name='submit'>Submit</button>
</form>

Then on order.php

if(isset($_POST['submit'])){
    $quantity = mysqli_real_escape_string( $bookCon , trim($_POST['orderQuantity'])) ;
}

You should also probably put in a JavaScript check on submit that doesn't allow the submit button to be clicked if the input isn't a number.

Newspire
  • 104
  • 1
  • 7
  • This is a good answer, but there remains the danger of a SQL-injection. Depending on how this code is going to be used, it might become a problem. See [this question](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1) for details. – tarleb May 24 '15 at 08:43
  • Thanks for Your replay @Newspire but what if i want to pass another **parameter** such as **Order Quantity** as an input from the Customer with the is it possible ?! – ShkarM May 24 '15 at 16:05
  • @ShkarM For that you'll need to put this all in a form and use the POST method. – Newspire May 24 '15 at 20:48
  • @Newspire it won't allow this is my **code**
    and i get the value of the input from another page like that $quantity=$_POST['orderQuantity']; and its says Undefined index: orderQuantity
    – ShkarM May 24 '15 at 21:54
  • @Newspire appreciate you editing but i already have another button which is Order and if i put another button inside the form it wont make sense! can i pass the input value with this anchor Order! – ShkarM May 24 '15 at 22:26