将HTML表输入保存到数据库,并显示更新的表

时间:2014-08-12 17:06:37

标签: php html mysql sql

我有以下代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>

    <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>  
    <link href='http://fonts.googleapis.com/css?family=Raleway:500' rel='stylesheet' type='text/css'>
    <link href="style.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript">   
        function displayResult()
        {
        var table=document.getElementById("table");
        var row=table.insertRow(2);
        var cell1=row.insertCell(0);
        var cell2=row.insertCell(1);
        var cell3=row.insertCell(2);
        var cell4=row.insertCell(3);
        cell1.innerHTML=document.getElementById('name').value;
        cell2.innerHTML=document.getElementById('description').value;
        cell3.innerHTML=document.getElementById('category').value;
        cell4.innerHTML=document.getElementById('link').value;

        }
    </script>

</head>


<body>

<div class="container">
  <div class="helper">
    <div class="content">
    <h1>Downloads Library</h1>
    <p>Below are a list of files I have worked on available for download</p><br />

        <table id="table">
            <thead>
                <tr>
                    <th scope="col">Download Name</th>
                    <th scope="col">Download Description</th>
                    <th scope="col">Category</th>
                    <th scope="col"></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Download Name</td>
                    <td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sapien sapien, lobortis sit amet vulputate non, facilisis fringilla urna. Sed ac aliquet arcu.</td>
                    <td>HTML/CSS</td>
                    <td align="center">
                    <div class="ph-float">
                        <a href='#' class='button green'>Download</a>
                    </div> 
                    </td>
                </tr>
             </tbody>
         </table>

         <form name="create" method="POST" action="db.php">         
            <label>Download Name:<br /><input type="textbox" id="name"></label><br /><br />
            <label>Download Description:<br /><textarea rows="3" id="description"></textarea></label><br /><br />
            <label>Category:<br /><input type="textbox" id="category"><br/></label><br /><br />
            <label>Download Link:<br /><input type="textbox" id="link"><br/></label><br /><br />

            <button type="button" onClick="displayResult()" id="submit">Insert</button>

        </form>





    </div>
  </div>
</div>

</body>

</html>

在db.php中,我有以下代码:

<?

mysql_connect("host", "user", "pass") or die(mysql_error());
mysql_select_db("name") or die(mysql_error());
mysql_query("INSERT INTO testing (name, description, category, link) VALUES('".$_POST['name']."', '".$_POST['description']."', '".$_POST['category']."', '".$_POST['link']."' ) ") or die(mysql_error()); 

?>

该代码允许我使用用户在表单中添加的值向HTML表中添加一行。

我的问题是,当单击提交按钮时,如何使新生成的行中的值保存到MySQL数据库中。另外,如何使用新生成的行更新表,以便在刷新页面时新行永久存在?

谢谢大家的帮助

0 个答案:

没有答案