0

for example, the value that needs to be passed is "The Dog's Owner" why when passing the data to the query it only recognizes the "The Dog".


here's the code of the index

  <?php
                        $query = "SELECT * FROM bookinfo";

                        if ($result = $conn->query($query)) {
                            $count = 0;
                            while ($row = $result->fetch_assoc()) {
                                $id = $row["id"];
                                $tname = $row["btitle"];
                                $quantity = $row["quantity"];
                                $name = htmlspecialchars($tname, ENT_QUOTES);
                                echo "<tr>";
                                echo "<td style='margin:20px;'>" . $id . "</td>";
                                echo "<td><button name='submit' type='submit' value='" . $name . "'>" . $name . "</button>";
                                echo "<td>" . $quantity . " left</td>";
                                echo "</tr>";
                                ++$count;
                            }
                            $result->free();
                        }
                        ?>

the code to start the session

<?php
if (isset($_POST['submit'])) {
    session_start();
    require 'include.dbh.php';
    $_SESSION['sss'] = htmlspecialchars($_POST['submit'], ENT_QUOTES);
    header("Location: ../books.php");
} else {
    header("Location: index.php");
    exit();
}

and the code to initiate the commands. it supposes to do a SQL query to search the book base on the session

<?php
    session_start();
    if (!isset($_SESSION['sss'])) {
        header("Location: index.php");
        exit();
    } else if (!isset($_SESSION['userID'])) {
        header("Location: ../login.php");
        exit();
    } else {
        $butt = $_SESSION['sss'];
        require 'php/include.dbh.php';

        // -------------------------
        echo $butt;
        $read = $conn->prepare("SELECT bid, btitle, bdesc, quantity, imgpath FROM bookinfo WHERE btitle=?");
        $read->bind_param("s", $butt);
        $read->execute();
        $read->store_result();
        if ($read->num_rows > 0) {
            $read->bind_result($id, $bookname, $desc, $q, $img);
            $read->fetch();
            $read->close();
        }
        // -------------------------

        // $sql = "SELECT * FROM bookinfo WHERE btitle='" . $butt . "'";
        // echo $sql;
        // if ($result = $conn->query($sql)) {

        //     while ($row = $result->fetch_assoc()) {
        //         $ID = $row['bid'];
        //         $bookname = $row['btitle'];
        //         $desc = $row['bdesc'];
        //         $q = $row['quantity'];
        //         $img = $row['imgpath'];
        //     }

        //     /*freeresultset*/
        //     $result->free();
        // }
        // $a = "SELECT bid FROM borrower WHERE gid='" . $bookname . "'";
        // if ($result = $conn->query($a)) {
        //     while ($row - $result->fetch_assoc()) {
        //         $b = $row['bid'];
        //     }
        // }
    }
    ?>

 <!DOCTYPE html>
 <html>

 <head>
     <link rel="stylesheet" href="css/up.css">
     <link rel="icon" href="img/favicon.jpg">
     <meta name="viewport" content="width=device-width">
     <script>
     var today = new Date();
     var dd = String(today.getDate()).padStart(2, '0');
     var mm = String(today.getMonth() + 1).padStart(2, '0');
     var yyyy = today.getFullYear();
     today = mm + '-' + dd + '-' + yyyy;
     document.getElementById('dob').value = Date();
     </script>
 </head>

 <body>
     <div class="content">
         <div class="header">
             <table class="heading">
                 <tr>
                     <td>
                         <span class="title">Seshat</span>
                         <span class="tag"> | a faster way to record books</span>
                     </td>
                     <td style="text-align: right;">
                         <span class="title">Book Details</span>
                     </td>
                 </tr>
             </table>
             <hr>

             <div class="cheader">Book Details</div>
             <div class="cbody">
                 <div class="booktitle">
                     <table>
                         <tr>
                             <td>
                                 <label for="">Title: </label>
                                 <span><?php echo $bookname; ?></span>
                             </td>
                             <td>
                                 <label for="">Book ID:</label>
                                 <span name="bid"><?php echo $ID; ?></span>
                             </td>
                         </tr>
                     </table>
                 </div>
                 <hr>
                 <div>
                     <label for="">total quantity: </label>
                     <span><?php echo $q; ?></span>
                 </div>
                 <br>
                 <div class="description">
                     <label for="">Description </label>
                     <div>
                         <p>
                             <?php
                                echo $desc;
                                ?>
                         </p>
                     </div>
                 </div>
                 <div class="buttons">
                     <div onclick="show()" id="s" name="borrow">Borrow</div>
                     <form action="php/include.discard.php" method="POST">
                         <button value="<?php echo $bookname; ?>" type="submit" name="discard"
                             title="Delete this Book">Discard</button>
                     </form>
                 </div>
             </div>
             <div class="cheader">Borrowers</div>
             <div class="cbody">
                 <form action="php/include.return.php" method="POST">
                     <div class="users">
                         <table border="1">
                             <thead>
                                 <td id="asize">Book ID</td>
                                 <td id="asize">Name</td>
                                 <td id="asize">Grade And Section</td>
                                 <td id="asize">LRN</td>
                                 <td id="asize">Borrowed date</td>
                                 <td id="asize">Date Of expected return</td>
                                 <td id="asize" class="v">Actions</td>
                             </thead>

                             <!-- ----------------------------------- -->

                             <?php
                                $sqlb = "SELECT * FROM borrower where gid='" . $butt . "'";
                                if ($result = $conn->query($sqlb)) {
                                    while ($row = $result->fetch_assoc()) {
                                        $field1name = $row["bid"];
                                        $field2name = $row["fname"] . " " . $row["lname"];
                                        $field3name = $row["grade"] . " - " . $row["section"];
                                        $field4name = $row["lrn"];
                                        $field5name = $row["dob"];
                                        $field6name = $row["der"];
                                        $id = $row['id'];
                                        echo '<tr> 
                                            <td>' . $field1name . '</td> 
                                            <td>' . $field2name . '</td> 
                                            <td>' . $field3name . '</td> 
                                            <td>' . $field4name . '</td> 
                                            <td>' . $field5name . '</td> 
                                            <td>' . $field6name . '</td> 
                                            <td><button type="submit" name="return" value="' . $field1name . '" class="edit" title="return the books">Return</button></td>
                                        </tr>';
                                    }
                                    $result->free();
                                }
                                ?>

                             <!-- ------------------------------------------- -->

                         </table>
                     </div>
                 </form>
             </div>
             <br>
             <hr><br>
             <div id="f">
                 <div class="cheader">Borrow Form</div>
                 <div class="cbody" id="form">
                     <span style="margin:20pt 20pt;">Student</span>
                     <form class="" action="php/include.book.php" method="post">

                         <div class="student">
                             <table class="formt">
                                 <tr>
                                     <td>
                                         <span>First Name</span>
                                         <br>
                                         <input id="nem" type="text" name="fn" value="">
                                     </td>
                                     <td>
                                         <span>Last Name</span>
                                         <br>
                                         <input type="text" name="ln" value="">
                                     </td>
                                     <td>
                                         <span>Grade And Section</span><br>
                                         <input style="width:30%;" name="g" type="number" placeholder="Grade" value="">
                                         <input style="width:50%;" type="text" placeholder="Section" name="s" value="">
                                     </td>
                                     <td>
                                         <span style="font-size:10pt">Learner's Reference Number</span>
                                         <br>
                                         <input type="text" name="lrn" value="">
                                     </td>
                                 </tr>
                             </table>
                         </div>
                         <span style="margin:20pt 20pt;">Book</span>
                         <div class="student">
                             <table class="formt">
                                 <tr>
                                     <td>
                                         <span>Book ID</span>
                                         <br>
                                         <input type="text" name="bid">
                                     </td>
                                     <td>
                                         <span style="font-size: 8pt;">Date of Borrowing and the day of expected
                                             return</span>
                                         <br>
                                         <input type="date" id="dob" name="dob" style="width:40%;">
                                         <input type="date" name="der" style="width:40%;">
                                     </td>
                                 </tr>
                             </table>
                         </div>
                         <br>
                         <div class="buttons">
                             <button type="submit" <?php echo "value='" . $butt . "'"; ?> name="submit">Submit</button>
                             <button type="reset">Reset</button>
                         </div>
                 </div>
                 </form>
             </div>
             <div id="last">
                 <form action="php/include.book.php" method="POST" enctype="multipart/form-data">
                     <div id="bt" class="buttons">
                         <button type="submit" name="back">Back to Main</button>
                     </div>
                 </form>
             </div>
         </div>
     </div>
     </div>
     <script type="text/javascript">
     function show() {

         if (document.getElementById("f").style.height < "384px") {
             document.getElementById("f").style.transition = ".5s";
             document.getElementById("f").style.height = "384px";
             document.getElementById("f").style.opacity = "1";
             document.getElementById("s").innerHTML = "Close";
             document.getElementById("bt").style.top = "0pt";
         } else {
             document.getElementById("f").style.transition = ".5s";
             document.getElementById("f").style.height = "0px";
             document.getElementById("f").style.display = "0";
             document.getElementById("s").innerHTML = "Borrow";
             document.getElementById("bt").style.top = "-20pt";


         }
     }
     </script>
 </body>

 </html>
