【问题标题】:Save loop output to database将循环输出保存到数据库
【发布时间】:2012-09-10 23:48:19
【问题描述】:
  1. 在下面的代码中回显 $AA 可以正常工作!回显正确显示 $rec['totsb'] 减 1,从数据库中的现有值中减去 1。然后我需要将减一后生成的新数字保存回数据库,这是我的代码肯定是错误的。我尝试了许多替代方法来纠正它,但无法通过。谁能告诉我如何将新号码保存回数据库?

信息:我的数据库如下所示。正如您所见,数字填充在下拉列表中以供用户选择。(数据库仅从 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,3个字段?老实说,我的大脑被冻结了,我根本无法理解你在做什么。
  • 数据库只有sbstart、sbend和totsb三个字段,即sbstart和end的区别。那么我应该如何改变我的方法呢?谢谢

标签: php mysql


【解决方案1】:

尝试更改此行

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

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

【讨论】:

    【解决方案2】:

    除非您缺少代码...

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

    您可以进一步清理您的代码,但这应该会让您走上正轨

    【讨论】:

    • 你也可以重用 $db 链接而不是调用那些连接字符串...除非你在别处关闭它们并且它们没有显示
    • 戴夫,感谢您的回复。这会将生成的新号码保存回数据库。但是,在我上面的演示中,数据库中有多个开始和结束数字的情况下,更新仅发生在第一个数据行。 (我确定这是由于你提到的限定问题。)为了进一步澄清我想说的,例如,如果用户选择一个介于 200 到 208 之间的值,则应根据上述数据库从 9 中减去一个样本。但实际上发生的是一个从 8 减少,这是值范围 302 到 309 的“totsb”。
    • 只是在更新回数据库发生之前,我需要编写代码以找出 sbstart 和 sbend 值落在哪个范围内,然后根据需要进行更新。你觉得我应该怎么处理这件事。感谢您迄今为止的帮助。
    • 那么您从数据库中匹配了错误的“行”。试试这个;
    • 还要检查我在底部验证附近添加的“return true”,你可能不想要那个。
    猜你喜欢
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2010-12-13
    • 2021-07-11
    • 2022-11-28
    • 2015-04-21
    相关资源
    最近更新 更多