PHP验证数据错误

时间:2013-04-15 04:59:01

标签: php mysql

我的PHP验证数据存在问题。 这是我的代码到目前为止

$cek_saldo=mysql_query
("SELECT * FROM t_balance");

        while ($data_cek = mysql_fetch_array($cek_saldo));
        {
            $b_id = $data_cek['badge_id'];
            $mon = $data_cek['month'];
            $bal = $data_cek['balance_type'];
        }

        if ($b_id == '$badge_id' AND $mon == '$date_month' AND $b_type == '$jns_saldo')
        {
            echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
        }

        else
        {
            if($_POST)
            {
                $query = "INSERT INTO t_balance(badge_id, balance_amount, month, balance_type, date_transaction)
                        VALUES ('$badge_id', '$saldo', '$bulan', '$jns_saldo', '$date_transaction')
                ";
                $hasil = mysql_query($query);

                if($hasil)
                {

                    echo "<div class='emp_err success'>Balance transaction successfully added.</div>";
                }
                else
                {

                    echo "<div class='emp'>Gagal menambahkan saldo.</div>";
                }
            }
        }

规则是:
Tabungan Wajib每月可提交1次。因此,如果两次,则会出错:“之前已添加了本月的余额。”

Tabungan Tambahan每月可提交超过1次。因此,如果提交超过1次,则会保存。

有人有建议吗?

4 个答案:

答案 0 :(得分:0)

if ($b_id == "$badge_id" AND $mon == "$date_month" AND $b_type == "$jns_saldo")
        {
            echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
        }

您的代码因使用变量的比较中的单引号而失败,尝试了解php中单引号和双引号字符串之间的区别

答案 1 :(得分:0)

此验证无效  原因是您要从数据库中选择多行并仅将最后一行指定给$b_id .. $mon

您正在使用single quotes进行比较

解决

  1. 如果可以

    选择基于某个ID的数据
    $cek_saldo=mysql_query("SELECT * FROM t_balance where id = 'someid'");
    
    while ($data_cek = mysql_fetch_array($cek_saldo));
    {
        $b_id = $data_cek['badge_id'];
        $mon = $data_cek['month'];
        $bal = $data_cek['balance_type'];
    }
    
  2. 设置错误标志

    $error = false;
    
    while ($data_cek = mysql_fetch_array($cek_saldo));
    {
        $b_id = $data_cek['badge_id'];
        $mon = $data_cek['month'];
        $bal = $data_cek['balance_type'];
        if($b_id == $badge_id AND $mon == $date_month AND $b_type == $jns_saldo)
       {
          $error = true;
          break
       }
    }
     if($error)
     {
        echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
    
     }
    
  3. 希望有所帮助

答案 2 :(得分:0)

您无需引用。我认为$badge_id $date_month $jns_saldo来自您的表单,内容为$ _POST

if ($b_id == $badge_id AND $mon == $date_month AND $b_type == $jns_saldo)

答案 3 :(得分:0)

使用此功能。

$cek_saldo= mysql_query("SELECT * FROM t_balance");

    while ($data_cek = mysql_fetch_array($cek_saldo));
    {
        $b_id = $data_cek['badge_id'];
        $mon = $data_cek['month'];
        $bal = $data_cek['balance_type'];

        if ($b_id == "$badge_id" && $mon == "$date_month" && $b_type == "$jns_saldo")
        {
            echo "<div class='emp_err warn'>Balance for this month has been added before.</div>";
        }
        else
        {
            if($_POST)
            {
                $query = "INSERT INTO t_balance(badge_id, balance_amount, month, balance_type, date_transaction)
                        VALUES ('".$badge_id."', '".$saldo."', '".$bulan."', '".$jns_saldo."', '".$date_transaction."')";
                $hasil = mysql_query($query);

                if($hasil)
                {

                    echo "<div class='emp_err success'>Balance transaction successfully added.</div>";
                }
                else
                {

                    echo "<div class='emp'>Gagal menambahkan saldo.</div>";
                }
            }
        }
    }