dynamic row values wrongly updated into mysql php

后端 未结 4 1843
长发绾君心
长发绾君心 2021-01-27 10:58

Here i\'m trying to edit & update my dynamic row values using php. This is my edit.php page coding. it fetch the dynamic row datas from mysql perfectly..

$ui         


        
相关标签:
4条回答
  • 2021-01-27 11:12

    If you have many rows with same VoucherID_Fk UPDATE set data for all this rows. You need have set TariffSlNo to WHERE condition

    $e_tariff = "UPDATE ebvouchertariffs SET 
        TariffDate = '$e_date', 
        TariffParticulars = '$e_particulars', 
        NoOfNights = '$e_noofnights', 
        TariffRate = '$e_rate', 
        TariffPrice = '$e_price', 
        TariffTax = '$e_tax', 
        TariffNetTotal = '$e_nettotal', 
        TariffAddTotal = '$e_totalamount', 
        TariffFinalTotal = '$e_finaltotal', 
        ModifiedOn = NOW() 
    WHERE
        TariffSlNo = '$e_slno' AND VoucherID_Fk = '$uid'";
    
    0 讨论(0)
  • 2021-01-27 11:14

    You are not executing the sql query inside the for loop. when the loop get finished it returns the last query and then execute the last sql query using mysql_query which is outside the loop.

    Solution:

    Execute "mysql_query($e_tariff)or die(mysql_error());" inside the for loop so that the query will get execute every the loop runs.

    Example:

    include("config.php");
    if (isset($_POST['submit_val'])) {
        for ($_POST['slno'] as $key=>$slno) {
            // ....
            // rest of the code
            $e_tariff = "UPDATE ebvouchertariffs SET TariffSlNo = '$e_slno', TariffDate = '$e_date', TariffParticulars = '$e_particulars', NoOfNights = '$e_noofnights', TariffRate = '$e_rate', TariffPrice = '$e_price', TariffTax = '$e_tax', TariffNetTotal = '$e_nettotal', TariffAddTotal = '$e_totalamount', TariffFinalTotal = '$e_finaltotal', ModifiedOn = NOW() WHERE VoucherID_Fk = '$uid'";
            mysql_query($e_tariff)or die(mysql_error());
        }
    

    Hope this helps :-)

    0 讨论(0)
  • 2021-01-27 11:16

    As per your comment: for example if user want to edit a voucher. in that voucher already contains 5 rows. user can edit their row values and if they want add two more rows... then try this.

    first delete the old data and then insert new one.

    $deletequery  = "delete from `ebvouchertariffs` WHERE VoucherID_Fk = '$uid' ";
    

    and then insert new data.

     foreach( $_POST['slno'] as $key=>$slno ) {
       $insertquery  = "insert into `ebvouchertariffs`.......";
     }
    
    0 讨论(0)
  • 2021-01-27 11:19

    try this code. I have commented where your error is and make it correct.

    include("config.php");
    if(isset($_POST['submit_val'])) {
       // $uid = (int)$_POST["edited"]; <-- this will get same id for all records there is no value for "edited" in $_POST so it will update all records.
       foreach( $_POST['slno'] as $key=>$slno ) {
            $uid = (int)$slno; // <-- Update it by slno this will work.
            $e_date = $_POST['date'][$key];
            $e_particulars = $_POST['particulars'][$key];
            $e_noofnights = $_POST['noofnights'][$key];
            $e_rate = $_POST['rate'][$key];
            $e_price = $_POST['price'][$key];
            $e_tax = $_POST['tax'][$key];
            $e_nettotal = $_POST['nettotal'];
            $e_totalamount = $_POST['totalamount'];
            $e_finaltotal = $_POST['finaltotal'];
    
            $e_slno = mysql_real_escape_string($slno); // <-- here you are doing mistake replace $e_slno by $slno
    
            $e_date = mysql_real_escape_string($e_date);
            $e_particualrs = mysql_real_escape_string($e_particulars);
            $e_noofnights = mysql_real_escape_string($e_noofnights);
            $e_rate = mysql_real_escape_string($e_rate);
            $e_price = mysql_real_escape_string($e_price);
            $e_tax = mysql_real_escape_string($e_tax);
            $e_nettotal = mysql_real_escape_string($e_nettotal);
            $e_totalamount = mysql_real_escape_string($e_totalamount);
            $e_finaltotal = mysql_real_escape_string($e_finaltotal);
            $e_tariff = "UPDATE ebvouchertariffs SET TariffSlNo = '$e_slno', TariffDate = '$e_date', TariffParticulars = '$e_particulars', NoOfNights = '$e_noofnights', TariffRate = '$e_rate', TariffPrice = '$e_price', TariffTax = '$e_tax', TariffNetTotal = '$e_nettotal', TariffAddTotal = '$e_totalamount', TariffFinalTotal = '$e_finaltotal', ModifiedOn = NOW() WHERE TariffSlNo = '$uid'";
        }
        mysql_query($e_tariff)or die(mysql_error());
        mysql_close($link);
    }
    
    0 讨论(0)
提交回复
热议问题