更新表的单列值

时间:2017-01-03 10:44:29

标签: javascript php html database

我们使用以下代码在表格和表格中显示结果现在,对于 commission1 列值,我们需要在 ajax 的帮助下更新数据库中的值。

enter image description here

在commission1栏下,我们需要添加“编辑”按钮,一旦我们点击“编辑”按钮,就应该允许编辑该值10。一旦我们点击“更新”按钮,它应该在数据库中保存值。

enter image description here

enter image description here

Display.php的

function getDesignerCollection() {
    $user_home = new USER();

    if (!$user_home->is_logged_in()) {
        header("Location: index.php");
        die();
    }

    $stmt = $user_home->runQuery("SELECT * FROM tbl_users");
    $stmt->execute(array(":uid" => $_SESSION['userSession']));
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    $stmt->execute();

    $i=0;
    while($data = $stmt->fetch()) {
        $responce[$i]=array($data['userID'],
                      . $data['phone'],
                      $data['commission1'],
                      .
                      .                  
        $i++;
    }
    echo json_encode($responce);
}

连接

try {
    $dbo = new PDO('mysql:host='.$host_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

脚本

<script>
    var colsOption = [  
        {id: 'phone' , header: "phone" , width :"130"},
    //Edit commission

    <?php 
    $count="SELECT commission1 FROM tbl_users";
    $i=1;

    foreach ($dbo->query($count) as $row) {
        $m=$i%2;
        $sid='s' . $row['commission1'];
        echo "
        <tr class='r$m'>
            <td>
                <input type=button id=$sid value='Edit' onclick=edit_field($row[commission1])> 
            </td>
        </tr>";
        $i=$i+1;
    }
    ?>
//Edit commission end
    ];
</script>

为了显示佣金,我们使用了以下代码:{id: 'commission1' , header: "commission1" , width :"120"},]因为我们需要编辑commission1列的值,现在我们使用//Edit commission下面的代码,但现在表格没有显示。

我是编码和编码的新手。在询问之前检查了很多其他帖子....

2 个答案:

答案 0 :(得分:0)

您应该在正文中而不是在脚本部分中构建html代码。所以将下面的代码移到body部分。 `

<?php 
    $count="SELECT commission1 FROM tbl_users";
    $i=1;

    foreach ($dbo->query($count) as $row) {
        $m=$i%2;
        $sid='s' . $row['commission1'];
        echo "
        <tr class='r$m'>
            <td>
                <input type=button id=$sid value='Edit' onclick=edit_field($row[commission1])> 
            </td>
        </tr>";
        $i=$i+1;
    }
    ?>`

答案 1 :(得分:0)

请按照以下步骤操作。

将此添加到用户列创建脚本

{id: 'commission1' , header: "commission1" , width :"120",renderer : my_CommisionRenderId},

my_CommisionRenderId函数

function my_CommisionRenderId(value ,record,columnObj,grid,colNo,rowNo)
{
    var no= record[columnObj.fieldIndex]; 
    return record[8]+"&nbsp;&nbsp;&nbsp;<input type=button id=sid value='Edit' onclick=edit_fieldDisplay('"+record[0]+"','"+record[1]+"','"+record[8]+"','"+record[2]+"','"+record[3]+"')>";  
}

edit_field显示功能

function edit_fieldDisplay(TxtUserID,TxtDesignName,TxtCommission,TxtEmail,TxtPhone){ 
    $('#DivCommissionEdit').show();

    $('#TdTxtUserID').html(TxtUserID+'<input  type="hidden" id="TxtBxTxtUserID" value=""/>');
    $('#TxtBxTxtUserID').val(TxtUserID);

    $('#TdTxtDesignName').html(TxtDesignName+'<input  type="hidden" id="TxtBxTdTxtDesignName" value=""/>');
    $('#TxtBxTdTxtDesignName').val(TxtDesignName);

    $('#TdTxtPhone').html(TxtPhone+'<input  type="hidden" id="TxtBxTdTxtPhone" value=""/>');
    $('#TxtBxTdTxtPhone').val(TxtPhone);

    $('#TdTxtEmail').html(TxtEmail+'<input  type="hidden" id="TxtBxTdTxtEmail" value=""/>'); 
    $('#TxtBxTdTxtEmail').val(TxtEmail);

    $('#TdTxtCommission').html('<input  type="text" id="TxtBxTdTxtCommission" value="'+TxtCommission+'"/>');

} 

Update_field功能

function Update_field( ){
        var NewTxtCommission=$('#TxtBxTdTxtCommission').val();
        var NewTxtEmail=$('#TxtBxTdTxtEmail').val();
        var NewTxtPhone=$('#TxtBxTdTxtPhone').val();
        var NewTxtDesignName=$('#TxtBxTdTxtDesignName').val();
        var NewTxtUserID=$('#TxtBxTxtUserID').val();
        if(confirm("Do you want to proceed ?")){
            $.post("ajaxChangeCommision.php","NewTxtCommission="+NewTxtCommission+"&NewTxtEmail="+NewTxtEmail+"&NewTxtPhone="+NewTxtPhone+"&NewTxtDesignName="+NewTxtDesignName+"&NewTxtUserID="+NewTxtUserID,function(result,status,xhr){
                alert(result);
                $('#DivCommissionEdit').hide();
                $('#DivCommissionResponse').show();
                $('#DivCommissionResponse').html(result);
                window.location.reload()
            })
            .fail(function(){  alert("something went wrong please try again."); });
        }

    }

在头部添加

<script type="text/javascript">
$(document).ready(function(){
     $('#DivCommissionEdit').hide();
});
</script>

在html部分添加

<div style="margin: 10px; display: block;background:#e6ae64;" align="center" id="DivCommissionEdit">
    <div style="color: #000">
        <span style="font-weight: bold;font-size: 20px;">CHANGE THE COMMISSION HERE</span>
    </div>
    <table border="1">
        <tr >
            <td>Id</td>
            <td>Designer Name</td>
            <td>Phone No</td>
            <td>Email</td>
            <td>Commission</td>
            <td>Action</td>
        </tr>
        <tr>
            <td id="TdTxtUserID"> </td>
            <td id="TdTxtDesignName"> </td>
            <td id="TdTxtPhone"> </td>
            <td id="TdTxtEmail"> </td>
            <td id="TdTxtCommission"> </td>
            <td id=""><input type=button id="" value="UPDATE" onclick="Update_field();"></td>
        </tr>

    </table>
  </div>
  <div style="margin: 10px; display: none;background:#4bb458;" align="center" id="DivCommissionResponse"></div>

<强> ajaxChangeCommision.php

<?php
    $host = "localhost";
    $db_name = "xxx";
    $username = "xxx";
    $password = "xxxxx";

    $NewConn=mysqli_connect($host,$username,$password,$db_name);

if( isset($_POST['NewTxtCommission']) && !empty($_POST['NewTxtCommission']) && $_POST['NewTxtCommission']!="" )
{  $NewTxtCommission=$_POST['NewTxtCommission'];  }else{$NewTxtCommission=0;}

if( isset($_POST['NewTxtEmail']) && !empty($_POST['NewTxtEmail']) && $_POST['NewTxtEmail']!="" )
{  $NewTxtEmail=$_POST['NewTxtEmail'];  }else{$NewTxtEmail="";}

if( isset($_POST['NewTxtPhone']) && !empty($_POST['NewTxtPhone']) && $_POST['NewTxtPhone']!="" )
{  $NewTxtPhone=$_POST['NewTxtPhone'];  }else{$NewTxtPhone=0;}

if( isset($_POST['NewTxtDesignName']) && !empty($_POST['NewTxtDesignName']) && $_POST['NewTxtDesignName']!="" )
{  $NewTxtDesignName=$_POST['NewTxtDesignName'];  }else{$NewTxtDesignName="";}

if( isset($_POST['NewTxtUserID']) && !empty($_POST['NewTxtUserID']) && $_POST['NewTxtUserID']!="" )
{  $NewTxtUserID=$_POST['NewTxtUserID'];  }else{$NewTxtUserID=0;}


$CommissionUpdateQry="UPDATE tbl_users SET commission1='".$NewTxtCommission."' WHERE userID='".$NewTxtUserID."' ;";
    $queryResult=TRUE;
    if( isset($NewConn) && !empty($NewConn) && $NewConn!="" ) {
        $queryResult=mysqli_query($NewConn,$CommissionUpdateQry);
    }

    if($queryResult==TRUE){
        echo "Commission Updated Successsfully";
    }else{
        echo "something went wrong please try again";
    }

?>