php - 更新行不起作用,保存按钮复制行

标签 php mysql sql-server

屏幕截图

enter image description here

Notice: Undefined index: cur_id1 in C:\Program Files (x86)\Apache2.2\htdocs\hr\setup\manage_position_sub.php on line 87

Warning: mssql_query() [function.mssql-query]: message: Incorrect syntax near the keyword 'and'. (severity 15) in C:\Program Files (x86)\Apache2.2\htdocs\hr\includes\connecto.php on line 15

Warning: mssql_query() [function.mssql-query]: Query failed in C:\Program Files (x86)\Apache2.2\htdocs\hr\includes\connecto.php on line 15

Notice: Undefined index: cur_id1 in C:\Program Files (x86)\Apache2.2\htdocs\hr\setup\manage_position_sub.php on line 89

Warning: mssql_query() [function.mssql-query]: message: Incorrect syntax near the keyword 'and'. (severity 15) in C:\Program Files (x86)\Apache2.2\htdocs\hr\includes\connecto.php on line 15

Warning: mssql_query() [function.mssql-query]: Query failed in C:\Program Files (x86)\Apache2.2\htdocs\hr\includes\connecto.php on line 15

Notice: Undefined index: cur_id1 in C:\Program Files (x86)\Apache2.2\htdocs\hr\setup\manage_position_sub.php on line 92

Warning: mssql_query() [function.mssql-query]: message: Incorrect syntax near the keyword 'and'. (severity 15) in C:\Program Files (x86)\Apache2.2\htdocs\hr\includes\connecto.php on line 15

Warning: mssql_query() [function.mssql-query]: Query failed in C:\Program Files (x86)\Apache2.2\htdocs\hr\includes\connecto.php on line 15

Notice: Undefined index: cur_id1 in C:\Program Files (x86)\Apache2.2\htdocs\hr\setup\manage_position_sub.php on line 94

Warning: mssql_query() [function.mssql-query]: message: Incorrect syntax near the keyword 'and'. (severity 15) in C:\Program Files (x86)\Apache2.2\htdocs\hr\includes\connecto.php on line 15

Warning: mssql_query() [function.mssql-query]: Query failed in C:\Program Files (x86)\Apache2.2\htdocs\hr\includes\connecto.php on line 15

Notice: Undefined index: cur_id1 in C:\Program Files (x86)\Apache2.2\htdocs\hr\setup\manage_position_sub.php on line 96

