将循环输出保存到数据库


Save loop output to database

  1. 在下面的代码上回声$AA它工作!echo正确显示$rec['totsb']减去1,从数据库中的现有值中减去1。然后,我需要将减少一个数字后生成的新数字保存回数据库,我的代码肯定是错误的。我试了很多方法来纠正它,但都没能通过。有人能告诉我如何将新号码保存回数据库吗

info:我的数据库如下。正如您所看到的,中间的数字填充在下拉列表中,由用户选择。(数据库只有起始编号302和结束编号309,下拉列表中有所有的302303304…309)因此,例如,如果用户选择306,它应该自动识别起始编号和结束编号306之间的位置,并将新编号保存为合适的总数。

+--------+---------+------+
|sbstart |sbend    | totsb|
+--------+---------+------+
|302     |309      | 8    |
|200     |208      | 9    |
|405     |409      | 5    |
+--------+---------+------+

代码:

<?php
$con=mysql_connect('localhost','root') or die ("Server connection failure!");
$db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the database");
$SQL="SELECT * FROM newchk";
$run=mysql_query($SQL,$con) or die ("SQL Error");
$nor=mysql_num_rows($run);
while ($rec = mysql_fetch_array($run))
{
for($i=$rec['sbstart']; $i<=$rec['sbend']; $i++)
    {
    $opt=$_POST['options'];
    if($i = $opt)
     {
        if($rec['totsb'] <= "0")
        {
        echo "You have already entred this cheque number."; 
        return false;
        } else {
        echo "You can proceed with this entry";
        $AA = $rec['totsb']-1;
        $BB=$rec['sbstart'];
        echo $AA;
        $con=mysql_connect('localhost','root') or die ("Server connection      failure!");
        $db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the     database");
        $SQL="UPDATE newchk SET totsb='$AA'";   
        return false;
        }
     }
     else 
     { echo "Error: Cant find choosen in the databse"; 
      return false;
     }
    }
}
?>

尝试更改此行

$SQL="UPDATE newchk SET totsb='$AA'";

$SQL="UPDATE newchk SET totsb=".$AA;

除非您缺少代码。。。

1) 您没有像第一个那样对SQL语句执行任何操作(需要实际运行它)
2) 如果金额如您在chema表中所示为数字,则不需要在其周围加引号
3) 还建议使用您的sbstart作为要更新的记录的限定符,否则您将更新所有内容。

$SQL2="UPDATE newchk SET totsb=$AA where sbstart=$BB"; 
$run2=mysql_query($SQL2,$con) or die ("SQL Error"); 

如果这没有帮助,请发送更多关于数据库架构的信息

添加的代码

$matches=0;
$opt=$_POST['options'];
while ($rec = mysql_fetch_array($run)){
    if($opt>=$rec['sbstart'] && $opt<=$rec['sbend']){
        # we have a match, run your code
        if($rec['totsb'] <= "0") 
        { 
        echo "You have already entred this cheque number.";  
        return false; 
        } else { 
        echo "You can proceed with this entry"; 
        $AA = $rec['totsb']-1; 
        $BB=$rec['sbstart']; 
        echo $AA; 
        $con=mysql_connect('localhost','root') or die ("Server connection      failure!"); 
        $db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the     database"); 
# fixed lines
        $SQL2="UPDATE newchk SET totsb=$AA where sbstart=$BB"; 
        $run2=mysql_query($SQL2,$con) or die ("SQL Error"); 
# end fixed lines
        return false; 
        } 
        # end your code
        $matches++
    }else{
        # no match
    }
} # while loop through records
if($matches==0){
    echo "Error: Cant find choosen in the databse";  
    return false; 
}else{
    return true;
}

您可以对代码进行更多的清理,但这应该会让您走上正确的轨道