2017-05-17 156 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

相關問題