现在的问题是,当我单击更新查询的保存按钮时,它只是复制最后一行。插入的时候没有问题。 php 代码如下所示。如果需要,我将发布完整的代码,任何熟悉 phpmysql 的人请检查一下。希望大家明白这个问题。请仔细检查。

    <?php
    session_start();
    ?>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1256">

    <?php 
    header("Content-Type: text/html;charset=windows-1256");
    include_once('../includes/header_config.php');
    $sorting=' order by depe asc';
    if (isset($_POST['field']))
    {
    if ($_POST['field'] != '')
    {
    $sorting=' order by '.$_POST['field'];
    }
    else
    {
    $sorting=' ';
    }
    }
    ?>

    <?php
    //delete
    if(isset($_POST['action']) && ($_POST['action']==3) ) {
    $sql="delete from hr_position where code=".$_POST['delid'];
    $qry = DB_query($sql,$db);
    $sql="delete from listsvalues where listid=40 and id=".$_POST['delid'];
    $qry = DB_query($sql,$db);
    $sql = "delete from hrnodes  where contactid = '".$_POST['delid']."' and userid=15";
    $result2= DB_query($sql,$db);
    }
    ?>


    <?php
    //insert 

    if(isset($_POST['action']) && ($_POST['action']==2) ) {
    if ($_POST['invoicebreakdown']=='' || $_POST['invoicebreakdown']==0) $invoice_breakdown = 1; else $invoice_breakdown = $_POST['invoicebreakdown'];
    for ($i = 1; $i <= $invoice_breakdown; $i++){
    if (isset($_POST['field1'.$i]) ) {
    if(isset($_POST['cur_id_'.$i]) && $_POST['cur_id_'.$i]!='') $cur_id = $_POST['cur_id_'.$i]; else $cur_id ='';
    if($cur_id=='') {
            $sql="select max(code) as mx from hr_position";
            $qry = DB_query($sql,$db);
            if ($res = DB_fetch_array($qry))
            {
                if ($res['mx']==NULL)
                {

                    $newid= 1;
                }
                else
                    {
                    $i =(int) $res['mx'];
                    $i=++$i;
                    $newid= $i;
                    }
            }       

    echo $sql = "insert into hr_position (code,descr,lang) values (".$newid.",'".$_POST['field1'.$i]."',0)";
    $result= DB_query($sql,$db);
    echo $sql = "insert into listsvalues (id,listid,userid,descr,lang,seq) values (".$newid.",40,-1,'".$_POST['field1'.$i]."',0,".$newid.")";
    $result= DB_query($sql,$db);
    echo $sql = "insert into hr_position (code,descr,lang) values (".$newid.",'".$_POST['field2'.$i]."',1)";
    $result= DB_query($sql,$db);
    echo $sql = "insert into listsvalues (id,listid,userid,descr,lang,seq) values (".$newid.",40,-1,'".$_POST['field2'.$i]."',2,".$newid.")";
    $result= DB_query($sql,$db);
    echo $sql = "insert into hr_position (code,descr,lang) values (".$newid.",'".$_POST['field3'.$i]."',2)";
    $result= DB_query($sql,$db);
    echo $sql = "insert into listsvalues (id,listid,userid,descr,lang,seq) values (".$newid.",40,-1,'".$_POST['field3'.$i]."',1,".$newid.")";
    $result= DB_query($sql,$db);

    $sql = "select max(id) as mx from hrnodes ";
    $result1= DB_query($sql,$db);
    $newid1 = DB_fetch_array($result1);
    $nwd = $newid1['mx']+1;
    if ($_SESSION['lang']==0) {$name = $_POST['field1'.$i];} else if ($_SESSION['lang']==1) {$name = $_POST['field2'.$i];} else {$name = $_POST['field3'.$i];}
    $sql = "insert into hrnodes (id,userid,title,position,parentid,contactid,acyear) VALUES 
    ('".$nwd."','15','".$name."','0','2','".$newid."','')";
    $result2= DB_query($sql,$db);

    }
    else
    {
    $sql = "update hr_position set descr='".$_POST['field1'.$i]."' where code =".$_POST['cur_id'.$i]." and lang = 0";
$result= DB_query($sql,$db);
$sql = "update listsvalues set descr='".$_POST['field1'.$i]."' where listid=40 and id =".$_POST['cur_id'.$i]." and lang = 0";
$result= DB_query($sql,$db);

$sql = "update hr_position set descr='".$_POST['field2'.$i]."' where code =".$_POST['cur_id'.$i]." and lang = 2";
$result= DB_query($sql,$db);
$sql = "update listsvalues set descr='".$_POST['field2'.$i]."' where listid=40 and id =".$_POST['cur_id'.$i]." and lang = 2";
$result= DB_query($sql,$db);
$sql = "update hr_position set descr='".$_POST['field3'.$i]."' where code =".$_POST['cur_id'.$i]." and lang = 1";
$result= DB_query($sql,$db);
$sql = "update listsvalues set descr='".$_POST['field3'.$i]."' where listid=40 and id  =".$_POST['cur_id'.$i]." and lang = 1";
$result= DB_query($sql,$db);


if ($_SESSION['lang']==0) {$name = $_POST['field1'.$i];} else if ($_SESSION['lang']==1) {$name = $_POST['field2'.$i];} else {$name = $_POST['field3'.$i];}

$sql = "update hrnodes set title='".$name."' where  contactid = '".$_POST['cur_id'.$i]."' and userid=15";
$result2= DB_query($sql,$db);
    }
    }
    }
    }
    ?>

    <?php
    if(!isset($_POST['breakdown'])) $_POST['breakdown']=0;
    $sql = "SELECT    distinct hr_position.code, hr_position.descr AS depe, hr_position_1.descr AS depf, hr_position_2.descr AS depa
    FROM         hr_position INNER JOIN
                          hr_position hr_position_1 ON hr_position.code = hr_position_1.code INNER JOIN
                          hr_position hr_position_2 ON hr_position_1.code = hr_position_2.code 
    WHERE   hr_position.code<>-1 and  (hr_position.lang = 0) AND (hr_position_2.lang = 2) AND (hr_position_1.lang = 1)".$sorting;

    $result= DB_query($sql,$db);
    $num_rows = DB_num_rows($result);
    $tot = $_POST['breakdown']+$num_rows;
    ?>


    <input type="hidden" name="amttype" value="" id="amttype">
    <input type="hidden" name="invoicebreakdown" value="<?php echo $tot;?>" id="invoicebreakdown">
    <input type="hidden" name="subbreakdown" value="" id="subbreakdown">



    <table width="95%" border="0"  cellpadding="5" cellspacing="0" class="table_fullwidth">
    <tr><th width="30%">Position</th><th width="33%">Position</th>
    <th width="32%">ÇÇáãÑßÒ</th>
    </tr>

    <?php $i=1;
    while ($myrow1 = DB_fetch_array($result)) { 
     if ($i % 2==0) $bkg='td1'; else $bkg='td2';
    ?>
    <tr id="trow_<?php echo $i;?>" class="<?php echo $bkg;?> ">
    <td align="center"><input type="hidden" name="cur_id_<?php echo $i;?>" id="cur_id_<?php echo $i;?>" value="<?php echo $myrow1['code'];?>"> <input  type="text" name="field1<?php echo $i;?>" id="field1<?php echo $i;?>" class="sel_long1 required" value="<?php echo $myrow1['depe'];?>" tabindex="<?php echo $i;?>_1" > </td>

    <td align="center"><input tabindex="<?php echo $i;?>_2" class="sel_long1 required" type="text" name="field2<?php echo $i;?>" id="field2<?php echo $i;?>" value="<?php echo $myrow1['depf'] ;?>">  </td>

    <td align="center"><input tabindex="<?php echo $i;?>_4" class="sel_long1" type="text" name="field3<?php echo $i;?>" id="field3_<?php echo $i;?>" value="<?php echo $myrow1['depa'] ;?>"> </td>
    <td align="center"> <img src="<?php echo $site_path;?>images/del.png" width="20" class="cur" title="Delete" onclick="deleteitem('<?php echo $myrow1['code'];?>');"></td> 
    </tr>
    <?php
    $i++;

    }

    $breakdown = $_POST['breakdown']+$num_rows;
    $j=$num_rows+1;
    while($j<=$breakdown) {
    if ($j % 2==0) $bkg='td1'; else $bkg='td2';
     ?>




    <tr id="trow_<?php echo $j;?>" class="<?php echo $bkg;?> "><td align="center"><input type="hidden" name="cur_id_<?php echo $j;?>" id="cur_id_<?php echo $j;?>" value="">
    <input  type="text" name="field1<?php echo $j;?>" id="field1<?php echo $j;?>" class="sel_long1 required" value="" tabindex="<?php echo $j;?>_1" > </td>

    <td align="center" ><input  class="sel_long1 required" type="text" name="field2<?php echo $j;?>" id="field2<?php echo $j;?>" value="" tabindex="<?php echo $j;?>_2"></td>

    <td align="center"><input  class="sel_long1 required" type="text" name="field3<?php echo $j;?>" id="field3<?php echo $j;?>" value="" tabindex="<?php echo $j;?>_4"> </td>
    <td align="center"><img src="<?php echo $site_path;?>images/del.png" title="Delete"  onclick="delete_row_details(<?php echo $j;?>)" width="20" class="cur"></td>
    </tr>
    <?php $j++; }?>
    </table>
    <div class="save_bt"><input  name="save" id="savebt" class="no_marright submit-green save_right" value="Save"  type="button" onclick="validate_save()" tabindex="<?php echo $j;?>"> </div>

    <div id="did1"></div>




    <script>
     var $targets = $('#erp_from_sub').find('input, button , select'),
        steps = $targets.map(function() {
            return $(this).attr('tabindex');
        }).get();

    $('#erp_from_sub').on('keypress', 'input, button, select', function(e) {
        if (e.keyCode == 13) {
            var current = $.inArray($(this).attr('tabindex'), steps),
                next = steps[++current % steps.length];
            $targets.filter('[tabindex="' + next + '"]').focus();
        }
    });

    function deleteitem(a) {
      if(confirm('Are you sure you want delete?')) { document.erp_from_sub.delid.value=a;  document.getElementById('action').value='3';load_subpage('erp_from_sub');}

      }
      function delete_row_details(id)
    {
    var el = document.getElementById('trow_'+id);
    if(confirm("Are you sure you want to delete this row")==true)
    el.parentNode.removeChild(el);
    /*calculateSum_credit();*/
    }
    </script>

最佳答案

问题出在 id 上,也出在 if else 语句中。更正后的代码是这样的。

<?php
session_start();
?>
<?php 
header("Content-Type: text/html;charset=windows-1256");
include_once('../includes/header_config.php');
$sorting=' order by depe asc';
if (isset($_POST['field']))
{
if ($_POST['field'] != '')
{
$sorting=' order by '.$_POST['field'];
}
else
{
$sorting=' ';
}
}
?>

<?php
//delete
if(isset($_POST['action']) && ($_POST['action']==3) ) {
$sql="delete from hr_position where code=".$_POST['delid'];
$qry = DB_query($sql,$db);
$sql="delete from listsvalues where listid=40 and id=".$_POST['delid'];
$qry = DB_query($sql,$db);
$sql = "delete from hrnodes  where contactid = '".$_POST['delid']."' and userid=15";
$result2= DB_query($sql,$db);
}
?>


<?php
//insert 

if(isset($_POST['action']) && ($_POST['action']==2) ) {
if ($_POST['invoicebreakdown']=='' || $_POST['invoicebreakdown']==0) $invoice_breakdown = 1; else $invoice_breakdown = $_POST['invoicebreakdown'];
for ($i = 1; $i <= $invoice_breakdown; $i++){

if(isset($_POST['cur_id_'.$i]) && $_POST['cur_id_'.$i]!='') $cur_id = $_POST['cur_id_'.$i]; else $cur_id ='';
// Insert the value if the cur_id is null
        if($cur_id=='') {
        $sql="select max(code) as mx from hr_position";
        $qry = DB_query($sql,$db);
        if ($res = DB_fetch_array($qry))
        {
            if ($res[0] != NULL and $res[0] > 0 ){
            $newid = $res[0] + 1;
            }else{
            $newid =1;
        }
        }       

$sql = "insert into hr_position (code,descr,lang) values (".$newid.",'".$_POST['field1'.$i]."',0)";
$result= DB_query($sql,$db);

$sql = "insert into listsvalues (id,listid,userid,descr,lang,seq) values (".$newid.",40,-1,'".$_POST['field1'.$i]."',0,".$newid.")";
$result= DB_query($sql,$db);

$sql = "insert into hr_position (code,descr,lang) values (".$newid.",'".$_POST['field2'.$i]."',2)";
$result= DB_query($sql,$db);

$sql = "insert into listsvalues (id,listid,userid,descr,lang,seq) values (".$newid.",40,-1,'".$_POST['field2'.$i]."',2,".$newid.")";
$result= DB_query($sql,$db);

$sql = "insert into hr_position (code,descr,lang) values (".$newid.",'".$_POST['field3'.$i]."',1)";
$result= DB_query($sql,$db);

$sql = "insert into listsvalues (id,listid,userid,descr,lang,seq) values (".$newid.",40,-1,'".$_POST['field3'.$i]."',1,".$newid.")";
$result= DB_query($sql,$db);

$sql = "select max(id) as mx from hrnodes ";
$result1= DB_query($sql,$db);
$newid1 = DB_fetch_array($result1);
$nwd = $newid1['mx']+1;
if ($_SESSION['lang']==0) {$name = $_POST['field1'.$i];} else if ($_SESSION['lang']==1) {$name = $_POST['field2'.$i];} else {$name = $_POST['field3'.$i];}
$sql = "insert into hrnodes (id,userid,title,position,parentid,contactid,acyear) VALUES 
('".$nwd."','15','".$name."','0','2','".$newid."','')";
$result2= DB_query($sql,$db);

}
else
{
$sql = "update hr_position set descr='".$_POST['field1'.$i]."' where code =".$_POST['cur_id_'.$i]." and lang = 0";
$result= DB_query($sql,$db);
$sql = "update listsvalues set descr='".$_POST['field1'.$i]."' where listid=40 and id =".$_POST['cur_id_'.$i]." and lang = 0";
$result= DB_query($sql,$db);

$sql = "update hr_position set descr='".$_POST['field2'.$i]."' where code =".$_POST['cur_id_'.$i]." and lang = 1";
$result= DB_query($sql,$db);
$sql = "update listsvalues set descr='".$_POST['field2'.$i]."' where listid=40 and id =".$_POST['cur_id_'.$i]." and lang = 1";
$result= DB_query($sql,$db);
$sql = "update hr_position set descr='".$_POST['field3'.$i]."' where code =".$_POST['cur_id_'.$i]." and lang = 2";
$result= DB_query($sql,$db);
$sql = "update listsvalues set descr='".$_POST['field3'.$i]."' where listid=40 and id  =".$_POST['cur_id_'.$i]." and lang = 2";
$result= DB_query($sql,$db);


if ($_SESSION['lang']==0) {$name = $_POST['field1'.$i];} else if ($_SESSION['lang']==1) {$name = $_POST['field2'.$i];} else {$name = $_POST['field3'.$i];}

$sql = "update hrnodes set title='".$name."' where  contactid = '".$_POST['cur_id_'.$i]."' and userid=15";
$result2= DB_query($sql,$db);
}
}
}
?>

<?php
if(!isset($_POST['breakdown'])) $_POST['breakdown']=0;
$sql = "SELECT    distinct hr_position.code, hr_position.descr AS depe, hr_position_1.descr AS depf, hr_position_2.descr AS depa
FROM         hr_position INNER JOIN
                      hr_position hr_position_1 ON hr_position.code = hr_position_1.code INNER JOIN
                      hr_position hr_position_2 ON hr_position_1.code = hr_position_2.code 
WHERE   hr_position.code<>-1 and  (hr_position.lang = 0) AND (hr_position_2.lang = 2) AND (hr_position_1.lang = 1)".$sorting;

$result= DB_query($sql,$db);
$num_rows = DB_num_rows($result);
$tot = $_POST['breakdown']+$num_rows;
?>


<input type="hidden" name="amttype" value="" id="amttype">
<input type="hidden" name="invoicebreakdown" value="<?php echo $tot;?>" id="invoicebreakdown">
<input type="hidden" name="subbreakdown" value="" id="subbreakdown">



<table width="95%" border="0"  cellpadding="5" cellspacing="0" class="table_fullwidth">
<tr><th width="30%">Position</th><th width="33%">Position</th>
<th width="32%">ÇÇáãÑßÒ</th>
</tr>

<?php $i=1;
while ($myrow1 = DB_fetch_array($result)) { 
 if ($i % 2==0) $bkg='td1'; else $bkg='td2';
?>

<tr id="trow_<?php echo $i;?>" class="<?php echo $bkg;?> ">
<td align="center"><input type="hidden" name="cur_id_<?php echo $i;?>" id="cur_id_<?php echo $i;?>" value="<?php echo $myrow1['code'];?>"> <input  type="text" name="field1<?php echo $i;?>" id="field1<?php echo $i;?>" class="sel_long1 required" value="<?php echo $myrow1['depe'];?>" tabindex="<?php echo $i;?>_1" > </td>

<td align="center"><input tabindex="<?php echo $i;?>_2" class="sel_long1 required" type="text" name="field2<?php echo $i;?>" id="field2<?php echo $i;?>" value="<?php echo $myrow1['depf'] ;?>">  </td>

<td align="center"><input tabindex="<?php echo $i;?>_4" class="sel_long1" type="text" name="field3<?php echo $i;?>" id="field3_<?php echo $i;?>" value="<?php echo $myrow1['depa'] ;?>"> </td>
<td align="center"> <img src="<?php echo $site_path;?>images/del.png" width="20" class="cur" title="Delete" onclick="deleteitem('<?php echo $myrow1['code'];?>');"></td> 
</tr>
<?php
$i++;

}



$breakdown = $_POST['breakdown']+$num_rows;
$j=$num_rows+1;
while($j<=$breakdown) {
if ($j % 2==0) $bkg='td1'; else $bkg='td2';
?>




<tr id="trow_<?php echo $j;?>" class="<?php echo $bkg;?> "><td align="center">
<input type="hidden" name="cur_id_<?php echo $j;?>" id="cur_id_<?php echo $j;?>" value="">
<input  type="text" name="field1<?php echo $j;?>" id="field1<?php echo $j;?>" class="sel_long1 required" value="" tabindex="<?php echo $j;?>_1" > </td>

<td align="center" ><input  class="sel_long1 required" type="text" name="field2<?php echo $j;?>" id="field2<?php echo $j;?>" value="" tabindex="<?php echo $j;?>_2"></td>

<td align="center"><input  class="sel_long1 required" type="text" name="field3<?php echo $j;?>" id="field3<?php echo $j;?>" value="" tabindex="<?php echo $j;?>_4"> </td>
<td align="center"><img src="<?php echo $site_path;?>images/del.png" title="Delete"  onclick="delete_row_details(<?php echo $j;?>)" width="20" class="cur"></td>
</tr>
<?php $j++; }?>
</table>
<div class="save_bt"><input  name="save" id="savebt" class="no_marright submit-green save_right" value="Save"  type="button" onclick="validate_save()" tabindex="<?php echo $j;?>"> </div>

<div id="did1"></div>




<script>
 var $targets = $('#erp_from_sub').find('input, button , select'),
    steps = $targets.map(function() {
        return $(this).attr('tabindex');
    }).get();

$('#erp_from_sub').on('keypress', 'input, button, select', function(e) {
    if (e.keyCode == 13) {
        var current = $.inArray($(this).attr('tabindex'), steps),
            next = steps[++current % steps.length];
        $targets.filter('[tabindex="' + next + '"]').focus();
    }
});

function deleteitem(a) {
  if(confirm('Are you sure you want delete?')) { document.erp_from_sub.delid.value=a;  document.getElementById('action').value='3';load_subpage('erp_from_sub');}

  }
  function delete_row_details(id)
{
var el = document.getElementById('trow_'+id);
if(confirm("Are you sure you want to delete this row")==true)
el.parentNode.removeChild(el);
/*calculateSum_credit();*/
}
</script>

关于php - 更新行不起作用,保存按钮复制行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45931848/

相关文章:

php - 变量似乎未初始化

mysql - 在 1 where 条件中有 2 个字段

c# - RegEx 字表现 :\w vs [a-zA-Z0-9_]

php - symfony 4 中没有服务 FOSJsRoutingBundle

php - 如何使用 mySQL 为页面标题、下一篇文章、上一篇文章编写 php 代码?

MySQL 在排序记录时仅使用字符串的前 20 个字符

mysql - SQL 查询联合具有不同列数的多个表

mysql - 如何从 XSL 文档进行 SQL 查询?

sql-server - SQL Server 中的表数据类型回滚有何影响?

php - 奇怪的 cURL 错误 - WHMCS 中的 errno 104(代码 56)