将循环输出保存到数据库

时间:2012-09-10 14:40:16

标签: php mysql

  1. 在下面的代码上回显$ AA它有效​​! echo显示$ rec ['totsb']正确减去1,从数据库中的现有值减去1。然后我需要保存在减少一个回到数据库后生成的新数字,这是我的代码肯定是错误的。我尝试了许多替代方案来纠正它,但无法通过。有人可以告诉我如何将新号码保存回数据库吗?
  2. info:我的数据库如下所示。正如你所看到的那样,数字之间是一个下拉列表,由用户选择。(数据库只有302开始,结束为309,下拉有所有302,303,304 ... 309)所以如果用户选择306,例如它应自动识别起始编号和结束编号306之间的编号,并在totsb中适当保存新编号。

    +--------+---------+------+
    |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;
         }
        }
    }
    ?>
    

2 个答案:

答案 0 :(得分:1)

尝试更改此行

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

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

答案 1 :(得分:0)

除非您缺少代码......

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

你可以更多地清理你的代码,但这应该让你走上正确的轨道