如何在JavaScript

时间:2016-04-14 07:19:26

标签: javascript php mysql json

首先,我很抱歉我的英语不好。

我正在为学校制作信息系统,并在更新学生数据时遇到问题。学生表上有兄弟姐妹的字段。他们可以添加一个或多个兄弟姐妹(一对多关系)。该程序已经成功地将兄弟姐妹添加到数据库中(有两个数据库,学生(id [自动增量 - 主键],student_id名称,性别,年龄,...等)和兄弟姐妹(id(主键),student_id ,siblings_name,siblings_age,...等)。

但是,当我更新兄弟姐妹数据时,程序失败了。

ADD SIBLINGS PHP:

<table id='siblingsTable'>
    <a href='javascript:void(0);' onclick='addRow()'><img src='../images/add.png'/></a>&nbsp;&nbsp;&nbsp;
    <a href='javascript:void(0);' onclick='deleteRow()'><img src='../images/delete.png'/></a><br />
    <th>Name</th>
    <th>Age</th>
    <th></th>
</table>

JS:

<script>
   function addRow() {
   var table = document.getElementById("siblingsTable");
   var rowCount = table.rows.length;
   var row = table.insertRow(rowCount);

   var cell1 = row.insertCell(0);
   var element1 = document.createElement("input");
   element1.type = "text";
   element1.name    = "siblings_name[]";
   element1.value   = "";
   cell1.appendChild(element1);

   var cell2 = row.insertCell(1);
   var element2 = document.createElement("input");
   element2.type = "text";
   element2.name = "siblings_age[]";
   element2.value = "";
   cell2.appendChild(element2);
}

function deleteRow() {
   var table = document.getElementById("siblingsTable");
   var rowCount = table.rows.length;
   table.deleteRow(rowCount -1);
}
</script>

保存到数据库:

if (!empty($_POST['siblings_name']) && !empty($_POST['siblings_age']) && is_array($_POST['siblings_name']) && is_array($_POST['siblings_age']) && count($_POST['siblings_name']) === count($_POST['siblings_age'])) {
    $auth_array = $_POST['auth_key'];
    $name_array = $_POST['siblings_name'];
    $age_array = $_POST['siblings_age'];
        for ($i = 0; $i < count($name_array); $i++) {
            $auth = mysql_real_escape_string($auth_array);
            $name = mysql_real_escape_string($name_array[$i]);
            $age = mysql_real_escape_string($age_array[$i]);
            mysql_query("INSERT INTO skkk_web_siblings (auth_key, siblings_name, siblings_age) VALUES ('$auth', '$name', '$age')");
        }
    }

Table "Add Siblings"

当我想要更新数据时,这是我的问题:

$sib = mysql_query("SELECT COUNT(auth_key) FROM skkk_web_siblings WHERE auth_key = '$_GET[id]'");
$totalsiblings = mysql_result($sib,0);
...
$siblings = mysql_query("SELECT * FROM skkk_web_siblings WHERE auth_key = '$_GET[id]'");
            while($row = mysql_fetch_array($siblings)){
                $id = $row['id'];
                $name = $row['siblings_name'];
                $age = $row['siblings_age'];
                $siblingsname[] = $name;
                $siblingsage[] = $age;
            }
...
...
...
<table id='siblingsTable'>
                    <a href='javascript:void(0);' onclick='editRow()'><img src='../images/edit.png'/></a><br />
                    <th>Name</th>
                    <th>Age</th>
</table>
...
<script>
    function editRow() {
        var table       = document.getElementById("siblingsTable");

        var total   = '<?php echo $totalsiblings; ?>';
        var name    = jQuery.parseJSON('<?php echo json_encode($siblingsname); ?>');
        var age = jQuery.parseJSON('<?php echo json_encode($siblingsage); ?>');
        var i;
        var table = '';
        for (i = 0; i < total; i++) {
            tbl += '<input type="text" name="siblings_name[]" value="' + name[i] + '"/>';
            tbl += '<input type="text" name="siblings_age[]" value="' + age[i] + '"/>';
        }
        document.getElementById("siblingsTable").innerHTML = table;
    }
</script>
...
//updating to database
if (!empty($_POST['siblings_name']) && !empty($_POST['siblings_age']) && is_array($_POST['siblings_name']) && is_array($_POST['siblings_age']) && count($_POST['siblings_name']) === count($_POST['siblings_age'])) {
            $name_array = $_POST['siblings_name'];
            $age_array = $_POST['siblings_age'];
            for ($i = 0; $i < count($name_array); $i++) {
                $name = mysql_real_escape_string($name_array[$i]);
                $age = mysql_real_escape_string($age_array[$i]);
                mysql_query("UPDATE skkk_web_siblings SET siblings_name = '$name', siblings_age = '$age' WHERE auth_key = '$_POST[id]'");
            }
        }

当我更新一个名字时,它会更改所有兄弟姐妹的名字。我该怎么办?

感谢您的帮助。

0 个答案:

没有答案