动态行值错误地更新到MySQL的PHP [英] dynamic row values wrongly updated into mysql php
问题描述
$ 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>
$ t
$ lt; td> $ lt; td> $ lt; / td>
< td>
< td>
< td> ; / 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']。 < /选项> 中;
echo< option>< / option>;
echo< option value = 1> 1< / option>
< optionvalue = 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']。>
< / tr>
$ b这是我的update.php页面编码。它错误地更新数据。
在更新之前: .stack.imgur.com / nJtYc.pngalt =在这里输入图片描述>
更新后:
我编辑了所有的行和列,当我更新凭证时,它总是更新所有行中的最后一行值。你可以看到那个图像。但是我正在使用单个文本字段的编辑和更新选项。它工作正常。动态行值错误地更新到数据库。如何解决这个问题?
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)或die(mysql_error());
mysql_close($ link);
}
解决方案 如果您有许多行具有相同的 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';
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
解决方案 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'";
这篇关于动态行值错误地更新到MySQL的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文