【问题标题】:PHP- Insert all table values to DB in a single click and edit value?PHP-单击将所有表值插入数据库并编辑值?
【发布时间】:2020-03-24 22:30:04
【问题描述】:

如何使用单个提交按钮将所有表值插入数据库?

如何编辑表格值并更新到数据库?

对于下面的代码sn -p

在代码中我想插入数据库并使用编辑选项显示在另一个表中。

问题是如何使用单个提交按钮将值更新为数据库中已经存在的值。

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->

<div class="container">
    <table id="myTable" class=" table order-list">
    <thead>
            <tr>
                <th width="5%">SI. No</th>
                <th width="45%">Items / Products</th>
                <th width="5%">Qty Requested</th>
                <th width="20%">Quantity Procured</th>
                <th width="10%">Specification</th>
                <th width="20%">Actions</th>
            </tr>
        </thead>
    <tbody>
        <!-- row will be dynamically add here 

            <tr>
            <td class="col-sm-0">
                <input type="text" name="sno" class="form-control" />
            </td>
            <td class="col-sm-4">
                <input type="mail" name="items"  class="form-control"/>
            </td>
            <td class="col-sm-3">
                <input type="text" name="qty_r"  class="form-control"/>
            </td>

            <td class="col-sm-4">
                <input type="text" name="qty_p" class="form-control" />
            </td>
            <td class="col-sm-4">
                <input type="mail" name="specification"  class="form-control"/>
            </td>
            <td class="col-sm-3">
                <input type="text" name="phone"  class="form-control"/>
            </td>

            <td class="col-sm-2"><a class="deleteRow"></a>

            </td>
        </tr>
        -->
    </tbody>
    <tfoot>
        <tr>
            <td colspan="5" style="text-align: left;">
                <input type="button" class="btn btn-lg btn-block " id="addrow" value="Add Row" />
            </td>
        </tr>
        <tr>
        </tr>
    </tfoot>
</table>
<div class="form-group text-center">
        <label></label>
        <button type="submit" class="btn btn-warning" >Save <i class="glyphicon glyphicon-send"></i></button>
    </div>

</div>

<script>
$(document).ready(function () {
    var counter = 1;

    $("#addrow").on("click", function () {
        var newRow = $("<tr>");
        var cols = "";

        cols += '<td><input type="text" class="form-control" name="sno' + counter + '" value="'+counter+'" /></td>';
        cols += '<td><input type="text" class="form-control" name="items' + counter + '"/></td>';
        cols += '<td><input type="text" class="form-control" name="qty_r' + counter + '"/></td>';

        cols += '<td><input type="text" class="form-control" name="qty_p' + counter + '"/></td>';
        cols += '<td><input type="text" class="form-control" name="specification' + counter + '"/></td>';
        cols += '<td><input type="text" class="form-control" name="phone' + counter + '"/></td>';

        cols += '<td><input type="button" class="ibtnDel btn btn-md btn-danger "  value="Delete"></td>';
        newRow.append(cols);
        $("table.order-list").append(newRow);
        counter++;
    });



    $("table.order-list").on("click", ".ibtnDel", function (event) {
        $(this).closest("tr").remove();       
        counter -= 1
    });


});


</script>

【问题讨论】:

  • 据我所知,PHP 中没有“传递图像”之类的东西。您可以做的是将其上传到一个页面上,重定向并在另一个页面上显示。 “传递图像”到底是什么意思?
  • 我想他只是想通过 post 方法将表单值(在这种情况下,上传的图像名称)传递给另一个文件。如果是这样,那么我给出的例子将对他有所帮助。谢谢

标签: php sql database phpmyadmin


【解决方案1】:

请检查一下。这个对我有用。 我只是在输入类型中添加了一个 name 属性并通过 post 方法传递它,不要忘记将 index.html 重命名为 index.php

仅单次上传:

index.php

 <form method="POST" action="img.php">
        <img id="img" alt="image" width="100" height="100" />
    <input type="file" name="img_url" onchange="document.getElementById('img').src = window.URL.createObjectURL(this.files[0])">
    <input type="submit" name="">

img.php

<?php 
echo $_POST['img_url'];

?>

多个上传:

index.php

<form id="dynamicForm" method="POST" action="img.php">
    <b>This single img works but not in js</b> <br>
    <img id="img" alt="your image" width="100" height="100" />
    <input type="file" name="single_img" onchange="document.getElementById('img').src = window.URL.createObjectURL(this.files[0])">

    <br/>
    No of Img <input type="text" id="noi" value="" onkeyup="addFields()">
        <br />

     <div id="dynamicField"></div>
<input type="submit" name="">

<script> 


    function addFields(){
        // Number of inputs to create
        var number = document.getElementById("noi").value;
        // Container <div> where dynamic content will be placed
        var container = document.getElementById("dynamicField");
        var array = ["CRICTICAL","HIGH","LOW","INFO"];
        // Clear previous contents of the container
        while (container.hasChildNodes()) {
            container.removeChild(container.lastChild);
        }


        for (i=1;i<=number;i++){

            var img = document.createElement("img");
            img.width="100";
            img.height="100";
            img.id="img "+i;

            var upload = document.createElement("input");
            upload.type="file";
            upload.name="file_"+i;
            upload.id="upload "+i;
            //Working_______________
            upload.onchange=upload.onchange= function () {

                var img_id=this.getAttribute('id');
                var imgId = img_id.charAt(7) ; 
                document.getElementById("img "+imgId).src = window.URL.createObjectURL(this.files[0])
            }

            //________________________________________
            container.appendChild(img);
            container.appendChild(upload);
            container.appendChild(document.createElement("br"));
        }
    } 
</script>

img.php

<?php 

$result =  $_POST;
$mul =  count($result);
if($mul>0){
    foreach ($result as $key => $value) {
        echo $value."<br>";
    }
}

?>

【讨论】:

  • 如果index.php有多个图片怎么办?
  • 如果图像存在于表单之外,那么它将不再有影响如果您的 from 在一种输入类型中支持多个图像,在这种情况下,您可能会得到一个数组值(不确定) .或者您使用多个输入类型,然后只需使用多个属性作为名称,就像上面一样。但是,你能分享一下代码 sn-p 吗?
  • 获取动态图像字段并将其传递到下一页 chk here stackoverflow.com/a/59101028/12113772
  • 根据您的要求更新了代码 sn-p。希望你会喜欢:)
  • 那我去看看
猜你喜欢
  • 2019-04-29
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
  • 2012-12-15
  • 2013-01-18
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
相关资源
最近更新 更多