简体   繁体   中英

How to add a record to a database using id instead of changing them (javascript and php)?

How do I add a few records to a database using the id's I had listed in phpMyAdmin. The problem is once I entered the id , it display the database but when I entered another id , it took out and change the value of the first id database I entered and replace it with the second id. What I want is to add the database one by one through each click but instead of replacing it, I would like to add them below after the first ID I entered earlier. This is what I'm working on so far.

 <html> <head> <title>Search data by its ID</title> </head> <body> <center> <h1>Search a single DATA</h1> <h2>Retrieve data from database</h2> <div class="container"> <form action="" method="POST"> <input type="text" name="id" placeholder="Student ID" /> <input type="submit" name="search" value="Search By ID" /> </form> <table border="2" id="newton"> <tr> <th>Product Name</th> <th>Quantity</th> <th>Returned Date</th> </tr><br><br> <?php $connection = mysqli_connect("localhost","root", ""); $db = mysqli_select_db($connection,"myfirstdb"); if(isset($_POST['search'])) { $id = $_POST['id']; $query = "SELECT * FROM `table3` where id = '$id'"; $query_run = mysqli_query($connection, $query); while($row = mysqli_fetch_array($query_run)) { ?> <tr> <td> <?php echo $row ['product_name']; ?> </td> <td> <?php echo $row ['quantity']; ?> </td> <td> <?php echo $row ['returned_date']; ?> </td> </tr> <?php } } ?> </table> </form> </div> </center> </body> </html>

If I understand your problem correctly, the following code can solve your problem. Although the student ID has nothing to do with the name of the product !! you can use a session for this problem.

<html>

<head>
    <title>Search data by its ID</title>
</head>

<body>
<center>
    <h1>Search a single DATA</h1>
    <h2>Retrieve data from database</h2>

    <div class="container">
        <form action="" method="POST">
            <input type="text" name="id" placeholder="Student ID" />
            <input type="submit" name="search" value="Search By ID" />

        </form>
        <table border="2" id="newton">
            <tr>

                <th>Product Name</th>
                <th>Quantity</th>
                <th>Returned Date</th>
            </tr><br><br>
            <?php
            $connection = mysqli_connect("localhost","root", "");
            $db = mysqli_select_db($connection,"myfirstdb");

            session_start();

            if (!isset($_SESSION['id'])) {
                $_SESSION['id'] = array();
            }
            

            if(isset($_POST['search']))
            {
                $id = $_POST['id'];

                array_push($_SESSION['id'],$id);
                $_SESSION['id'] = array_unique($_SESSION['id']);

                $id = implode(',',$_SESSION['id']);



                $query = "SELECT * FROM `table3` where id in ($id)";
                $query_run = mysqli_query($connection, $query);

                while($row = mysqli_fetch_array($query_run))
                {
                    ?>
                    <tr>
                        <td>
          <?php echo $row ['product_name']; ?> </td>
        <td>
          <?php echo $row ['quantity']; ?> </td>
        <td>
          <?php echo $row ['returned_date']; ?> </td>


                    </tr>

                    <?php

                }
            }


            ?>

        </table>
        </form>
    </div>
</center>
</body>

</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM