dynamic row values wrongly updated into mysql php

邮差的信 提交于 2020-01-11 13:47:06

问题


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..

$uid = (int)$_GET['id'];
$tariff_query = mysql_query("SELECT * FROM ebvouchertariffs WHERE VoucherID_Fk = $uid");
if(mysql_num_rows($tariff_query)>=1) {
    echo "<table>
    <tr>
    <td>SL.NO</td>
    <td>DATE</td>
    <td>PARTICULARS</td>
    <td>NO OF NIGHTS</td>
    <td>RATE</td>
    <td>PRICE</td>
    <td>TAX %</td>
    </tr>";
    while($t_row = mysql_fetch_array($tariff_query)) {
        echo "<tr>
            <td><input type=text name=slno[] value= ". $t_row['TariffSlNo'] ."></td>
            <td><input type=text value=". $t_row['TariffDate'] ." name=date[] id=SelectedDate onClick=GetDate(this); readonly=readonly/></td>
            <td><input type=text name=particulars[] placeholder=\"Description\" value=". $t_row['TariffParticulars'] ."></td>
            <td>";
        echo "<select name=noofnights[] value= >";
        echo "<option>" . $t_row['NoOfNights'] . "</option>";
        echo "<option></option>"; 
        echo "<option value=1>1</option>
              <option value=2>2</option>
              <!-- cutted -->
              <option value=20>20</option>";
        echo "</select>"; 
        echo "
            <input type=text onblur=\"this.value=addzeros(this.value)\" onKeyUp=\"return valtxt(this)\" name=rate[] value=". $t_row['TariffRate'] .">

            <input type=text name=price[] value=". $t_row['TariffPrice'] ." readonly=readonly>
            <input type=text name=tax[] onblur=\"this.value=addzeros(this.value)\" onKeyUp=\"return valtxt(this)\" value=". $t_row['TariffTax'] ." >
            <input type=hidden name=taxtotal[] readonly=readonly value= ></td>
        </tr>";
    }
}

This is my update.php page coding. it updates the datas wrongly.

Before Update :

After Update :

i edited all rows and columns and when i updated the voucher it always updates the last row values in all rows. you can see in that image. But i'm using edit and update option for single text field. it workings fine. Dynamic row values wrongly updated into database. for generate dynamic rows i'm using javascript... how to solve this problem?

include("config.php");
if(isset($_POST['submit_val'])) {
   $uid = (int)$_POST["edited"];
    foreach( $_POST['slno'] as $key=>$slno ) {
        $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);
        $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 VoucherID_Fk = '$uid'";
    }
    mysql_query($e_tariff)or die(mysql_error());
    mysql_close($link);
}

I posted another question here Here is the link for another question


回答1:


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'";



回答2:


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




回答3:


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);
}



回答4:


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


来源:https://stackoverflow.com/questions/20628000/dynamic-row-values-wrongly-updated-into-mysql-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!