使用php保存来自可编辑表格的值 [英] save values from editable table using php

查看:119
本文介绍了使用php保存来自可编辑表格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有一个从php生成的表,它是可编辑的,我想将编辑后的值保存到数据库。我不知道如何做到这一点,因为页面上有很多行(动态)。
下面是一个截屏: -



请建议

编辑: -

我的代码是

  echo< table border ='1'> 
< tr>
< th> S1 Number< / th>
< th> Product Id< / th>
产品名称
产品类别
报价价格
最大值价格
最小价格
初始库存
售出数量$ b $第b个当前库存第
个最后订单第
个编辑/更新第
个< / tr> ;
while($ row = $ result-> fetch_assoc())
{
echo< tr contenteditable>;
回显< td> 。 $ row [行号]。 < / TD> 中;
回显< td> 。 $ row [Product Id]。 < / TD> 中;
回显< td> 。 $ row [Product Name]。 < / TD> 中;
回显< td> 。 $ row [Product Catagory]。 < / TD> 中;
回显< td> 。 $ row [Retal Price]。 < / TD> 中;
回显< td> 。 $ row [最大价格]。 < / TD> 中;
回显< td> 。 $ row [最低价格]。< / td>;
回显< td> 。 $ row [Initial Stock]。< / td>;
回显< td> 。 $ row [已售出数量]。 < / TD> 中;
回显< td> 。 $ row [当前股票]。< / td>;
回显< td> 。 $ row [Last Order]。< / td>;
echo< td contenteditable ='false'>< button href ='#'>更新< / a>< / td>;
回声< / tr>;
}


解决方案

让我给你最好的方法
首先你的数据库表有空格:正确的例如


$ b

$ row [Initial Stock] to $ row [Initial_Stock ]



然后,我会建议您使用ajax而不是浪费时间点击按钮



HTML页面

 < script src =https://cdnjs.cloudflare.com/ajax/libs/jquery/ 3.0.0-β1/ jquery.min.js>< /脚本> 
