I am trying to generate a either 0 or 1 and when it is generated update a row with the value. 1 being heads and 0 being tails, like a coin flip. I can't use external libraries for this.
The problem is when it generates a 0 it won't update the row but with a 1 it does, and I've no idea as to why.
<?php
include_once('../library/user.php');
include_once('../config/connect.php');
$sql = "SELECT * FROM coinroulette ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
$rows = $result->fetch_array(MYSQLI_ASSOC);
$id = $rows['id'];
$user = $_SESSION["user"]->getUsername();
$beginGame = new DateTime($rows['time']);
$currentTime = new DateTime(date("Y-m-d H:i:s"));
$diff = $currentTime->diff($beginGame);
if ($currentTime < $beginGame) {
echo "Remaining:" . $diff->format('%S') . "<br>";
}
$beginGame->sub(new DateInterval('PT15S'));
//echo $beginGame->format('Y-m-d H:i:s') . "<br>" . $currentTime->format('Y-m-d H:i:s') . "<br>" . $rows['time'] . "<br>";
//echo rand(0,1);
//Check is game is in place. (result == empty) game in place.
//Otherwise its over and a new game is made.
if (empty($rows['result'])) {
echo "Accepting bets.";
if ($currentTime >= $beginGame) {
//Needs to be converted to an INT or else it cant be inserted into datbase. Took me 3 hours to figure this error out. :(
$gamble = mt_rand(0,1);
$sql = "UPDATE coinroulette SET result = '$gamble' WHERE id = '$id'";
$result = $conn->query($sql);
}
} else {
echo "No longer accepting bets.<br>Result: ";
if ($rows['result'] == "1") {
echo "Heads<br>";
} else {
echo "Tails<br>";
}
$sql = "SELECT * FROM bets WHERE gameID = '$id' AND user = '$user'";
$result = $conn->query($sql);
$betRows = $result->fetch_array(MYSQLI_ASSOC);
if ($result->num_rows > 0) {
echo "InGame<br>";
if ($rows['result'] == $betRows['guess']) {
if(empty($betRows['result'])){
$sql = "UPDATE bets SET result = 1 WHERE user = '$user'";
$result = $conn->query($sql);
$winAmount = $betRows['amount'] * 2;
$sql = "UPDATE users SET points = points + '$winAmount' WHERE username = '$user'";
$result = $conn->query($sql);
}
echo "Winner";
} else {
$sql = "UPDATE bets SET result = 0 WHERE user = '$user'";
$result = $conn->query($sql);
echo "Loser";
}
} else {
echo "Not<br>";
}
}
?>
Solved it myself, you cant update a null field. So I set it as -1.
Related
I need to read data from database then edit these data with php and update database with these data.
I can get data from database but not able to write them back (I add another query to see if value is changed)
I’ve tried to use phpMyAdmin to see if values are changed but they are stil
same
$POWER = "";
if ($pressed == "1") {
echo "pressed\n";
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "pressed power request $row[0]\n";
}
if ($POWER == "ON") {
$sql = 'UPDATE `VolleyTest` SET `POWER`=\"OFF\" WHERE ID = 1';
$conn->query($sql);
echo " POWER ON to OFF\n";
} else if ($POWER == "OFF") {
$sql = 'UPDATE `VolleyTest` SET `POWER`=\"ON\" WHERE ID = 1';
$conn->query($sql);
echo " POWER OFF to ON\n";
}
}
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "$row[0]\n";
}
echo $POWER; //. " ". $pressed . " " . $_POST['key'];
You dont need to escape double quotes inside a single quoted string.
Doing that generates a string like this
UPDATE `VolleyTest` SET `POWER`=\"ON\" WHERE ID = 1
So this may work
$POWER = "";
if ($pressed == "1") {
echo "pressed\n";
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "pressed power request $row[0]\n";
}
if ($POWER == "ON") {
$sql = 'UPDATE `VolleyTest` SET `POWER`="OFF" WHERE ID = 1';
$conn->query($sql);
echo " POWER ON to OFF\n";
} else if ($POWER == "OFF") {
$sql = 'UPDATE `VolleyTest` SET `POWER`="ON" WHERE ID = 1';
$conn->query($sql);
echo " POWER OFF to ON\n";
}
}
$sql = "SELECT `POWER` FROM `VolleyTest` WHERE ID = 1";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_row($result)) {
$POWER = $row[0];
echo "$row[0]\n";
}
echo $POWER; //. " ". $pressed . " " . $_POST['key'];
User will enter like more than one payment card 1.00,cash 2.00,card 10,00,cash 20.00 etc...After these all values insert into payment_details table one by one along with current date.So after this i need to insert data to another table called moneybox table.
Count of total cash and total card will store into money box group by current date.Always two rows ie; card and cash will be there in money table,going to total of cash and card will be store based on current date.
As far as I understand the requirements, this may help...
<?php
if (isset($_POST["getamount"])) {
$getinvoiceid = $_POST['getinvoiceid'];
$getstorepaymode = $_POST['getstorepaymode'];
$getamount = $_POST['getamount'];
$sql1 = "select date from moneybox order by ID desc limit 1";
$result1 = mysqli_query($link, $sql1);
$row1 = mysqli_fetch_array($result1);
//echo json_encode($row1);
$last_moneybox_created_date = $row1['date'];
$sqlclosebalcash = "select closing_balance from moneybox where date='$last_moneybox_created_date' and type='cash'";
$resultclosebal_cash = mysqli_query($link, $sqlclosebalcash);
$rowclosebal_cash = mysqli_fetch_array($resultclosebal_cash);
//echo json_encode($row1);
//$last_moneybox_closingbalanacecash = $rowclosebal_cash['closing_balance'];
$sqlclosebalcard = "select closing_balance from moneybox where date='$last_moneybox_created_date' and type='bank'";
$resultclosebal_card = mysqli_query($link, $sqlclosebalcard);
$rowclosebal_card = mysqli_fetch_array($resultclosebal_card);
//$last_moneybox_closingbalanacecard = $rowclosebal_card['closing_balance'];
$tz = 'Asia/Dubai'; // your required location time zone.
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
$todayDate = $dt->format('Y-m-d');
if ($rowclosebal_cash['closing_balance'] == '') {
$last_moneybox_closingbalanacecash = "0.00";
} else {
$last_moneybox_closingbalanacecash = $rowclosebal_cash['closing_balance'];
}
if ($rowclosebal_card['closing_balance'] == '') {
$last_moneybox_closingbalanacecard = "0.00";
} else {
$last_moneybox_closingbalanacecard = $rowclosebal_card['closing_balance'];
}
for ($count = 0; $count < count($getamount); $count++) {
$payamt_clean = $getamount[$count];
$getstorepaymode_clean = $getstorepaymode[$count];
date_default_timezone_set('Asia/Dubai');
$created = date("y-m-d H:i:s");
$query = 'INSERT INTO payment_details (invoiceID,paymentMode,Amount,created)
VALUES ("' . $getinvoiceid . '" , "' . $getstorepaymode_clean . '", "' . $payamt_clean . '", "' . $created . '");
';
mysqli_query($link, $query);
// check whether card or cash or both rows are already there or not
$sql1 = "select * from moneybox WHERE date='$todayDate' AND type='cash' ";
$resultcash = mysqli_query($link, $sql1);
if(mysqli_num_rows($resultcash)) {
$cashMode = 1;
}
else {
$cashMode = 0;
}
$sql1 = "select * from moneybox WHERE date='$todayDate' AND type='bank' ";
$resultcard = mysqli_query($link, $sql1);
if(mysqli_num_rows($resultcard)) {
$cardMode = 1;
}
else {
$cardMode = 0;
}
$cal_closingbalancecash = $last_moneybox_closingbalanacecash - $payamt_clean;
$cal_closingbalancecard = $last_moneybox_closingbalanacecard - $payamt_clean;
switch($getstorepaymode_clean) {
case "CASH":
if($cashMode === 0) {
echo 'Different Date cash'; //insert happen based on the type
$last_moneybox_created_date = $todayDate;
$cashMode = 1;
$query = "INSERT INTO moneybox (type,inflow,date)
VALUES ('cash','$payamt_clean','$todayDate');";
}
else {
echo 'Same Date cash'; //update happen based on type and date
$query = "UPDATE moneybox SET
inflow = inflow + $payamt_clean,
closing_balance= opening_balance + inflow - outflow
WHERE type = 'cash' and date = '$todayDate';";
}
break;
case "CARD":
if($cardMode === 0) {
echo 'Different Date card'; //insert happen based on the type
$last_moneybox_created_date = $todayDate;
$cardMode = 1;
$query = "INSERT INTO moneybox (type,inflow,date)
VALUES ('bank','$payamt_clean','$todayDate');";
}
else {
echo 'Same Date card'; //update happen based on type and date
$query = "UPDATE moneybox SET
inflow = inflow + $payamt_clean,
closing_balance= opening_balance + inflow - outflow
WHERE type = 'bank' and date = '$todayDate';";
}
break;
} // end switch case
if (mysqli_query($link, $query)) {
echo 'paydetails Inserted';
}
else {
echo "Error: " . $query . "<br>" . mysqli_error($link);
}
}
}
?>
<?php
include ("db.php");
$id2 = "SELECT MAX(id) FROM osalejad;";
$id = $conn->query($id2);
if (id<= 8){
while($id<= 7) {
$min = 1;
$max = 20;
$suvanumber = rand($min, $max);
$bar = (string) $suvanumber;
$prii= "vaba2" . $bar;
$sql="INSERT INTO osalejad (name) VALUES ('$prii')";
$result = $conn->query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
}
}
else {
echo '8 osalejat on juba kirjas';
}
?>
I have a question, i want to insert data into MYSQl table, word with a random number, but problem is, it never work as it should - if i have 3 records in name column, then till, it adds 8 more records, instead of 5 records. Is something outside of scope atm?
I will use the DB in this code.
<?php
include("db.php");
$sql = "SELECT * FROM `osalejad`;";
$result = $conn->query($sql);
$var = array();
if ($result->num_rows > 0) {
// output data of each row
while ($row = mysqli_fetch_array($result)) {
$var[] = $row["name"];
}
} else {
/// echo "0 results";
}
//shuffle the array, and encode into json.
shuffle($var);
$brack=json_encode($var);
$testbrack= json_encode(array_chunk($var,2));
$final = '{';
$final .= '"teams":';
$final .= $testbrack;
$final .= ',"results":[[[[0,0],[null,null]],[[null,null],[null,null]]]]}';
//updating last tournament bracket.
$sql1 = "UPDATE lan_brackets SET json='$final' ORDER BY tid DESC LIMIT 1;";
$roundOne=$conn->query($sql1);
?>
I want to select two table values so I am using join query see below, from this code I stored one session variable like $_SESSION['emp_id'], I want select query like who are in te.emp_id='".$_SESSION['emp_id']."', From this code $count will return 0 but in my database I have one row.
$q = mysql_query("SELECT *
FROM task_employee te
JOIN task t
ON te.emp_id = t.t_assign_to
WHERE t.t_status = '0'
AND t.t_delete_on != '1'
AND te.emp_id = '" . $_SESSION['emp_id'] . "'");
$data = array();
while($r = mysql_fetch_assoc($q))
{
$data[] = $r;
}
$count = sizeof($data);
if($count > 0)
{
$return = array('status'=>'success', 'count'=>sizeof($data), 'data'=>$data);
echo json_encode($return);
}
else
{
$return=array('status'=>'not-found', 'count'=>sizeof($data), 'data'=>$data);
echo json_encode($return);
}
I am writing like this means I can get but using join query that time I can't get values
<?php
$sql = mysql_query("SELECT *
FROM task
WHERE t_delete_on !='1'
AND t_assign_to = '$emp_id'");
for ($i = 1; $row = mysql_fetch_assoc($sql); $i++)
{
$assign_to = $row['t_assign_to'];
$mysql = mysql_query("SELECT *
FROM task_employee
WHERE emp_id = '$assign_to'");
while ($r = mysql_fetch_assoc($mysql))
{
$emp_name = $r['emp_firstname']; // here i got value
}
}
?>
I am trying to create a tracker code, which is when a user enter my page, I will record and add the visitor view by 1
The problem with my code is on every monday, it not only reset the week stats, it also reset the month stats, I paste my increment code and reset code in php below.
Thanks for helping me, I try to figure it out for a few days but can't spot where I code wrongly.
This is my ads.php which I run it by do
<img src="ads.php" width="1" height="1">
at every page
This is my ads.php code
include 'conn.php';
$postID = $_GET['pid'];
$todayDate = date("m-d-y");
//Query with postDate
$sql = "SELECT * FROM wp_tracker WHERE postID LIKE '" . $postID . "'";
$result = mysql_query($sql);
$exist = 0;
while ($row = mysql_fetch_array($result)) {
$postDate = $row['postDate'];
$dayView = $row['dayView'];
$weekView = $row['weekView'];
$monthlyView = $row['monthlyView'];
$exist++;
}
if ($exist == 0) {
//create new record
$sql = "INSERT INTO wp_tracker (postID, postDate) VALUES ('$postID ','$todayDate')";
if (!mysql_query($sql, $con)) {
die('Error: ' . mysql_error());
}
}
if ($exist > 0) {
//if record exist
//check if it same day
if ($postDate == $todayDate) {
//if same day - just increment views
$dayView = $dayView + 1;
$weekView = $weekView + 1;
$monthlyView = $monthlyView + 1;
$sql2 = "UPDATE wp_tracker SET dayView = '$dayView',weekView = '$weekView',monthlyView = '$monthlyView' WHERE postID='$postID '";
$result2 = mysql_query($sql2);
}
}//end if exist
Below is my reset code (reset.php)
$todayDate = date("m-d-y");
//Query with postDate
$sql = "SELECT postDate FROM wp_tracker order by postDate ASC";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$postDate = $row[0];
$timestamp = time();
if ($postDate != $todayDate) {
//a new day -- update dayView to 0
$dayView = 1;
$sql3 = "UPDATE wp_tracker SET postDate= '" . $todayDate . "', dayView = '$dayView'";
$result3 = mysql_query($sql3);
}
if (date('D', $timestamp) === 'Mon') {
$weekView = 1;
$sql3 = "UPDATE wp_tracker SET weekView = '$weekView'";
$result3 = mysql_query($sql3);
}
if (date('d', $timestamp) === '01') {
$monthlyView = 1;
$sql3 = "UPDATE wp_tracker SET monthlyView= '$monthlyView'";
$result3 = mysql_query($sql3);
}