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

时间:2013-12-17 07:08:47

标签: javascript php mysql foreach

这里我正在尝试编辑&使用php更新我的动态行值。这是我的edit.php页面编码。它完全从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>";
    }
}

这是我的update.php页面编码。它错误地更新了数据。

更新前:

enter image description here

更新后:

enter image description here

我编辑了所有行和列,当我更新凭证时,它总是更新所有行中的最后一行值。你可以在那个图像中看到。但我正在使用单个文本字段的编辑和更新选项。它运作良好。动态行值错误地更新到数据库中。用于生成动态行我正在使用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);
}

我在这里发布了另一个问题Here is the link for another question

4 个答案:

答案 0 :(得分:1)

如果您有许多行,并且所有这些行都设置了相同的VoucherID_Fk UPDATE设置数据。您需要将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'";

答案 1 :(得分:0)

您没有在for循环中执行sql查询。当循环结束时,它返回最后一个查询,然后使用循环外的mysql_query执行最后一个sql查询。

解决方案:

执行“mysql_query($ e_tariff)或die(mysql_error());”在for循环中,以便查询将在每次循环运行时执行。

示例:

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

希望这会有所帮助: - )

答案 2 :(得分:0)

试试这段代码。我已经评论了你的错误,并使其正确。

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

答案 3 :(得分:0)

根据您的评论: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... 然后尝试这个。

首先删除旧数据,然后插入新数据。

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

然后插入新数据。

 foreach( $_POST['slno'] as $key=>$slno ) {
   $insertquery  = "insert into `ebvouchertariffs`.......";
 }