I am very frustrated with the function below. It is not updated my flag in the database. I have tried putting the ones and zeros inbetween quotes and not. I have the field set to small integer in database. Do you see what I am doing wrong? The entry is there but the 1 is not updating to 0.
function postValue(){
global $customerID;
$query="SELECT flag FROM welcomecall WHERE customerID= '$customerID'";
echo $query;
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "The customer flag is ". $row["flag"];
}
if ($row['flag']=='0'){
$query="UPDATE welcomecall SET flag='1' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
else if($row['flag']=='1'){
$query="UPDATE welcomecall SET flag='0' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
}
When the while expression exits, there'll be no $row left:
while ($row = mysql_fetch_assoc($result)) {
So this will not be true:
if ($row['flag']=='0'){
Consider moving the if statements inside the while loop.
Looks like you're using logic outside of your loop that belongs inside of your loop:
function postValue(){
global $customerID;
$query="SELECT flag FROM welcomecall WHERE customerID= '$customerID'";
echo $query;
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "The customer flag is ". $row["flag"];
// Moved
if ($row['flag']=='0'){
$query="UPDATE welcomecall SET flag='1' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
else if($row['flag']=='1'){
$query="UPDATE welcomecall SET flag='0' WHERE customerID='$customerID'";
$result = mysql_query($query) or die('Error in the query: ' . mysql_error());
}
}
}
Related
I have a list of items that is being output via PHP / MySQL. I also have an Edit button and a Delete button in one column. I am trying to figure out how to delete a list item on a specific row by clicking the Delete button. I have tried the following:
$id = $_GET['id'];
if(isset($_POST["deletelist"])) {
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '$id'";
} else {
echo "Cannot delete";
}
}
This of course does not work. Can anyone help me out with this?
UPDATE
This is the code for the entire page:
https://pastebin.com/raw/qjnZkUU2
UPDATED CODE
$id = $_GET['id'];
if(isset($_POST["deletelist"])) {
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '$id'";
mysqli_query($db, $query);
} else {
echo "Cannot delete";
}
}
What I am confused about is how does the query know which item to delete? Should I be appending the ID to the URL to pass the ID?
UPDATE
Ok I think I get it....In the delete button, I need to echo the ID of that row so when the query runs from clicking the delete button, it knows which ID to delete correct?
RESOLVED
Alright. I got it figured out!
I have a button that references the ID of the list item:
echo "
I then pass that ID to deletelist.php
if (!isset($_GET['id'])) {
echo 'No ID was given...';
exit;
}
if ($db->connect_error) {
die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
}
$sql = "DELETE FROM lists WHERE id = ?";
if (!$result = $db->prepare($sql)) {
die('Query failed: (' . $db->errno . ') ' . $db->error);
}
Item gets deleted.
you have to execute query for any action in database
mysqli_query($db, $query);
so execute a delete query and then try it again
You have to execute the query. There is no query execution code. Try this
$id = $_GET['id'];
if(isset($_POST["deletelist"])) {
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '$id'";
mysqli_query($db, $query);
} else {
echo "Cannot delete";
}
}
In order to delete a specific row what I needed to do was echo the ID within a link/button. This would then pass the ID to the needed PHP to delete the row from the database.
Echoing the row ID in the button
echo "
The PHP to delete the action row
<?php
include("db.php");
if (!isset($_GET['id'])) {
echo 'No ID was given...';
exit;
}
if ($db->connect_error) {
die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
}
$sql = "DELETE FROM lists WHERE id = ?";
if (!$result = $db->prepare($sql)) {
die('Query failed: (' . $db->errno . ') ' . $db->error);
}
if (!$result->bind_param('i', $_GET['id'])) {
die('Binding parameters failed: (' . $result->errno . ') ' . $result->error);
}
if (!$result->execute()) {
die('Execute failed: (' . $result->errno . ') ' . $result->error);
}
if ($result->affected_rows > 0) {
echo "The ID was deleted with success.";
} else {
echo "Couldn't delete the ID."; }
$result->close();
$db->close();
header('Location: ../account.php');
?>
This deletes the item row and then returns the user to account.php. Which in this case, the page never really changes.
Try like below
if(isset($_POST["deletelist"]) && isset($_GET['id']) ) {
$id = $_GET['id'];
$query = "SELECT * FROM lists";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) == 1) {
$query = "DELETE FROM lists WHERE id = '".mysqli_real_escape_string($db,$id)."'";
mysqli_query($db, $query);
} else {
echo "Cannot delete";
}
}
This time I'm trying to make some php code to work with mysqli in order to check if today date is between a range of dates in a Mysql table, if the condition is true I need to print a price from a table otherwise it shuould print a different price from another table. so I already have all sql connections set in another php file, the problem is that when I try the code, it just shows nothing, a blank page only. This is the code im using:
<?php
$currentdate = date("Y/m/d");
//basic include files
require_once('/home/user/public_html/folder/db.php');
$seasonalpricedate = mysqli_query($conn, "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1' AND $currentdate >= 'seasonal_from' AND $currentdate <= 'seasonal_to';");
$result = ($seasonalpricedate) or die(mysqli_error());
if (mysqli_num_rows($result) != 0) {
$standardprice = mysqli_query($conn, "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'");
if(! $standardprice ){
die('Could not get data: ' . mysqli_error());
}
while($standard = mysqli_fetch_array($standardprice, MYSQL_ASSOC)){
echo "$ {$standard['room_price']} ";
}
} else {
if(! $seasonalpricedate ){
die('Could not get data: ' . mysqli_error());
while($standard2 = mysqli_fetch_array($seasonalpricedate, MYSQL_ASSOC)){
echo "$ {$standard2['seasonal_price']} ";
}
}
}
?>
I already tried both codes with standardprice and seasonalprice working without a conditional, but when I try to do it like this, it does not show anything.
PostData: Im still trying to learn english, so please appologize me if I fail some words, thanks in Advance.
UPDATE: Ok so in this way it works if there is no values true its ok, it shows the standardprice, but if match the date, dont show anything, here is the code changed:
<?php
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
$currentdate = date("Y-m-d");
//basic include files
require_once('/home/trankilo/public_html/book/db.php');
$seasonalpricedate = mysqli_query($conn, "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1' AND '$currentdate' >= seasonal_from AND '$currentdate' <= seasonal_to");
$result = ($seasonalpricedate) or die(mysqli_error());
if (mysqli_num_rows($result) != 0) {
$seasonalprice = mysqli_query($conn, "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1'");
if(! $seasonalprice )
{
die('Could not get data: ' . mysqli_error());
while($standard2 = mysqli_fetch_array($seasonalprice, MYSQL_ASSOC))
{
echo "$ {$standard2['seasonal_price']} ";
}
}
} else {
$standardprice = mysqli_query($conn, "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'");
if(! $standardprice )
{
die('Could not get data: ' . mysqli_error());
}
while($standard = mysqli_fetch_array($standardprice, MYSQL_ASSOC))
{
echo "$ {$standard['room_price']} ";
}
}
mysqli_close($conn);
?>
So close to make it work, thanks to
UPDATE: Because you are also updated your OP, now i found what causes the problem. Ther proble were when you says: die('Could not get data: ' . mysqli_error()); And after that you want to try a while loop. while won't executed, because you terminated the script with a die(); Move the while to the else case of your if condition. See my comments.
Use this:
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
//Also display your errors.
display_errors(true);
$currentdate = date("Y-m-d");
//basic include files
require_once('/home/trankilo/public_html/book/db.php');
//Put your query into a variable, so you can dump / print it.
$sql = "SELECT `seasonal_price`"
. " FROM `hotel_seasonal_price`"
. " WHERE room_type_id = '1'"
. " AND '" . $currentdate . "' >= seasonal_from"
. " AND '" . $currentdate . "' <= 'seasonal_to'";
echo $sql;
//Try to run it in the sql directly. Is it gives back you any result?
//Do not need to
$result = mysqli_query($conn, $sql) or die(mysqli_error());
if (mysqli_num_rows($result) != 0) {
//Check if we have result by echoing some dummy text
echo "Yes, we have result!";
$sql = "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1'";
//Do the same as the previous query. Does it gives you back anything?
$seasonalprice = mysqli_query($conn, $sql);
if (!$seasonalprice) {
//I do not really get what happens here. If you have no seasonalprice,
//then you can not fetch_array on that!
//Move this whole section.... You've say die, and after that do a while?
die('Could not get data: ' . mysqli_error());
} else {
while ($standard2 = mysqli_fetch_assoc($seasonalprice)) {
echo "$ " . $standard2['seasonal_price'] . " ";
}
}
} else {
//Same as previous
$sql = "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'";
$standardprice = mysqli_query($conn, $sql);
if (!$standardprice) {
die('Could not get data: ' . mysqli_error());
//same here, move the while to the else...
} else {
while ($standard = mysqli_fetch_array($standardprice, MYSQL_ASSOC)) {
echo "$ {$standard['room_price']} ";
}
}
}
mysqli_close($conn);
I'm trying to execute a simple mySQL query in a php page, and I keep getting this error :
"Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in..."
even though the query returns a result in mysql workbench.
This is my code:
<?php
$con=mysqli_connect("localhost","root","","eshkol");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql1="SET Names utf8";
$sql = mysql_query("SELECT * FROM student WHERE idStudent=2");
$r = mysql_fetch_array($sql);
echo $r["idStudent"];
if (!mysqli_query($con,$sql1))
{
die('Error hebrew: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "success";
mysqli_close($con);
?>
What am I doing wrong here?
You're mixing mysql_* and mysqli_* functions.
$sql = mysql_query($con, "SELECT * FROM student WHERE idStudent=2");
$r = mysql_fetch_array($sql);
should be
$sql = mysqli_query($con, "SELECT * FROM student WHERE idStudent=2");
$r = mysqli_fetch_array($sql);
What's interesting is you're using them just below that code:
if (!mysqli_query($con,$sql1))
{
die('Error hebrew: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
You probably want to combine the two to clean up your code:
$sql = mysql_query($con, "SELECT * FROM student WHERE idStudent=2");
if (!$sql) {
die('Error: ' . mysqli_error($con));
}
$r = mysql_fetch_array($sql);
I am working on recieveing an item from another application with the use of $_POST and I am trying to see if that item already exists in the database. If it does, then $count increases by one. If it does not exist in the database, then it will added in with the use of INSERT INTO.
Here is my code:
<?php
date_default_timezone_set('Asia/Manila');
$today = date('m-d-Y');
echo $today;
$con= mysqli_connect("******","******","******")
or die ('Error: ' . mysql_error());
mysqli_select_db($con,"a3656574_opacmin");
$sql= "SELECT keyWord FROM searchedWords";
$result= mysqli_query($con,$sql);
if($result==$_POST[keyWord])
{
$upD="UPDATE searchedWords SET countr = countr + 1";
while (!mysqli_query($con,$upD))
{
die('Error: ' . mysqli_error($con));
}
}
else
{
$insertIn="INSERT INTO `searchedWords`( `keyWord`, `countr`) values ('$_POST[keyWord]',1)";
while (!mysqli_query($con,$insertIn))
{
die('Error: ' . mysqli_error($con));
}
}
?>
I don't know what's wrong. No items are sent to the database at all. Does anyone know how to fix it?
Change your code like this...
$result= mysqli_query($con,"SELECT keyWord FROM searchedWords");
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if($row['keyWord']==$_POST[keyWord])
{
$upD="UPDATE searchedWords SET countr = countr + 1";
while (!mysqli_query($con,$upD))
{
die('Error: ' . mysqli_error($con));
}
}
$result==$_POST['keyWord'] won't work becase $result is object there so...
After this line
$result= mysqli_query($con,$sql);
You have to fetch the data
$keyword = '';
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$keyword = $row["keyWord"];
}
$sqlQuery = "SELECT * FROM allowedUsers WHERE UserID = '" . $kUserID . "'";
$result=mysql_query($sqlQuery, $db);
if(!result)
{
echo "Error running query <br>" . mysql_error();
exit;
}
while($row = mysql_fetch_array($result))
{
echo $row[2];
}
I run the SQLQuery in phpMyAdmin and I am getting a valid result (1 row)
the table (allowedUsers) has 6 fields
I can't get anything out of the DB.
Any help is appreciated.
if(!result) should be if(!$result)
According to PHP.net's documentation, you don't need to pass $db to mysql_query(). Take a look at the example code:
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}
mysql_free_result($result);
?>
It may be helpful to see your connection code, ensure you've selected a database, etc.