PHP verification data error - php

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

You don't need to quote.I suppose $badge_id $date_month $jns_saldo come from your form with $_POST
if ($b_id == $badge_id AND $mon == $date_month AND $b_type == $jns_saldo)

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

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

Related

Non-noob way to fetch data in Laravel with jQuery

I'm rebuilding an application in Laravel and currently use the following code, I guess this would be called AJAX.
$('.date').change(function () {
var date = $('.date').val();
$('#date_summary').html(date);
if (date == "") {
$('#date_summary').html('Choose your service date...');
} else if ($.trim(date) !== '') {
$.get('/calendar.php', {
date: date
}, function (data) {
var data = data.toString();
$('#date_data').html(data);
});
}
});
$('.calendar').trigger('change');
To fetch this code:
if ( isset($_GET['date']) === true && empty($_GET['date']) === false ) {
global $db;
$date = $_GET['date'];
$query = "select * from appointments where Day = :date";
$statement = $db->prepare($query);
$statement->bindValue(':date', $date);
$statement->execute();
$result = $statement->fetch();
$statement->closeCursor();
$eight_to_nine = $result['8AM_9AM'];
$nine_to_ten = $result['9AM_10AM'];
$ten_to_eleven = $result['10AM_11AM'];
$eleven_to_twelve = $result['11AM_12PM'];
$twelve_to_one = $result['12PM_1PM'];
$one_to_two = $result['1PM_2PM'];
$two_to_three = $result['2PM_3PM'];
$three_to_four = $result['3PM_4PM'];
$four_to_five = $result['4PM_5PM'];
$five_to_six = $result['5PM_6PM'];
$six_to_seven = $result['6PM_7PM'];
if ($eight_to_nine != 1) {
echo "<option value='8AM_9AM'>8:00AM - 9:00AM</option>";
}
if ($nine_to_ten != 1) {
echo "<option value='9AM_10AM'>9:00AM - 10:00AM</option>";
}
if ($ten_to_eleven != 1) {
echo "<option value='10AM_11AM'>10:00AM - 11:00AM</option>";
}
if ($eleven_to_twelve != 1) {
echo "<option value='11AM_12PM'>11:00AM - 12:00PM</option>";
}
if ($twelve_to_one != 1) {
echo "<option value='12PM_1PM'>12:00PM - 1:00PM</option>";
}
if ($one_to_two != 1) {
echo "<option value='1PM_2PM'>1:00PM - 2:00PM</option>";
}
if ($two_to_three != 1) {
echo "<option value='2PM_3PM'>2:00PM - 3:00PM</option>";
}
if ($three_to_four != 1) {
echo "<option value='3PM_4PM'>3:00PM - 4:00PM</option>";
}
if ($four_to_five != 1) {
echo "<option value='4PM_5PM'>4:00PM - 5:00PM</option>";
}
if ($five_to_six != 1) {
echo "<option value='5PM_6PM'>5:00PM - 6:00PM</option>";
}
if ($six_to_seven != 1) {
echo "<option value='6PM_7PM'>6:00PM - 7:00PM</option>";
}
}
My question is, is there a more elegant way to do this in Laravel? I'm just trying to implement what are considered best practices. Off the top of my head, I think this would be considered something like an API? Any help is appreciated.
If you are asking about a better way to do this in Laravel, then surely configuring API will be a prominent way to do that.
Simply configure API routes, and in the Controller method use the Laravel Eloquent ORMs to have the Database transactions.
Then finally call the routes in your AJAX request and wait for the response. Then do the final operations based on the returned response from Laravel.

Php flip the echo result

I confused the method to flip the place of the echo result, below is my code
while ($looptools == 0) {
$mysqlihelper = " SELECT * FROM soal WHERE nomorsoal = $numberofmysqli ";
$mysqliquery = mysqli_query($konek, $mysqlihelper);
$resultquery = mysqli_fetch_assoc($mysqliquery);
$resulttextjudul = $resultquery['judul'];
if ($resulttextjudul == null) {
unset($resulttextjudul);
$resulttextjudul = "Tunggu Soal Berikutnya ! ..";
$nullerror = true;
} else {
}
if ($nullerror == true) {
echo "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">" . $resulttextjudul . "</div>";
} else {
echo "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">" . $resulttextjudul . "</div>";
}
if ($nullerror == true) {
mysqli_close($konek);
break;
} elseif ($looptools == 10) {
mysqli_close($konek);
break;
} else {
}
}
As you see in the "while", it's echo the first result and the second result below it, but I want the first result in below of the second result, can anyone tell me the method to do it?
I assume you mean you want to print all successes followed by all errors, or something like that. Here's how:
if($nullerror == true){
$errors .= "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>";
}else{
$successes .= "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>";
}
Then, when the while loop is done:
echo $successes ;
echo $errors ;
If you really want to work your way backwards through the results in $mysqliquery, that's a different answer.
UPDATE: to reverse the order of successes / errors on display, just put the latest additions in front:
if($nullerror == true){
$errors = "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>" . $errors;
}else{
$successes = "<div class=\"head-main-recenttest-result\" style=\"text-decoration:none\">".$resulttextjudul."</div>" . $successes ;
}
sorry if i answer my own question because i found it out myself
use mysqli_num_rows or often called mysqli count
http://php.net/manual/en/mysqli-result.num-rows.php

counting rows if any one of the field is empty in mysql

hi i have several columns in my table if any of the column is empty it should be count as 1...at the same time if 2 or more columns empty in same row...it should not count as 2...
help me the mysql query.....
<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$a=$row['ch1'];
$b=$row['ch2'];
$c=$row['ch3'];
$d=$row['ch4'];
$e=$row['ans'];
$f=$row['ques'];
}
}
else
{
echo "";
}
?>
if $a or $b or $c or $d or $e or $f is empty...it should count as 1...per row only once..not to count as 2 for the same row
Try this simple code
<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
$empty_record = 0;
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$a=$row['ch1'];
$b=$row['ch2'];
$c=$row['ch3'];
$d=$row['ch4'];
$e=$row['ans'];
$f=$row['ques'];
if($a=='' || $b=='' || $c=='' || $d=='' || $e=='' || $f=='')
{
$empty_record++;
}
}
}
else
{
echo "";
}
echo $empty_record;
?>
I would try something like this:
<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
if(mysql_num_rows($result)>0)
{
$numOfEmpty = 0;
while($row=mysql_fetch_row($result))
{
for($i = 0;$i<count($result);$i++) {
if ($result[$i] == "") {
$numOfEmpty++;
break;
}
}
}
echo $numOfEmpty;
}
else
{
echo "";
}
?>
Please let me know however, if the code works. :)
I don't really understand your problem.
Maybe this will help - you can use the isempty() function, which will return true if your variable is empty.
Make a variable named empty and give it value 0. Then on your for loop add this:
if(isempty($a)||isempty($b)||isempty($c)||isempty($d)||isempty($e)||isempty($f))
$empty++;

IF Statement Inside Function

I am writing a if statement that if the two dates listed are equal it echo's the first date and if the they aren't(else statement) it echoes "date1-date2" I have it inside a function and I do not know if I am alowed to do that.Here is the error I am getting
Parse error: syntax error, unexpected T_IF
Here is my function:
function getfevent ($conn) {
$sql = "SELECT `name` FROM `event` WHERE `featured` = 0 LIMIT 0, 30 ";
$statement=$conn->prepare($sql);
$statement->execute();
while($row=$statement->fetch()) {
$eventname = $row['name'];
$row_id=geteventid ($conn,$eventname);
$row_end=geteventend ($conn,$eventname);
$date=if ($row_id == $row_end){
echo $row_id;
}
else {
echo $row_id " - " $row_end;
}
?>
<?php echo "<td>$eventname</td><td>$date</td></tr>"; ?>
<?php
}
}
getfevent($conn);
Probably you want this.
$date = $row_id == $row_end ? $row_id : $row_id . " - " . $row_end;
echo $date;
Don't forget. Use . to concat.
It's the same of
if ($row_id == $row_end) {
$date = $row_id;
} else {
$date = $row_id . " - " . $row_end;
}
Yes, of course If statements are allowed inside functions. Your problem is that you are setting your variable $date's value to the value of a T_IF, which is illegal.
Rather, you could do something like this:
$date=($row_id == $row_end);
if($date)
{
echo $row_id;
}
else {
echo $row_id." - ".$row_end;
}
Instead of:
$date=if ($row_id == $row_end){
echo $row_id;
}
else {
echo $row_id " - " $row_end;
}
Try:
$date;
if ($row_id == $row_end)
$date=$row_id;
else
$date=$row_id." - ".$row_end;
What you're doing in the original code is essentially telling PHP to set the value of variable $date to an if statement, which is, well, not possible.
The alternate piece of code creates a new variable $date, and then sets its value to $row_id or $row_id." - ".$row_end; depending on how the if statement evaluates.
The echo statement is used to print something to the output buffer.
you have to do this instead:
if ($row_id == $row_end) {
$date = something
} else {
$date = somethingElse
}
//do whatever you want with $date
try
$date=$row_id;
if ($row_id != $row_end)
{
$date.=" - ".$row_end;
}
which should be much shorter i gues

PHP Syntax Error?

I have coded a nice script but i am constantly getting
Error on line 29: Parse error, unexpected T_IF(if)
I have tried debugging code, wasted plenty of time. But nothing, came out.
Here is my code.
<?php
include("geoip.inc");
$ip=$_SERVER['REMOTE_ADDR'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, "$ip");
$referrer=$_SERVER['HTTP_REFERER'];
// Country name is not used so commented
// Get Country Name based on source IP
//$country = geoip_country_name_by_addr($gi, "$ip");
$real=0;
geoip_close($gi);
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
$real = 1;
}
else {
if ($_COOKIE['iwashere'] != "yes") {
setcookie("iwashere", "yes", time()+315360000);
if ($country_code="IN") {
if(preg_match('/google/i', $referrer)) {
$key = "g17x9erm28n7cgifddssfqhgorjf3e"; // Account API Key
$ip = $_SERVER['REMOTE_ADDR']; // IP to Lookup
$result = file_get_contents('http://www.ipqualityscore.com/api/ip_lookup.php?KEY='.$key.'&IP='.$ip);
$real=$result
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's
{if($real==0)
{setcookie("testcookie", "testvalue");
if( isset( $_COOKIE['testcookie'] ) ) {
if (isset($_POST['jstest'])) {
$nojs = FALSE;
} else {
// create a hidden form and submit it with javascript
echo '<form name="jsform" id="jsform" method="post" style="display:none">';
echo '<input name="jstest" type="text" value="true" />';
echo '<script language="javascript">';
echo 'document.jsform.submit();';
echo '</script>';
echo '</form>';
// the variable below would be set only if the form wasn't submitted, hence JS is disabled
$nojs = TRUE;
}
if ($nojs){
$real=1;
}
}
else
$real=1;
}
else
$real=1;
} else
$real = 1;
}
else {
$real = 1;
}
} }
if ($real==1) {
include_once('Biggenius1.htm');
}
?>
It is if inside. Please give me advice, on how can i avoid these error. And also is there any alternative to code such complex script with multiple nested if statements?
Please post entire code:
try this
$real = 0;
geoip_close($gi);
if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
$real = 1;
} else {
if ($_COOKIE['iwashere'] != "yes") {
setcookie("iwashere", "yes", time() + 315360000);
if ($country_code = "IN") {
if (preg_match('/google/i', $referrer)) {
$key = "g17x9erm28n7cgifddssfqhgorjf3e"; // Account API Key
$ip = $_SERVER['REMOTE_ADDR']; // IP to Lookup
$result = file_get_contents('http://www.ipqualityscore.com/api/ip_lookup.php?KEY=' . $key . '&IP=' . $ip);
$real = $result;
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's {
if ($real == 0) {
setcookie("testcookie", "testvalue");
if (isset($_COOKIE['testcookie'])) {
if (isset($_POST['jstest'])) {
$nojs = FALSE;
} else {
}
// create a hidden form and submit it with javascript
echo '<form name="jsform" id="jsform" method="post" style="display:none">';
echo '<input name="jstest" type="text" value="true" />';
echo '<script language="javascript">';
echo 'document.jsform.submit();';
echo '</script>';
echo '</form>';
// the variable below would be set only if the form wasn't submitted, hence JS is disabled
$nojs = TRUE;
}
if ($nojs) {
$real = 1;
}
}
else
$real = 1;
}
else
$real = 1;
} else
$real = 1;
}
else {
$real = 1;
}
}
if ($real == 1) {
include_once('Biggenius1.htm');
}
On line 29, $real=$result should end in a semi-colon and on the following line {if($real==0) should be if($real==0){.
The error message is your friend, it suggested you look to line 29.
You placed a curely braces before the if condition
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's
{if($real==0)
remove it then your error wil be removed
From reading over your code, it seems like the only errors I can find are these:
{if($real==0)
And:
$real=$result
Which should be changed into:
if($real==0){
And:
$real=$result;
Here are the few errors I found:
if ($country_code="IN") : This is an assignment not comparision, will always return true
$real=$result : Missing Termination ; on the end

Categories