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'];
Related
The first problem was with mysql_query which returns FALSE and mysql_num_rows expects parameter 1 to be resource not a boolean. I ve made this to get the error
if($itemsres === FALSE)
die(mysql_error());
Now i have this error " You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
<?php
ob_start();
session_start();
require("header.php");
require("functions.php");
echo "<h1>Your shopping cart</h1>";
showcart();
if(isset($_SESSION['SESS_ORDERNUM']) == TRUE) {
$sql = "SELECT * FROM orderitems WHERE order_id = " .
$_SESSION['SESS_ORDERNUM'] . ";";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
if($numrows >= 1) {
echo "<h2><a href='checkout-address.php'>Go to the checkout</a></h2>";
}
}
require("footer.php");
?>
The problem started from here.
if(isset($_SESSION['SESS_LOGGEDIN']))
{
$custsql = "SELECT id, status from orders WHERE customer_id = ".
$_SESSION['SESS_USERID']. " AND status < 2;";
$custres = mysql_query($custsql);
$custrow = mysql_fetch_assoc($custres);
$itemssql = "SELECT products.*, orderitems.*, orderitems.id AS itemid FROM
products, orderitems WHERE orderitems.product_id =products.id AND order_id
= " . $custrow['id'];
$itemsres = mysql_query($itemssql);
if($itemsres === FALSE)
die(mysql_error());
$itemnumrows = mysql_num_rows($itemsres);
}
mysql_num_rows expects parameter 1 instead of boolean which is given by mysql_query and after i made that check now i have this sql error.
<?php
ob_start();
session_start();
require("config.php");
if(isset($_SESSION['SESS_LOGGEDIN']) == TRUE) {
header("Location: " . $config_basedir);
}
if(isset($_POST['submit']))
{
$loginsql = "SELECT * FROM logins WHERE username = '" . $_POST['userBox'].
"' AND password = '" . sha1($_POST['passBox']) . "'";
$loginres = mysql_query($loginsql);
$numrows = mysql_num_rows($loginres);
if($numrows == 1)
{
$loginrow = mysql_fetch_assoc($loginres);
session_start("SESS_LOGGEDIN");
session_start("SESS_USERNAME");
session_start("SESS_USERID");
$_SESSION['SESS_LOGGEDIN'] = 1;
$_SESSION['SESS_USERNAME'] = $loginrow['username'];
$_SESSION['SESS_USERID'] = $loginrow['id'];
$ordersql = "SELECT id FROM orders WHERE customer_id = " .
$_SESSION['SESS_USERID'] . " AND status < 2"; $orderres =
mysql_query($ordersql); $orderrow = mysql_fetch_assoc($orderres);
session_start("SESS_ORDERNUM"); $_SESSION['SESS_ORDERNUM'] =
$orderrow['id']; header("Location: ".$config_basedir);
}
else {
header("Location: http://" .$_SERVER['HTTP_HOST']. $_SERVER['SCRIPT_NAME'] .
"?error=1");
}
}
else {
require("header.php");
?>
You have a syntax error
Use query like this
$sql = "SELECT * FROM orderitems WHERE order_id = $_SESSION['SESS_ORDERNUM']";
Or
$sql = "SELECT * FROM orderitems WHERE order_id = ".$_SESSION['SESS_ORDERNUM'];
Or
$sql = "SELECT * FROM orderitems WHERE order_id = '$_SESSION['SESS_ORDERNUM']'";
NOTE: SQL Injection.
PS: Use prepared statements
$loginrow = mysql_fetch_assoc($loginres);
session_start();
$_SESSION['SESS_LOGGEDIN'] = 1;
$_SESSION['SESS_USERNAME'] = $loginrow['username'];
$_SESSION['SESS_USERID'] = $loginrow['id'];
$ordersql = "SELECT id FROM orders WHERE customer_id = " .
$_SESSION['SESS_USERID'] . " AND status < 2";
$orderres =
mysql_query($ordersql); $orderrow = mysql_fetch_assoc($orderres);
$_SESSION['SESS_ORDERNUM'] =
$orderrow['id']; }
First of all, you should check the value of $_SESSION['SESS_ORDERNUM'].
The error means the created query($sql) is not valid, so you should debug $sql.
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.
i have this code from my source
if ($where != "")
$where = "WHERE $where";
$res = $mysqli->query("SELECT COUNT(id) FROM torrents $where LIMIT 1") or die(mysql_error());
$row = mysqli_fetch_array($res);
$count = $row[0];
i try this is this corect? :
if ($where != "")
$where = "WHERE $where";
$ress = $mysqli->query('SELECT COUNT(id) FROM torrents $where LIMIT 1';) or die(mysql_error());
$nombre ='details'.$ress;
$res = mysqli_query($ress);
$count = array();
while($count = mysqli_fetch_array($res)) $rows[] = $count;
$memcache->set($nombre, $count, 1);
Result "sorry pal not found"
if ($where != "")
$where = "WHERE $where";
$res = mysql_query("SELECT COUNT(id) FROM torrents $where LIMIT 100") or die(mysql_error());
$key = md5($res);
$get_data = $memcache->get($key);
if($get_data) {
echo 'Data Pulled From Cache';
} else {
while ($row = mysql_fetch_array($res)) {
$memcache->set($key, $res, TRUE, 3600);
$count = $row[0];
}
}
While is resolved my prolem
Trying to load $location into the sql query but just returning Invalid:
//query to Equipment
$Location= $_GET["id"];
$sql = mysqli_query($con,"SELECT * FROM `Equipment` WHERE `Location` = `".$Location."`");
$result = $sql;
if (!$result)
{
die('Invalid query: ' . mysqli_error());
}
while ($row = mysqli_fetch_array($result))
{
The $_GET['id'] is called from another page. I double checked link says id
#Dave Baker try something like below :
<?php
$Location = isset($_GET["id"]) ? $_GET["id"] : 0;
if($Location){
$sql = mysqli_query($con,"SELECT * FROM Equipment WHERE Location = $Location"); //if location type is integer else use below one
$sql = mysqli_query($con,"SELECT * FROM Equipment WHERE Location = '$Location'"); //if location type is string
$result = $sql;
if (!$result)
{
die('Invalid query: ' . mysqli_error());
}
while ($row = mysqli_fetch_array($result))
{}
}
else{
echo "id is not set";
}
I run my script ( see below ) but the place where industry & keywordrw vars are set it just doesn't do anything
The other queries where just the industry is set works fine
the other 2 that involve doing multiple LIKE queries on the keyword variable don't do anything
<?php
if (!isset($industry)) {
$industry = '';
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $keywordrw;
} elseif (!isset($keyword)) {
$industry = $_GET['industry'];
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE category LIKE '$industry' LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $industry;
} elseif (isset($keyword) && ($industry)) {
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') AND (category = '$industry') LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $keywordrw . '-' . $industry;
} else {
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
//echo $keywordrw.'-'.$industry;
}
?>
Changed a few things and updated working code is below
if(!isset($industry))
{
$industry = '';
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE title LIKE '%keywordrw%'",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} elseif(!isset($keyword)) {
$industry = $_GET['industry'];
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE category LIKE '$industry' LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} elseif(isset($keyword) &&($industry)){
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') AND (category = '$industry') LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} else {
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
//echo $keywordrw.'-'.$industry;
}