简体   繁体   English

如何使用 id 将记录添加到数据库而不是更改它们(javascript 和 php)?

[英]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.如何使用我在 phpMyAdmin 中列出的 id 向数据库添加一些记录。 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.问题是一旦我输入 id ,它就会显示数据库,但是当我输入另一个 id 时,它取出并更改我输入的第一个 id 数据库的值并将其替换为第二个 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.我想要的是通过每次点击一个一个地添加数据库,而不是替换它,我想在我之前输入的第一个 ID 之后添加它们。 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 PHP 和 Javascript 从自动完成搜索中获取记录 ID? - How to get record ID from an autocomplete search using PHP and Javascript? 使用javascript,mysql和php将记录插入数据库 - Insert record into database using javascript, mysql, and php 如何从MYSQL数据库获取1条记录的ID,然后将其传递到详细信息页面以使用动态php和mysqli查看 - How to get the ID for 1 record from MYSQL database, then pass it to the details page for viewing using dynamic php & mysqli 添加auto_increment id列后,无法使用php和javascript将数据添加到mysql数据库 - Unable to add data to mysql database using php & javascript after adding an auto_increment id column 在数据库中保存或更新记录时,如何强制eloquent接受&#39;ID&#39;而不是&#39;id&#39;? - How to force eloquent to accept 'ID' instead of 'id' when saving or updating a record in database? 使用PHP将javascript数组添加到数据库的想法 - idea to add javascript array to database using PHP 如何使用javascript动态添加html元素并将其记录保存在php中 - How to dynamically add html elements using javascript and also keep their record in php 使用AJAX在PHP文件中调用函数,该ID具有数据库中记录的特定ID以更新记录 - Calling a Function in PHP file, using AJAX, with an ID specific to a record in Database to update record PHP使用ID删除记录 - php delete record using id 使用事件名而不是ID删除记录 - Delete Record Using eventname instead of ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM