i have a code here that
if the $cost <= $row2['cost_disbursed'], it will execute the if statement here
but, it still deducts the values even if it is less than the value
while ( ($row = mysqli_fetch_array($query_result)) && ($row2 =
mysqli_fetch_array($query_result2)) ) {
if ( $cost <= $row2['cost_disbursed'] ) {
if ($cost <= $row['remaining']){
$sql= "UPDATE project set cost_disbursed = cost_disbursed - '$cost'
WHERE id = '$id'";
$result = mysqli_query($con,$sql);
$sql2= "UPDATE budget set remaining = remaining - '$cost' where id='1'";
$result2 = mysqli_query($con,$sql2);
echo "<script>window.alert('Balance successfully deducted!')
location.href = 'disbursed-Page.php'</script>";
}
}
else {
echo "<script>window.alert('Balance is not enough')
location.href = 'disbursed-Page.php'</script>";
}
}
Related
i have two button on my homepage one is time-in and the other is time-out,
i want to prevent the user/student to time-in using same id if he did not put time-out on his last time-in to create valid entry. Hope you can help me.
here is my php code:
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn, "SELECT * FROM stud WHERE rfid_num = '$rfid'");
$count = mysqli_num_rows($sql);
if ($count == 0 ) {
header("location:notexist.php");
} elseif (empty($row['timeout'])) {
header("location:page say the user/student need to put timeout first before time-in again");
} else {
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
?>
this is my answer just wanna share it, i just add select student_att table
to fetch the data and check if timeout column is empty.
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn,"select * from stud where rfid_num ='$rfid' ");
$count = mysqli_num_rows($sql);
if ($count == 0) {
header("location:notexist.php");
}else{
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$sql1 = mysqli_query($conn,"select * from student_att where rfid_num ='$rfid' order by number DESC limit 1 ");
while( $row = mysqli_fetch_array($sql1)) {
if(empty($row['timeout'])){
header("location:logout.php");
}else{
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
}
}
?>
how to check resulted row values from while loop are same,I need to update when status is completed from all results...
$result = $db->query("SELECT * FROM fy_working_staf_cstm WHERE task_id_c='".$t_id."'");
while($row = $db->fetchRow($result)){
++$tas;
$staff_id=$row['id_c'];
$result1 = $db->query("SELECT `status` FROM `fy_working_staf` WHERE id='".$staff_id."' AND `status`='Completed'");
$staf = $db->fetchByAssoc($result1);
$status = $staf['status'];
if($stat=='Completed')
{
++$tas1;
//$comple_staus='Closed_Closed';
}
}
if(($tas == $tas1) && ($tas1 !=0) )
{
$q = $db->query("UPDATE `tasks` SET `status`='Completed' WHERE id='".$t_id."' ");
$st = $db->fetchByAssoc($q);
}
Instead i can use break condition in this as :
while($row = $db->fetchRow($result)){
$staff_id=$row['id_c'];
$result1 = $db->query("SELECT `status` FROM `fy_working_staf` WHERE id='".$staff_id."' AND `status`='Completed'");
$staf = $db->fetchByAssoc($result1);
$status = $staf['status'];
if($stat!='Completed')
{
$comple_staus='Closed_Closed';
break;// will exit the loop when the row is not same
}
}
i have a problem figuring out, how to roll the dice/s, so that the result/s will either do nothing or only UPDATE the selected users inventory.
<?php
if(isset($_SESSION['loggedin']))
{
include 'system/config.php';
//SESSION
$username = $_SESSION['loggedin'];
//selecting id from table users
$sql = "SELECT id FROM users WHERE username ='$username'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
//the user id from users
$user_id = $row['id'];
$sql = "SELECT user_id, size_kg, fish1, fish2, fish3, fish4, fish5, seaweed FROM inventory WHERE user_id='$user_id'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$userId= $row['user_id'];
$fish1 = $row['fish1'];
$fish2 = $row['fish2'];
$fish3 = $row['fish3'];
$fish4 = $row['fish4'];
$fish5 = $row['fish5'];
$seaweed = $row['seaweed'];
//for debug
echo "$userId. id " . "$fish1 . fish1 <br>";
//$CatchProbability: dice roll for Catch Probability (ex: CatchProbability >= 30; echo You cought a $FishType(fish1, fish2, fish3, fish4, fish5, seaweed))
function rollcatch() {
return mt_rand(1,100);
}
echo rollcatch()." catch <br>";//for debug
//$FishType: dice roll for type of Fish (ex: $FishType(fish1) = 1-10 , $FishType(fish2) = 11-20, $FishType(fish4) = 31-40 $FishType(fish5) = 41-50, $FishType(seaweed) = 51-100)
function rolltype() {
return mt_rand(1,100);
}
echo rolltype()." type <br>";//for debug
function catchFish(){
if(rollcatch() < 30){
$rolltype = rolltype();
$result = "";
if($rolltype > 0 && $rolltype<10){
$result = "fish1";
}
else if($rolltype > 10 && $rolltype<=20){
$result = "fish2";
}
else if($rolltype > 20 && $rolltype<=30){
$result = "fish3";
}
else if($rolltype > 30 && $rolltype<=40){
$result = "fish4";
}
else if($rolltype > 40 && $rolltype<=50){
$result = "fish5";
}
else
{
$result="seaweed";
}
$sql = "UPDATE inventory SET $result = $result + 1 WHERE user_id='$userId'";
if(mysqli_query($conn, $sql)){
echo("You caught a $result");
}
}
else
{
echo("You caught nothing...");
}
}
catchFish(); //for debug
}
?>
Please, help me to debug, I get this error on successful catch:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in ... on line 72
Line 72
<?
if(mysqli_query($conn, $sql)){
echo("You caught a $result");
}
?>
If I understood your question correctly:
if($CatchProbability <= 30) {
$FishType = rolltype();
if ($FishType <= 10) {
$sql = "UPDATE inventory SET fish1 = fish1 +1 WHERE user_id = '$userId'";
if(mysqli_query($conn, $sql))
{
echo " </br> You caught one Fish1.</br>";
}
}
echo 'You caught a '.$FishType.';
Not entirely sure I understood perfectly, but this code:
Checks if you caught a fish (30% chance)
Determines which fish you caught (10% chance for fish 1-5, 50% chance seaweed)
Adds 1 to the caught fish in the database
Outputs a message of which fish you caught
Hopefully this works for you
function catchFish(){
if(rollcatch() < 30){
$rolltype = rolltype();
$result = "";
if($rolltype > 0 && $rolltype<=10){
$result = "fish1";
}
else if($rolltype > 10 && $rolltype<=20){
$result = "fish2";
}
else if($rolltype > 20 && $rolltype<=30){
$result = "fish3";
}
else if($rolltype > 30 && $rolltype<=40){
$result="fish4";
}
else if($rolltype > 40 && $rolltype<=50){
$result="fish5";
}
else
{
$result="seaweed";
}
$sql = "UPDATE inventory SET $result = $result + 1 WHERE user_id='$user_id'";
if(mysqli_query($conn,$sql)){
echo("You caught a $result.");
}
}
else
{
echo("You caught nothing...");
}
}
This is my table in my database. It should only contain one row.
I have this code, that checks if there is no data , if there is not.. The data is inserted, else it's updated. The problem is, that if i update only Brevet, the value of Baccalaureabt will disappear from my database. Any help?
Code:
if(isset($_POST['submit']))
{ $brevet = $_POST['Brevet'];
$baccalaureatbt = $_POST['Baccalaureatbt'];
$sql1="SELECT Brevet,Baccalaureatbt FROM university";
if ($result=mysqli_query($con,$sql1))
{
$rowcount=mysqli_num_rows($result);
}
if($rowcount==0)
{
$sql="INSERT INTO university(Brevet,Baccalaureatbt) VALUES('$brevet','$baccalaureatbt')";
$result = mysql_query($sql);
}
else
{
$sql2 = "UPDATE university SET Brevet = '$brevet' , Baccalaureatbt = '$baccalaureatbt'";
$result2 = mysql_query($sql2);
}
The issue is that you are setting both values, if however either $baccalaureatbt or $brevet is empty, it will be updated with a empty value, i.e the original value will be deleted.
There are multiple ways to avoid this, but one way to do it is like so:
$sql2 = "UPDATE university SET ";
if(!empty($brevet)){
$sql2 .= "Brevet = '$brevet'";
$first = 1;
}
if(!empty($baccalaureatbt)){
$sql2 .= isset($first) ? ", Baccalaureatbt = '$baccalaureatbt'" : "Baccalaureatbt = '$baccalaureatbt' " ;
}
$result2 = mysql_query($sql2);
To update the values dynamically, use the following code. Just add the values you want in the values array.
if(isset($_POST['submit']))
{
$brevet = $_POST['Brevet'];
$baccalaureatbt = $_POST['Baccalaureatbt'];
$sql1="SELECT Brevet,Baccalaureatbt FROM university";
$result=mysqli_query($con,$sql1);
if ($result)
{
$rowcount=mysqli_num_rows($result);
}
if($rowcount==0)
{
$sql="INSERT INTO university(Brevet,Baccalaureatbt) VALUES('$brevet','$baccalaureatbt')";
$result = mysql_query($sql);
}
else
{
$values = array("brevet","baccalaureatbt");
$sql2 = "UPDATE university ";
$n = 0;
foreach($_POST as $key => $val) {
if(in_array($key, $values)) {
$sql2 += ($n !== 0 ? ", " : 0);
$sql2 += "SET".$key." = ".$val;
$n++;
}
if($n > 0) {
$result2 = mysql_query($sql2);
}
}
}
}
Okay so I am new to PHP and attempting to make a script that takes all of the data from a mysql database I have of stock prices and then looks to see if there was an increase in the stock price during after hours trading (by comparing one day's close price with the next day's open price). I have set up a few scripts like this that work, but for some reason this script isn't copying the data into my sql database and I am completely stumped as to why it won't.When I set up echo statements throughout I discovered that my code is seemingly being executed everywhere but the data isn't transferring. Any help is greatly appreciated.
<?php
include("../includes/connect.php");
function masterLoop(){
$mainTickerFile = fopen("../tickerMaster.txt","r");
while (!feof($mainTickerFile)){
$companyTicker = fgets($mainTickerFile);
$companyTicker = trim($companyTicker);
$nextDayIncrease = 0;
$nextDayDecrease = 0;
$nextDayNoChange = 0;
$total = 0;
$overnight_change = 0;
$overnight_change_pct = 0;
$sumOfIncreases = 0;
$sumOfDecreases = 0;
$sql = "SELECT date, open, close, percent_change FROM $companyTicker";
$result = mysql_query($sql);
if($result){
while($row = mysql_fetch_array($result)){
$date = $row['date'];
$percent_change = $row['percent_change'];
$open = $row['open'];
$close = $row['close'];
$sql2 = "SELECT date, open, close, percent_change FROM $companyTicker WHERE date > '$date' ORDER BY date ASC LIMIT 1";
$result2 = mysql_query($sql2);
$numberOfRows = mysql_num_rows($result2);
if($numberOfRows==1){
$row2 = mysql_fetch_row($result2);
$tom_date= $row2[0];
$tom_open= $row2[1];
$tom_close= $row2[2];
$tom_percent_change= $row2[3];
if ($close == 0){
$close = $tom_open;
}
$overnight_change = $tom_open - $close;
$overnight_change_pct = ($overnight_change/$close)*100;
if($overnight_change_pct > 0){
$nextDayIncrease++;
$sumOfIncreases += $tom_percent_change;
$total++;
}else if($overnight_change_pct < 0){
$nextDayDecrease++;
$sumOfDecreases += $tom_percent_change;
$total++;
}else{
$nextDayNoChange++;
$total++;
}
}else if ($numberOfRows==0){
$total = 1;
$nextDayIncrease = 1;
$nextDayDecrease = 1;
}else{
echo "You have an error in analysis_c3";
}
}
}else{
echo "unable to select $companyTicker <br />";
}
$nextDayIncreasePercent = ($nextDayIncrease/$total) * 100;
$nextDayDecreasePercent = ($nextDayDecrease/$total) * 100;
$averageIncreasePercentage = $sumOfIncreases/$nextDayIncrease;
$averageDecreasePercentage = $sumOfDecreases/$nextDayDecrease;
insertIntoResultTable($companyTicker, $nextDayIncrease, $nextDayIncreasePercent, $averageIncreasePercentage, $nextDayDecrease, $nextDayDecreasePercent, $averageDecreasePercentage);
}
}
function insertIntoResultTable($companyTicker, $nextDayIncrease, $nextDayIncreasePercent, $averageIncreasePercentage, $nextDayDecrease, $nextDayDecreasePercent, $averageDecreasePercentage){
$buyValue = $nextDayIncreasePercent * $averageIncreasePercentage;
$sellValue = $nextDayDecreasePercent * $averageDecreasePercentage;
$trueValue = $buyValue + $sellValue;
$query="SELECT * FROM analysisOvernightGain5 WHERE ticker='$companyTicker' ";
$result=mysql_query($query);
$numberOfRows = mysql_num_rows($result);
if($numberOfRows==1){
$sql = "UPDATE analysisOvernightGain5 SET ticker='$companyTicker',daysInc='$nextDayIncrease',pctOfDaysInc='$nextDayIncreasePercent',avgIncPct='$averageIncreasePercentage',daysDec='$nextDayDecrease',pctOfDaysDec='$nextDayDecreasePercent',avgDecPct='$averageDecreasePercentage',buyValue='$buyValue',sellValue='$sellValue'trueValue='$trueValue' WHERE ticker='$companyTicker' ";
mysql_query($sql);
}else{
$sql="INSERT INTO analysisOvernightGain5 (ticker,daysInc,pctOfDaysInc,avgIncPct,daysDec,pctOfDaysDec,avgDecPct,buyValue,sellValue,trueValue) VALUES ('$companyTicker', '$nextDayIncrease', '$nextDayIncreasePercent', '$averageIncreasePercentage', '$nextDayDecrease', '$nextDayDecreasePercent', '$averageDecreasePercentage', '$buyValue', '$sellValue','$trueValue')";
mysql_query($sql);
}
}
masterLoop();
?>
you have missed , at ,sellValue='$sellValue'trueValue='$trueValue'
it should be ,sellValue='$sellValue',trueValue='$trueValue'