2017-05-17 160 views
-3

我有一个问题如何从表格(html)向数据库(mysql)插入数据?

我尝试在表(HTML)来显示从数据库中的数据,然后,我已显示的数据必须在另一个表保存具有相同内容

显示数据

<?php 
    include('ApprovalDB.php'); 
$result = mysql_query("SELECT pr_id, prcode, type, client, requestdate, status FROM t_purchaserequest where status = 'Approved' and type = 'Sample Only'") 
or die(mysql_error()); 

echo "<table class = 'tbl1' cellpadding='10'>"; 
echo "<thead><td>PRCODE</td><td>TYPE</td> <td>CLIENT</td> <td>REQUESTED DATE</td> <td>STATUS</td><td>ACTION</td></thead>"; 

while($row = mysql_fetch_array($result)) { 
echo "<tr>"; 

echo '<td>' . $row['prcode'] . '</td>'; 
echo '<td>' . $row['type'] . '</td>'; 
echo '<td>' . $row['client'] . '</td>'; 
echo '<td>' . $row['requestdate'] . '</td>'; 
echo '<td>' . $row['status'] . '</td>'; 

echo '<td><a href="returnDB.php?id=' . $row['pr_id'] . '" class = "link1">Return Item</a></td>'; 
echo "</tr>"; 

} 
echo "</table>"; 
echo "<a href = 'javascript:window.history.go(-1);' class = 'img_arrow'><img src = 'back_arrow.png'></a>"; 
?> 

此链接退货项目,该功能必须保存该项目显示在表格中..因为我是一个新手..我不如何我应该从这里开始......

感谢您对我的问题的回复

+1

当你点击你想要插入的链接? – lalithkumar

+0

该链接不会发送除行ID以外的任何数据。因此,在returnDB.php中,您需要查找原始表中的数据并将其插入另一个表中。 –

+0

@lalithkumar是的,根据选定的ID将它插入到另一个表上 – Jhesie

回答

1

首先不应该再次以HTML格式存储细节,因为您可以随时创建。

如果您想这样做,您可以创建一个变量并将渲染HTML存储在该变量中,您可以打印相同的变量并将该变量保留在隐藏字段中。

提交带有发布请求的表单,因为可能隐藏字段值的隐藏大小会更大。

如果隐藏字段的大小多于只发送记录的主键到服务器端再从数据库中获取详细信息创建相同的HTML并将其存储回另一个表。

下面是在变量中存储HTML并显示它的代码。您可以创建表单并提交上面提到的值。

<?php 

include('ApprovalDB.php'); 
$result = mysql_query("SELECT pr_id, prcode, type, client, requestdate, status FROM t_purchaserequest where status = 'Approved' and type = 'Sample Only'") 
or die(mysql_error()); 
$str = ""; 
$str .= "<table class = 'tbl1' cellpadding='10'>"; 
$str .= "<thead><td>PRCODE</td><td>TYPE</td> <td>CLIENT</td> <td>REQUESTED DATE</td> <td>STATUS</td><td>ACTION</td></thead>"; 

while ($row = mysql_fetch_array($result)) { 
    $str .= "<tr>"; 

    $str .= '<td>' . $row['prcode'] . '</td>'; 
    $str .= '<td>' . $row['type'] . '</td>'; 
    $str .= '<td>' . $row['client'] . '</td>'; 
    $str .= '<td>' . $row['requestdate'] . '</td>'; 
    $str .= '<td>' . $row['status'] . '</td>'; 
    $str .= "</tr>"; 

} 
$str . "</table>"; 


//display the data 

echo $str; 

//to save the data ideally you should not save in this format but still you want to do you can do in two way 

//1. most appropriate way you can get the product details in server side, create same string like i have created above and save it to db 

//2.create hidden field and save the data with post form 
echo "<input type='hidden' name='my-data' value='".$str."' >"; 


echo "<a href = 'javascript:window.history.go(-1);' class = 'img_arrow'><img src = 'back_arrow.png'></a>"; 
?> 
+0

虽然此代码可能会回答问题,但为何和/或代码如何回答问题提供更多背景可以提高其长期价值。 –

+0

请在您的答案中加上解释@gyaan –

+0

1.我不知道为什么要将数据保存在html formate中,您可以在渲染自己的同时创建数据。 – gyaan

相关问题