简体   繁体   English

动态行值错误地更新到mysql php

[英]dynamic row values wrongly updated into mysql php

Here i'm trying to edit & update my dynamic row values using php. 在这里,我正在尝试使用php编辑和更新我的动态行值。 This is my edit.php page coding. 这是我的edit.php页面编码。 it fetch the dynamic row datas from mysql perfectly.. 它完美地从mysql获取动态行数据。

$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. 这是我的update.php页面编码。 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? 用于生成动态行,我正在使用javascript ...如何解决此问题?

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 我在这里发布了另一个问题这是另一个问题的链接

If you have many rows with same VoucherID_Fk UPDATE set data for all this rows. 如果您有许多具有相同VoucherID_Fk UPDATE行,请为所有这些行设置数据。 You need have set TariffSlNo to WHERE condition 您需要将TariffSlNo设置为WHERE条件

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

You are not executing the sql query inside the for loop. 您没有在for循环内执行sql查询。 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. 循环完成后,它返回最后一个查询,然后使用循环外的mysql_query执行最后一个sql查询。

Solution: 解:

Execute "mysql_query($e_tariff)or die(mysql_error());" 执行“ mysql_query($ e_tariff)或die(mysql_error());” inside the for loop so that the query will get execute every the loop runs. 在for循环中,这样查询将在每次循环运行时执行。

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 :-) 希望这可以帮助 :-)

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

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... 根据您的评论: 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... 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... 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. 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...尝试此操作。

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

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

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