0

i want to send the cookie to another page that store the id from database retrieve value . the thing is it is send to the next page and also working. there is book record retrieve from database with image and one button . i have store the id of the book. but when it goes to next page it displays first book id no matter which book i select by the button

this is code where i set cookie

echo "<form action='issue.php' method='get'>";
                while ($row= mysqli_fetch_array($result)) {

                    echo "<div id='img_div'> ";
                    echo "<img src='books/".$row['image']."'>";
                    // echo "</div>";<div id='text'>

                    echo "&nbsp;&nbsp;&nbsp;&nbsp;<table>";


                    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;NAME</td><td>&nbsp;&nbsp;&nbsp;&nbsp;".$row['name']."</td></tr>";
                    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;AVAILABILITY</td><td>&nbsp;&nbsp;&nbsp;&nbsp;".$row['availabilty']."</td></tr>";
                    echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;CATEGORY</td><td>&nbsp;&nbsp;&nbsp;&nbsp;".$row['category']."</td></tr>";
                    echo "<tr><td colspan='2'>

                    <button type='submit' name='issue'>issue</button></td></tr>";
                    echo "</table>";
                    echo "</div><br/>";
                    if (isset($_GET['issue'])) {
                        # code...
                        $bookid=$row['isbn'];
                    setcookie("bid",$bookid);
                        if(!isset($_COOKIE['bid'])){
                            echo "COOKIE NOT SET";
                        }
                        else{ 
                            echo "COOKIE SET SUCCESSFULLY";
                        }
                    }

And this is code where i access the cookie next page

$id=$_SESSION['user']['username'];
                    $query = "select id from user_account where username='$id'";
                    $result=mysqli_query($con,$query);
                    $row= mysqli_fetch_assoc($result);
                        $uid=$row['id'];
                    // if (mysqli_num_rows($results) == 1) {
                    //  $row= mysqli_fetch_assoc($results);
                    // 
                    // }

                    $bookid=$_COOKIE['bid'];
                    echo "$uid";
                    echo "<br/>";
                    echo "$bookid";
                            $query = "INSERT INTO issue (bookid, userid)
                 VALUES ('$bookid', '$uid')";
             mysqli_query($con, $query)or die(mysqli_error($con));
Sfili_81
  • 2,377
  • 8
  • 27
  • 36
sid
  • 21
  • 5
  • Where do you retrieve the value of the cookie? in the same page? – Sfili_81 Nov 18 '19 at 16:05
  • no i retrieve it in a different page – sid Nov 18 '19 at 16:06
  • I see you echo the cookie value, what do you get? – Sfili_81 Nov 18 '19 at 16:10
  • But rember you are inside a loop. So every loop you overwrite the value of the cookie. So try to set your cookie outside the while loop. – Sfili_81 Nov 18 '19 at 16:12
  • i get id 1 of book from database – sid Nov 18 '19 at 16:16
  • but it is actually selecting the current book from loop so how can i set cookie outside the loop – sid Nov 18 '19 at 16:17
  • I don't understand why you use a form... yo can use a link to the issue.php page whit a parameter like this. issue.php?isbn=$yourisbn. and in the issue.php page you can get that value with $_GET[] – Sfili_81 Nov 18 '19 at 16:21
  • book data is displayed in table so how can i use $_GET value to retrieve the bookid for which i press issue button – sid Nov 18 '19 at 16:25
  • issue.php?isbn=300 in your issue.php you can retrieve that with $_GET['isbn']. So you can create a lot of link with different isbn paramteres. [read](https://stackoverflow.com/questions/871858/php-pass-variable-to-next-page) – Sfili_81 Nov 18 '19 at 16:27

0 Answers0