分别更新每条记录(php-mysql)

时间:2014-08-26 06:38:40

标签: mysql record updating

嗨我在Mysql数据库中有更新表(basic1)的问题我想单独更新每个记录(设置all_cost_lend列的值为new_cost_lend(all_cost_lend-min_mounth_lend))但是当我执行代码时它给了我重复的记录有一次请帮我解决这个问题。 这是代码

<?php
include("conn.php");
if(isset($_POST['xxx'])and $_POST['xxx']=='admin')
{
$data=mysql_query("select * from salafea order by id") or die(mysql_error());
while($row=mysql_fetch_assoc($data)){
$new_cost_lend=$row['all_cost_lend']-$row['min_mounth_lend'];
$xy=mysql_query("update  salafea set all_cost_lend='$new_cost_lend'")or die  (mysql_error());
if(isset($xy)){
echo"<h1>update done! </h1>";
}
$data1=mysql_query("select * from salafea order by id") or die(mysql_error());
while($row1=mysql_fetch_assoc($data1)){
echo"<table bgcolor='abd9f9'width='79%'border='4'bordercolor='#7ACB83'>
<td bgcolor='#7ACB83'>id</td>
<td bgcolor='#7ACB83'>:emp Name</td>
<td bgcolor='#7ACB83'>:File No</td>
<td bgcolor='#7ACB83'>date_lend:</td>
<td bgcolor='#7ACB83'>name_lend:</td>
<td bgcolor='#7ACB83'>all_cost_lend:</td> 
<td bgcolor='#7ACB83'>before_cash_lend:</td>
<td bgcolor='#7ACB83'>min_mounth_lend:</td>";
echo"<tr>
<td bgcolor='abd9f9'>".$row1['id']."</td>
<td bgcolor='abd9f9'>".$row1['emp_name']."</td>
<td bgcolor='abd9f9'>".$row1['no_file']."</td>
<td bgcolor='abd9f9'>".$row1['date_lend']."</td>
<td bgcolor='abd9f9'>".$row1['name_lend']."</td>
<td bgcolor='abd9f9'>".$row1['all_cost_lend']."</td>
<td bgcolor='abd9f9'>".$row1['before_cash_lend']."</td>
<td bgcolor='abd9f9'>".$row1['min_mounth_lend']."</td>";
}
}
echo"</table>";
}
else
{
echo "<form action='".$server['PHP_SELF']."' method='post'>
<input type='submit' name='submit' value=' Please Press this button'>
<input type='hidden' name='id'/>
<input type='hidden' name='xxx' value='admin'/>
</form>";
$data=mysql_query("select * from salafea order by id") or die(mysql_error());
echo"<table bgcolor='abd9f9'width='79%'border='4'bordercolor='#7ACB83'>
<td bgcolor='#7ACB83'>id</td>
<td bgcolor='#7ACB83'>:emp Name</td>
<td bgcolor='#7ACB83'>:File No</td>
<td bgcolor='#7ACB83'>date_lend:</td>
<td bgcolor='#7ACB83'>name_lend:</td>
<td bgcolor='#7ACB83'>all_cost_lend:</td> 
<td bgcolor='#7ACB83'>before_cash_lend:</td>
<td bgcolor='#7ACB83'>min_mounth_lend:</td>";
while($row=mysql_fetch_assoc($data)){
echo"<tr>
<td bgcolor='abd9f9'>".$row['id']."</td>
<td bgcolor='abd9f9'>".$row['emp_name']."</td>
<td bgcolor='abd9f9'>".$row['no_file']."</td>
<td bgcolor='abd9f9'>".$row['date_lend']."</td>
<td bgcolor='abd9f9'>".$row['name_lend']."</td>
<td bgcolor='abd9f9'>".$row['all_cost_lend']."</td>
<td bgcolor='abd9f9'>".$row['before_cash_lend']."</td>
<td bgcolor='abd9f9'>".$row['min_mounth_lend']."</td>";
}
echo"</table>";
}
?> 

先谢谢

1 个答案:

答案 0 :(得分:0)

问题是你在while中的while循环中添加了while循环if if if(isset($ _ POST [&#39; xxx&#39;])。这就是你获得重复记录的原因。参见更新下面的代码

<?php
include("conn.php");
if(isset($_POST['xxx'])and $_POST['xxx']=='admin')
{

$new_cost_lend=$row['all_cost_lend']-$row['min_mounth_lend'];
$xy=mysql_query("update  salafea set all_cost_lend='$new_cost_lend'")or die  (mysql_error());
if(isset($xy)){
echo"<h1>update done! </h1>";
}

$data1=mysql_query("select * from salafea order by id") or die(mysql_error());
while($row1=mysql_fetch_assoc($data1)){
echo"<table bgcolor='abd9f9'width='79%'border='4'bordercolor='#7ACB83'>
<td bgcolor='#7ACB83'>id</td>
<td bgcolor='#7ACB83'>:emp Name</td>
<td bgcolor='#7ACB83'>:File No</td>
<td bgcolor='#7ACB83'>date_lend:</td>
<td bgcolor='#7ACB83'>name_lend:</td>
<td bgcolor='#7ACB83'>all_cost_lend:</td> 
<td bgcolor='#7ACB83'>before_cash_lend:</td>
<td bgcolor='#7ACB83'>min_mounth_lend:</td>";
echo"<tr>
<td bgcolor='abd9f9'>".$row1['id']."</td>
<td bgcolor='abd9f9'>".$row1['emp_name']."</td>
<td bgcolor='abd9f9'>".$row1['no_file']."</td>
<td bgcolor='abd9f9'>".$row1['date_lend']."</td>
<td bgcolor='abd9f9'>".$row1['name_lend']."</td>
<td bgcolor='abd9f9'>".$row1['all_cost_lend']."</td>
<td bgcolor='abd9f9'>".$row1['before_cash_lend']."</td>
<td bgcolor='abd9f9'>".$row1['min_mounth_lend']."</td>";
}

echo"</table>";
}
else
{
echo "<form action='".$server['PHP_SELF']."' method='post'>
<input type='submit' name='submit' value=' Please Press this button'>
<input type='hidden' name='id'/>
<input type='hidden' name='xxx' value='admin'/>
</form>";
$data=mysql_query("select * from salafea order by id") or die(mysql_error());
echo"<table bgcolor='abd9f9'width='79%'border='4'bordercolor='#7ACB83'>
<td bgcolor='#7ACB83'>id</td>
<td bgcolor='#7ACB83'>:emp Name</td>
<td bgcolor='#7ACB83'>:File No</td>
<td bgcolor='#7ACB83'>date_lend:</td>
<td bgcolor='#7ACB83'>name_lend:</td>
<td bgcolor='#7ACB83'>all_cost_lend:</td> 
<td bgcolor='#7ACB83'>before_cash_lend:</td>
<td bgcolor='#7ACB83'>min_mounth_lend:</td>";
while($row=mysql_fetch_assoc($data)){
echo"<tr>
<td bgcolor='abd9f9'>".$row['id']."</td>
<td bgcolor='abd9f9'>".$row['emp_name']."</td>
<td bgcolor='abd9f9'>".$row['no_file']."</td>
<td bgcolor='abd9f9'>".$row['date_lend']."</td>
<td bgcolor='abd9f9'>".$row['name_lend']."</td>
<td bgcolor='abd9f9'>".$row['all_cost_lend']."</td>
<td bgcolor='abd9f9'>".$row['before_cash_lend']."</td>
<td bgcolor='abd9f9'>".$row['min_mounth_lend']."</td>";
}
echo"</table>";
}
?>