【问题标题】:Update single column values of Table更新表的单列值
【发布时间】:2017-01-03 10:44:28
【问题描述】:

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

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

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 下使用上面的代码,但现在表格没有显示。

我是编码新手,在问这里之前检查了很多其他帖子......

【问题讨论】:

  • 无需在脚本中创建 td 只需将 id 赋予 Commission1 列并使用 &lt;td id='comm'&gt;commission1 &lt;/td&gt; $("#comm").text($row[commission1]); 更改值
  • @NishantNair 我尝试了你的建议,但它仍然给我同样的结果....我试过echo "&lt;tr class='r$m'&gt; &lt;td id='comm'&gt;commission1 &lt;/td&gt; &lt;/tr&gt;"; & &lt;script&gt;$("#comm").text($row[commission1]);&lt;/script&gt;
  • 你只是在js脚本中使用''
  • @NishantNair 当我在脚本中使用这些代码时,表格将显示:{id: 'commission1' , header: "commission1" , width :"120"}, $("#commission1").text($row[commission1]);,但我需要更新commission1 列的值
  • @NishantNair 我走对了吗?在脚本中包含 php 代码以从数据库中获取佣金的详细信息并尝试更新这些值......

标签: javascript php html database


【解决方案1】:

请按此步骤操作。

添加 this is to user col 创建脚本

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

my_CommissionRenderId 函数

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_fieldDisplay函数

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>

ajaxChangeCommission.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";
    }

?>

【讨论】:

  • kk 慢慢来。
【解决方案2】:

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

<?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 按钮显示在表格外。我需要在表格中显示edit 按钮,你能帮我吗......
  • @user5348fh8y5 当前的 tr 元素被直接附加到正文而不是表格。由于您使用的是网格小部件,因此您可以更轻松地使用网格中的可用选项来操作列。检查此链接以获取示例github.com/sigmawidgets/…
  • @user5348fh8y5 如果你不能让它工作,请创建一个 plunkr/jsfiddle 并分享。
  • 当然,感谢您的建议,我会尝试您提供的链接并更新您.......
猜你喜欢
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2017-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多