Jason
  • 3
  • 2
  • **Too much code.** You need to do a better job of troubleshooting this yourself. We are *not* debuggers. You need to **isolate the problem** and debug from there. If you're stuck, provide a **clear explanation of what isn't working** with a **[mcve]**. I suggest reading **[ask]** a good question and **[the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)**. Also, be sure to take the **[tour]** and read **[this](https://meta.stackoverflow.com/questions/347937/im-new-to-stack-overflow-what-are-some-things-i-should-do-and-what-things-wil)**. – Blue Nov 25 '19 at 14:10
  • You are on the right track when using `$conn->prepare`. Just use `$conn->prepare` everywhere instead of concatenating input (like in `"SELECT * FROM borrower where gid='" . $butt . "'"`) – Adder Nov 25 '19 at 14:12
  • im sorry @FrankerZ im just studying it 2 days ago and it is just i was forced to do php with small amount of time left so i dont have the time to read this and that . but im trying my best. :D – Jason Nov 25 '19 at 14:17

1 Answers1

-1

Maybe you can use function addslashes($string) so it will add slash to every single quote "The Dog\'s Owner"

  • 1
    See [Examples of SQL Injections through addslashes()?](https://stackoverflow.com/questions/860954/examples-of-sql-injections-through-addslashes) for some cases where `addslashes` is not enough. – Adder Nov 25 '19 at 14:17