< script>
$(函数(){

$(#loading)。hide();
//确认消息
var message_status = $(#status );
$(td [contenteditable = true])。blur(function(){
var field_userid = $(this).attr(id);
var value = $(this).text();



$ .post('update.php',field_userid +=+ value,function(data){


$ b if(data!='')
{
message_status.show();
message_status.text(data);
//隐藏消息
setTimeout(function(){message_status.hide()},1000);
}
});
});




});
< / script>


< style>
table.zebra-style {
font-family:Lucida Sans Unicode,Lucida Grande,Sans-Serif;
text-align:left;
border:1px solid #ccc;
margin-bottom:25px;
width:100%
}
table.zebra-style th {
color:#444;
font-size:13px;
font-weight:normal;
padding:5px 4px;

}
table.zebra-style td {
color:#777;
padding:4px;
font-size:13px;

}
table.zebra-style tr.odd {
background:#f2f2f2;
}


#status {padding:10px;位置是:固定;顶部:10px的;边界半径:5像素;的z-index:千万;背景:#88C4FF;颜色:#000;字体重量:粗体;字体大小:12像素;边距:10px的;显示:无;宽度:100%; }
#loading {padding:10px;位置:绝对的;顶部:10px的;边界半径:5像素;的z-index:千万;背景:#F99;颜色:#000;字体重量:粗体;字体大小:12像素;边距:10px的;显示:无;宽度:100%; }
< / style>

< div id =status> < / DIV>
< div id =loading>< / div>







< thead>
< tr>
< th> S1号码< / th>
< th>产品ID< / th>
< th>产品名称< / th>
产品类别< / th>
Retal Price< / th>
Max Price< / th>
Min Min< / th>
初始库存< / th>
th销售数量< / th>
当前股票< / th>
< th>上次订单< / th>
编辑/更新< / th>
< / tr>
< / thead>
< tbody class =list>
<?php do {?>


< tr>
< td contenteditable =trueid =Row_Number:<?php echo $ row [Row_Number];?>><?php echo $ row [Row_Number]; ?>< / TD>
< td contenteditable =trueid =Product_Id:<?php echo $ row [Product_Id];?>><?php echo $ row [Product_Id]; ?>< / TD>
< td contenteditable =trueid =Product_Name:<?php echo $ row [Product_Name];?>><?php echo $ row [Product_Name]; ?>< / TD>
< td contenteditable =trueid =Product_Catagory:<?php echo $ row [Product Id];?>><?php echo $ row [Product_Catagory]; ?>< / TD>
< td contenteditable =trueid =Retal_Price:<?php echo $ row [Retal_Price];?>><?php echo $ row [Retal_Price]; ?>< / TD>
< td contenteditable =trueid =Max_Price:<?php echo $ row [Max_Price];?>><?php echo $ row [Max_Price]; ?>< / TD>
< td contenteditable =trueid =Min_Price:<?php echo $ row [Min_Price];?>><?php echo $ row [Min_Price]; ?>< / TD>
< td contenteditable =trueid =Initial_Stock:<?php echo $ row [Initial_Stock];?>><?php echo $ row [Initial_Stock]; ?>< / TD>
< td contenteditable =trueid =Quantity_Sold:<?php echo $ row [Quantity_Sold];?>><?php echo $ row [Quantity_Sold]; ?>< / TD>
< td contenteditable =trueid =Last_Order:<?php echo $ row [Last_Order];?>><?php echo $ row [Last_Order]; ?>< / TD>
< td contenteditable =trueid =Last_Order:<?php echo $ row [Last_Order];?>><?php echo $ row [Last_Order]; ?>< / TD>
< td contenteditable ='false'>< / td>;



< / tr>

<?php} while($ row = $ result-> fetch_assoc())?>
< / tbody>
< / table>
< / code >

并更新php页面

 < ?php 


$ db = new PDO('mysql:host = localhost; dbname = testdb; charset = UTF-8',
'username',
'password',
array(PDO :: ATTR_EMULATE_PREPARES => false,
PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION));


? >

<?php
if(!empty($ _ POST))
{
//数据库设置

foreach $ _POST as $ field_name => $ val)
{
//清理后的值
$ field_id = strip_tags(trim($ field_name));

/ /来自fieldname:user_id我们需要获取user_id
$ split_data = explode(':',$ f ield_id);
$ product_id = $ split_data [1];
$ field_name = $ split_data [0];
if(!empty($ product_id)&!empty($ field_name)&&!empty($ val))
{

$ affected_rows = $ db-> exec(UPDATE yourtable SET $ field_name ='$ val'WHERE product_ID = $ product_id);
echo $ affected_rows;

echo已更新;
} else {
echoInvalid Requests;
}
}
}

else {
echoInvalid Requests;
}
?>


Hi I have a table generated from php, it is editable, I want to save edited values to database. I have no Idea how can I do this as there are many rows(dynamic) on a page. Here is a screen-shot:-

Please suggest

Edit:-

My code is

    echo "<table border='1'>
<tr>
<th>Sl Number</th>
<th>Product Id</th>
<th>Product Name</th>
<th>Product Catagory</th>
<th>Retal Price</th>
<th>Max Price</th>
<th>Min Price</th>
<th>Initial Stock</th>
<th>Quantity Sold</th>
<th>Current Stock</th>
<th>Last Order</th>
<th>Edit/Update</th>
</tr>";
while($row = $result->fetch_assoc())
{
        echo "<tr contenteditable>";
    echo "<td>" . $row["Row Number"]. "</td>";
    echo "<td>" . $row["Product Id"]. "</td>";
    echo "<td>" . $row["Product Name"]. "</td>";
    echo "<td>" . $row["Product Catagory"]. "</td>";
    echo "<td>" . $row["Retal Price"]. "</td>";
    echo "<td>" . $row["Max Price"]. "</td>";
    echo "<td>" . $row["Min Price"]."</td>";
    echo "<td>" . $row["Initial Stock"]."</td>";
    echo "<td>" . $row["Quantity Sold"]. "</td>";
    echo "<td>" . $row["Current Stock"]."</td>";
    echo "<td>" . $row["Last Order"]."</td>";
    echo "<td contenteditable = 'false'><button href = '#'>Update</a></td>";
        echo "</tr>";   
}

解决方案

Let me give you with the best way First your database tables have spaces: correct that e.g.

from $row["Initial Stock"] to $row["Initial_Stock"]

Then i will propose you use ajax instead of wasting time clicking buttons

The HTML Page

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
 <script>
$(function(){

    $("#loading").hide();
    //acknowledgement message
    var message_status = $("#status");
    $("td[contenteditable=true]").blur(function(){
        var field_userid = $(this).attr("id") ;
        var value = $(this).text() ;



        $.post('update.php' , field_userid + "=" + value, function(data){



            if(data != '')
            {
                message_status.show();
                message_status.text(data);
                //hide the message
                setTimeout(function(){message_status.hide()},1000);
            }
        });
    });




});
</script>


<style>
table.zebra-style {
    font-family:"Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
    text-align:left;
    border:1px solid #ccc;
    margin-bottom:25px;
    width:100%
}
table.zebra-style th {
    color: #444;
    font-size: 13px;
    font-weight: normal;
    padding: 5px 4px;

}
table.zebra-style td {
    color: #777;
    padding: 4px;
    font-size:13px;

}
table.zebra-style tr.odd {
    background:#f2f2f2;
}


#status { padding:10px; position:fixed; top:10px; border-radius:5px; z-index:10000000; background:#88C4FF; color:#000; font-weight:bold; font-size:12px; margin-bottom:10px; display:none; width:100%; }
#loading { padding:10px; position:absolute; top:10px; border-radius:5px; z-index:10000000; background:#F99; color:#000; font-weight:bold; font-size:12px; margin-bottom:10px; display:none; width:100%; }
</style>

 <div id="status"> </div>
 <div id="loading"></div>





<table id="tableID" border="0"  class="sortable table zebra-style">


<thead>
  <tr>
    <th>Sl Number</th>
    <th>Product Id</th>
    <th>Product Name</th>
    <th>Product Catagory</th>
    <th>Retal Price</th>
    <th>Max Price</th>
    <th>Min Price</th>
    <th>Initial Stock</th>
    <th>Quantity Sold</th>
    <th>Current Stock</th>
    <th>Last Order</th>
    <th>Edit/Update</th>
  </tr>
</thead>
<tbody  class="list">
  <?php do { ?>


  <tr>
    <td contenteditable="true" id="Row_Number:<?php echo $row["Row_Number"]; ?>"><?php echo $row["Row_Number"]; ?></td>
    <td contenteditable="true" id="Product_Id:<?php echo $row["Product_Id"]; ?>"><?php echo $row["Product_Id"]; ?></td>
    <td contenteditable="true" id="Product_Name:<?php echo $row["Product_Name"]; ?>"><?php echo $row["Product_Name"]; ?></td>
    <td contenteditable="true" id="Product_Catagory:<?php echo $row["Product Id"]; ?>"><?php echo $row["Product_Catagory"]; ?></td>
    <td contenteditable="true" id="Retal_Price:<?php echo $row["Retal_Price"]; ?>"><?php echo  $row["Retal_Price"]; ?></td>
    <td contenteditable="true" id="Max_Price:<?php echo $row["Max_Price"]; ?>"><?php echo $row["Max_Price"]; ?></td>
    <td contenteditable="true" id="Min_Price:<?php echo $row["Min_Price"]; ?>"><?php echo  $row["Min_Price"]; ?></td>
    <td contenteditable="true" id="Initial_Stock:<?php echo $row["Initial_Stock"]; ?>"><?php echo  $row["Initial_Stock"]; ?></td>
    <td contenteditable="true" id="Quantity_Sold:<?php echo $row["Quantity_Sold"]; ?>"><?php echo  $row["Quantity_Sold"]; ?></td>
    <td contenteditable="true" id="Last_Order:<?php echo $row["Last_Order"]; ?>"><?php echo  $row["Last_Order"]; ?></td>
    <td contenteditable="true" id="Last_Order:<?php echo $row["Last_Order"]; ?>"><?php echo  $row["Last_Order"]; ?></td>
    <td contenteditable = 'false'></td>";



    </tr>

    <?php } while($row = $result->fetch_assoc()) ?>
 </tbody>
</table>

And the update php page

   <?php  


$db = new PDO('mysql:host=localhost;dbname=testdb;charset=UTF-8', 
                  'username', 
                  'password',
                  array(PDO::ATTR_EMULATE_PREPARES => false,
                  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));


?>

<?php
if(!empty($_POST))
{
    //database settings

    foreach($_POST as $field_name => $val)
    {
        //clean post values
        $field_id = strip_tags(trim($field_name));

        //from the fieldname:user_id we need to get user_id
        $split_data = explode(':', $field_id);
        $product_id = $split_data[1];
        $field_name = $split_data[0];
        if(!empty($product_id) && !empty($field_name) && !empty($val))
        {

            $affected_rows = $db->exec("UPDATE yourtable SET $field_name = '$val' WHERE product_ID = $product_id");
            echo $affected_rows;

            echo "Updated";
        } else {
            echo "Invalid Requests";
        }
    }
} 

else {
    echo "Invalid Requests";
}
?>

这篇关于使用php保存来自可编辑表格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