简体   繁体   中英

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! 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:

$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_tube = $_POST['postid']; 

by adding an input to it in type hidden and make the column invisible by

 style="display;none";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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