1

我的 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

4 回答 4

0

你不需要引用。我想$badge_id $date_month $jns_saldo来自你的表格,带有 $_POST

if ($b_id == $badge_id AND $mon == $date_month AND $b_type == $jns_saldo)
于 2013-04-15T05:26:58.977 回答
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>";
                }
            }
        }
    }
于 2013-04-15T05:48:15.777 回答
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 中单引号和双引号字符串之间的区别

于 2013-04-15T05:02:14.517 回答
0

此验证将不起作用,因为您从数据库中选择多行并仅将 e 上的最后一个分配给 $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>";
    
     }
    

希望能帮助到你

于 2013-04-15T05:08:24.643 回答