简体   繁体   English

使用来自Ajax的javascript和php编辑表中的数据

[英]Edit data from table with javascript and php from ajax

I am a beginner in PHP and Javascript I used a live editable table and I am trying to edit them and then save them to mysql in another page I did like this, but I didn't know how to retrieve where to edit in mysql with the id could some one help please! 我是PHP和Javascript的初学者,我使用了一个实时可编辑表,然后尝试对其进行编辑,然后将它们保存到另一页中的mysql中,我这样做,但是我不知道如何使用该id可以帮上忙! Am i wrong in the request? 我的要求错了吗? it doesn't change in the database like it does in the table in the page. 它在数据库中不会像在页面中的表中那样发生变化。 I sent the data like this: 我发送了这样的数据:

function postedit()
    {
        var _type = $('#typeid').val();
        var _pression = $('#pressionid').val();   
        var _code = $('input[name="code"]').val();
        var _designiation = $('input[name="designiation"]').val();
        var _diametre = $('input[name="diametre"]').val();
        var _epaisseur = $('input[name="epaisseur"]').val();
        var _prix = $('input[name="prix"]').val();
        var _etat = $('input[name="etat"]').val();

        $.post('update.php', {
            posttype: _type,
            postpression: _pression,
            postcode: _code,
            postdesigniation: _designiation,
            postdiametre: _diametre,
            postepaisseur: _epaisseur,
            postprix:_prix,
            postetat:_etat
        },
        function(data){
            $('tbody').append(data);
        });
    }

var _trEdit = null;

$(document).on('click', '.btn-edit',function(){
    _trEdit = $(this).closest('tr');
    var _code = $(_trEdit).find('td:eq(0)').text();
    var _designiation = $(_trEdit).find('td:eq(1)').text();
    var _diametre = $(_trEdit).find('td:eq(2)').text();
    var _epaisseur = $(_trEdit).find('td:eq(3)').text();
    var _prix = $(_trEdit).find('td:eq(4)').text();
    var _etat = $(_trEdit).find('td:eq(5)').text();

    $('input[name="code"]').val(_code);
    $('input[name="designiation"]').val(_designiation);
    $('input[name="diametre"]').val(_diametre);
    $('input[name="epaisseur"]').val(_epaisseur);
    $('input[name="prix"]').val(_prix);
    $('input[name="etat"]').val(_etat);
});

$('#btn-update').click(function(){
    if(_trEdit){
        var _code = $('input[name="code"]').val();
        var _designiation = $('input[name="designiation"]').val();
        var _diametre = $('input[name="diametre"]').val();
        var _epaisseur = $('input[name="epaisseur"]').val();
        var _prix = $('input[name="prix"]').val();
        var _etat = $('input[name="etat"]').val();

        $(_trEdit).find('td:eq(0)').text(_code);
        $(_trEdit).find('td:eq(1)').text(_designiation);
        $(_trEdit).find('td:eq(2)').text(_diametre);
        $(_trEdit).find('td:eq(3)').text(_epaisseur);
        $(_trEdit).find('td:eq(4)').text(_prix);
        $(_trEdit).find('td:eq(5)').text(_etat);

        alert("Ligne Modifier avec succée !");
        _trEdit = null;
    }
});

My sql request in a file update.php like this I thought that if I get the id from the database and search from it, it seems stupid but I am desperate: 我在这样的文件update.php sql请求,我认为如果我从数据库中获取id并从中搜索,这似乎很愚蠢,但我很绝望:

$type_utilisation = $_POST['posttype'];
$pression_tube = $_POST['postpression'];
$code_tube = $_POST['postcode'];
$designiation_tube = $_POST['postdesigniation'];
$diametre_tube = $_POST['postdiametre'];
$epaisseur_tube = $_POST['postepaisseur'];                       
$prix_tube = $_POST['postprix'];
$etat_tube = $_POST['postetat'];
$id_req = ("select id from $pression_tube where      typepehd='".$type_utilisation."' && code='".$code_tube."'");
$id_tube=$id_req;
echo"$id_tube";

if(isset($pression_tube)){
    if($pression_tube=="pn4"){
        if($type_utilisation=="pehdtelecom" && $pression_tube=="pn4" ){
            echo "verifier le type du Tube S.v.p";
        } else {
            $sql="UPDATE  pn4 SET    typepehd='".$type_utilisation."',code='".$code_tube."',designiation='".$designiation_tube."',diametre='".$diametre_tube."',epaisseur='".$epaisseur_tube."',prix='".$prix_tube."',etat='".$etat_tube."' where id='".$id_tube."'";      

            $result = mysqli_query($link, $sql);

            if($result){
            } else {
                echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
            }
        }
    }

thank you, i did solve it by using the id because its unique 谢谢,我确实通过使用id来解决它,因为它的唯一性

 $id_tube = $_POST['postid']; 

by adding an input to it in type hidden and make the column invisible by 通过向其添加隐藏类型的输入,并使该列不可见

 style="display;none";

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

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