简体   繁体   English

如何在phpmyadmin中创建一个像表一样的动态表?

[英]How to create a dynamic table like the table in phpmyadmin?

I have the code below which edits the cell value when clicked, however I want the new value to be stored in the database when the user has clicked the save button..please help me 我在下面的代码中单击时编辑单元格值,但是当用户单击保存按钮时,我希望将新值存储在数据库中。请帮助我

    <?php include"connect.php"; ?>
    <html>
        <head>
            <title>Edit time records</title>    
            <style>
                table{ text-align:justified;}
                a {text-decoration:none;
                   color:black;
                }
                .replace {display:none;}
            </style>
            <link rel="stylesheet" type="text/css" href="css/allRecordsStyle.css">
        <script type="text/javascript">
    /*<![CDATA[*/
    function INPUT(id){
    var obj=document.getElementById(id),tds=obj.getElementsByTagName('SPAN'),z0=0,ip,html;
    for (;z0<tds.length;z0++){
    tds[z0].onmouseup=function(){ AddInput(this); }
    }
    }

    function AddInput(td){
        var ip=zxcAddField('INPUT','text','');
        ip.value=td.innerHTML;
        ip.ondblclick = function () { removeInput(ip); };
        td.innerHTML='';
        td.appendChild(ip);
        td.onmouseup=null;
    }
    function removeInput(input) {
        var val = input.value;
        var td = input.parentNode;
        td.removeChild(td.lastChild);
        td.innerHTML = val;
        td.onmouseup = function () { AddInput(td); };
    }

    function zxcAddField(nn,type,nme){
    var obj;
    try {
    obj=document.createElement('<'+nn+' name="'+(nme||'')+'" '+(type?'type="'+type+'" ':'')+' >');
    }
    catch(error){
    obj=document.createElement(nn);
    if (type){
    obj.type=type;
    }
    obj.name=nme||'';
    }
    return obj;
    }


    /*]]>*/
    </script>
    <script type="text/javascript">
    /*<![CDATA[*/

    function Init(){
    INPUT('tst');
    }

    if (window.addEventListener){
    window.addEventListener('load',Init, false);
    }
    else if (window.attachEvent){
    window.attachEvent('onload',Init);
    }

    /*]]>*/
    </script>
        </head>
    <body>
    <a href="admin.php">back</a>
    <?php



      echo "<table border=1 id='tst' class='tab'>";
      echo "<tr><th class='data' rowspan='2' ><strong>EMPLOYEE</strong></th>";
      echo "<th class='data' rowspan='2'>DATE</th>
                <th colspan='2'>AM</th>
                <th colspan='2'>PM</th>
                <th colspan='2'>OVERTIME</th></tr>";
      echo "<tr>
        <th>Arrival</th>
        <th>Departure</th>  
        <th>Arrival</th>
        <th>Departure</th>
        <th>In</th>
        <th>Out</th>
    </tr>";


    $query="SELECT 
    employee_detail.employee_code,
    employee_detail.lname,
    employee_detail.fname,
    employee_detail.mname,
    employee_record.date,
    employee_record.am_in,
    employee_record.am_out,
    employee_record.pm_in,
    employee_record.pm_out,
    employee_record.over_in,
    employee_record.over_out
    FROM employee_detail INNER JOIN employee_record ON         employee_record.employee_code=employee_detail.employee_code ORDER BY id DESC ";

$result=mysql_query($query);
$affected=mysql_affected_rows();

    while($affected>=1&&$row=mysql_fetch_array($result))
    {

      echo '<tr><td>';
      echo $row['lname'].",&nbsp";
      echo $row['fname']."&nbsp";
      echo $row['mname']."</td>"; 
      echo '<td>'.$row['date'].'</td>';
      echo '<td><span name="am_in">'.$row['am_in'].'</span></td>';
      echo '<td><span name="am_out">'.$row['am_out'].'</span></td>';
      echo '<td><span name="pm_in">'.$row['pm_in'].'</span></td>';
      echo '<td><span name="pm_out">'.$row['pm_out'].'</span></td>';
      echo '<td><span name="over_in">'.$row['over_in'].'</span></td>';
      echo '<td><span name="over_out">'.$row['over_out'].'</span></td></tr>';
      $affected--;

    }?>
    <input id="save" type="submit" value="Save Changes" onclick=""/>


    </body>
    </html>

I can't read this. 我看不懂这个。 I've been trying for 15 minutes. 我已经尝试了15分钟。

To save to the database you need to make an AJAX call to the server, which probably means, in your case, that you will make another php script for that purpose, or divide your script into more than one file... 要保存到数据库,您需要对服务器进行AJAX调用,这可能意味着,在您的情况下,您将为此目的创建另一个PHP脚本,或将您的脚本划分为多个文件...

But you'll end up attaching a click event on this "save button" and then doing the following: 但是你最终会在这个“保存按钮”上附加一个点击事件,然后执行以下操作:

var xhr = makeXmlObject();
xhr.onreadystatechange = function () {
  if (xhr.readyState == 4) {
     alert("saved...hopefully...");
  } 
}
xhr.open("POST", "myscript.php", true);
xhr.send("name=cheese");

You also need to set the headers to the form encoding, and make the other script....and make the makeXmlObject function...but this should get you on the right track. 您还需要将标题设置为表单编码,并制作另一个脚本....并制作makeXmlObject函数...但这应该让您走上正确的轨道。

Or you could use a library and most of this gets abstracted for you so you everything isn't so cryptic...which is what I did a while ago and now can't remember how to use the straight up xmlhttprequest object... 或者你可以使用一个库,其中大部分都是为你抽象的,所以你的一切都不是那么神秘......这就是我前一段时间所做的,现在不记得如何使用直接的xmlhttprequest对象......

Anyway, good luck. 无论如何,祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM