I have an problem with my PHP verification data. This my code so far
$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>";
}
}
}
The rule is :
Tabungan Wajib can be submit for 1 time per month. So if twice, it will give error : "Balance for this month has been added before."
Tabungan Tambahan can be submit more than 1 time per month. So if submit more than 1 time, it will saved.
Anyone have a suggestions ?
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>";
}
Your code failed due to the single quotes in the comparison using variables try to learn the difference between single and double quoted strings in php
This validation will no work reason being you are selecting multiple rows from database and assigning only the last on e to $b_id
.. $mon
etc
and you are using single quotes
for comparison
RESOLUTION
select data based on some id if you can
$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']; }
set the error flag
$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>"; }
Hope it helps
您不需要报价。我想$badge_id
$date_month
$jns_saldo
来自您的表单,带有$ _POST
if ($b_id == $badge_id AND $mon == $date_month AND $b_type == $jns_saldo)
Use this.
$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>";
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.