I want to update the values of beginning and ending if the user enter an existing product. Is there something wrong in my update query? Thanks!
if(isset($_POST['status2'])){
$rowValues = explode("//", $_POST['status2']);
$orderId = $rowValues[0];
$dateReceivedNo = $rowValues[1];
$receivedDate = $_POST[$dateReceivedNo];
mysqli_query(connect(), "UPDATE `order` SET `status` = 'RECEIVED' WHERE `order_id` = '$orderId'");
$product = mysqli_query(connect(), "SELECT * FROM `order` WHERE `order_id` = '$orderId' AND branch_name = '$name' ");
$product2 = mysqli_fetch_assoc($product);
$cate = $product2['cat_name'];
$item = $product2['item_name'];
$prodName = $product2['product_name'];
$quantity = $product2['quantity'];
mysqli_query(connect(), "INSERT INTO `logs`(`log_id`, `branch_name`, `activity`, `date`) VALUES ('', '$name', 'RECEIVED ITEM ".$prodName. " ". $quantity ."', now())");
$count = mysqli_query(connect(), "SELECT count(invbranch_id) AS 'countItems' FROM `inventorybranch` WHERE `cat_name` = '$cate' AND `item_name` = '$item' AND `product_name` = '$prodName'");
$count2 = mysqli_fetch_assoc($count);
if($count2['countItems'] > 0){
mysqli_query(connect(), "UPDATE `inventorybranch` SET `beginning` = (`beginning` + '$quantity'), `date` = NOW() WHERE `cat_name` = '$cate' AND `item_name` = '$item' AND `product_name` = '$prodName' AND `status` = 'DELIVERED'");
mysqli_query(connect(), "UPDATE `inventorybranch` SET `quantity` = (`quantity` + '$quantity'), `date` = NOW() WHERE `cat_name` = '$cate' AND `item_name` = '$item' AND `product_name` = '$prodName' AND `status` = 'DELIVERED'");
}
else{
mysqli_query(connect(), "INSERT INTO `inventorybranch` VALUES ('','','$cate','','$item','','$prodName','$name','$quantity','$quantity','','','', now())");
}
mysqli_query(connect(), "UPDATE `inventoryho` SET quantity = (quantity-'$quantity') WHERE cat_name = '$cate' AND item_name = '$item' AND product_name = '$prodName'");
mysqli_query(connect(), "UPDATE `order` SET `date_receive` = now() WHERE `order_id` = '$orderId'");
}
Related
I have made a comment section to my pages which stores the comments in a mysql database. However when I click send, it sends duplicates of the same comment into the database. I can't see where I may have sent the query twice however.
<?php
if (!empty($_POST) && !empty($_POST['name']) && !empty($_POST['message'])) {
$name = strip_tags($_POST['name']);
$message = strip_tags($_POST['message']);
$name = mysqli_real_escape_string($db, $name);
$message = mysqli_real_escape_string($db, $message);
$query = "SELECT COUNT(*) AS count FROM comments";
$result = $db->query($query);
$data = $result->fetch_assoc();
$query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
$db->query($query);
$result = $db->query($query);
$query = "INSERT INTO `comment_work` (`comment_id`,`work_id`) SELECT `comments`.`id`, `comments`.`work_id` FROM `comments` WHERE `comments`.`sender_ name` = '".$name."' AND `comments`.`message` = '".$message."'";
$db->query($query);
$result = $db->query($query);
if ($result) {
echo "<p>Comment submitted!</p>";
} else {
echo "SQL Error: " . $db->error;
}
}
?>
Here
$query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
$db->query($query);//one query - **YOU must remove this line!**
$result = $db->query($query);//second query
To
$query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
$result = $db->query($query);
And here
$query = "INSERT INTO `comment_work` (`comment_id`,`work_id`) SELECT `comments`.`id`, `comments`.`work_id` FROM `comments` WHERE `comments`.`sender_ name` = '".$name."' AND `comments`.`message` = '".$message."'";
$result = $db->query($query);
Cause is simple:
Look at these code lines:
$query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
$db->query($query);
$result = $db->query($query);
$query = "INSERT INTO `comment_work` (`comment_id`,`work_id`) SELECT `comments`.`id`, `comments`.`work_id` FROM `comments` WHERE `comments`.`sender_ name` = '".$name."' AND `comments`.`message` = '".$message."'";
$db->query($query);
$result = $db->query($query);
Well, here you are executing a query with $db->query and then with $result = $db->query you executing another time the same query.
Replace this:
$query = "INSERT INTO `comment_work` (`comment_id`,`work_id`) SELECT `comments`.`id`, `comments`.`work_id` FROM `comments` WHERE `comments`.`sender_ name` = '".$name."' AND `comments`.`message` = '".$message."'";
$db->query($query);
$result = $db->query($query);
With:
$query = "INSERT INTO `comment_work` (`comment_id`,`work_id`) SELECT `comments`.`id`, `comments`.`work_id` FROM `comments` WHERE `comments`.`sender_ name` = '".$name."' AND `comments`.`message` = '".$message."'";
$result = $db->query($query);
And this:
$query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
$db->query($query);
$result = $db->query($query);
With:
$query = "INSERT INTO `comments` (`work_id`, `sender_ name`, `message`) VALUES ('".$id."','".$name."','".$message."')"; //id comes from a $_GET at the top of the page
$result = $db->query($query);
So should work.
I have an UPDATE query and using Ajax, I wanted to know if any value is empty can I only update the values that not empty in the database. I don't know if this is possible to have a if statement or something to check to skip the empty values. I know I can just add another form element but just wanted to know if there was another solution.
Only if the data is POST from front end form. If data not POST don't update this Title = '.$title .',
$id = $_POST['id'];
$title = "";
$description = $_POST['Description'];
$date = $_POST['Date'];
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = '.$title .',
`Description` = '.$description.',
`Date` = '.$date =.'
WHERE `id` = '.$id;
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());
Update: This is what worked for me. Thanks Karim Daraf
$query = " UPDATE user SET
Title = Coalesce($title,Title ) etc...
Try it with Coalesce .
$query = " UPDATE user
SET
`Title` = CASE WHEN `Title`='' or `Title` IS NULL THEN '$title' END,
`Description` = CASE WHEN `Description`='' Or `Description` IS NULL THEN '$description' END,
`Date` = CASE WHEN `Date`='' Or Date` IS NULL THEN '$date' END
WHERE `id` = '".$id."' ";
or :
$query = " UPDATE user
SET
`id` = Coalesce('$id''".$id."' , NULLIF(`id`,'')),
`Title` = Coalesce('$title''".$title."',NULLIF(`Title`,'') ) ,
`Description` = Coalesce('$description''".$description."' , NULLIF(`Description`,'') ) ,
`Date` = Coalesce('$date''".$date."',NULLIF(`Date`,''))
WHERE `id` = '$id''".$id."' ";
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = COALESCE(NULLIF("'.$title.'", ""),`Title`),
`Description` = "'.$description.'",
`Date` = "'.$date.'"
WHERE `id` = "'.$id.'"';
Not sure to understand: you have data and want to update, but only if some fied in the DB are empty?
In the case perfom only a where:
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = '.$title .',
`Description` = '.$description.',
`Date` = '.$date =.'
WHERE `id` = '.$id.' AND Title = '';
for example
I'm new to php and mySql. I'm trying to update multiple row by using php and mysql.
I'm having problem with updating multiple row in MySQL database. It's only update the last row of the table in the database. For example, user click on view product. The page will list 10 product that currently in the database. And user wants to update product information by on-click method. After finishing update, user click submit.
The problem is it only capture and update information of the last product in the table. I tried to put it in the foreach() function. But it doesnt work.
Please help. I just learned PHP and mySQL less than a week. I very much appreciate any helps.
<?php
include 'dbconn.inc.php';
include 'functions.inc.php';
$sql = "SELECT * FROM products";
$res = $mysqli->query($sql);
while( $row = $res->fetch_array(MYSQLI_ASSOC) ){
$products($row['id']) = 'id';
}
$id = $mysqli->real_escape_string( $_POST['id'] );
$weight = $mysqli->real_escape_string( $_POST['weight'] );
$name = $mysqli->real_escape_string( $_POST['name'] );
$supplier_id = $mysqli->real_escape_string( $_POST['supplier_id'] );
$price = $mysqli->real_escape_string( $_POST['price'] );
$description = $mysqli->real_escape_string( $_POST['description'] );
foreach( $products as $id){
$sql = "UPDATE products
SET
`id` = '$id',
`weight` = '$weight',
`price` = '$price',
`name` = '$name',
`supplier_id` = '$supplier_id',
`description` = '$description'
WHERE `id` = '$id'";
}
A couple of issues:
First, you're declaring the variable $id twice.
You should be using the $key not the $value in the loop
Instead, try this:
foreach( $products as $key => $value){
$sql = "UPDATE products
SET
`id` = '$id',
`weight` = '$weight',
`price` = '$price',
`name` = '$name',
`supplier_id` = '$supplier_id',
`description` = '$description'
WHERE `id` = '$key'";
}
The reason for using the array key rather than its value is because in the below line you are setting the key of the array to the values returned from the first query:
while( $row = $res->fetch_array(MYSQLI_ASSOC) ){
$products($row['id']) = 'id';
}
I might suggest instead doing this:
$products = array();
while( $row = $res->fetch_array(MYSQLI_ASSOC) ){
$products[]['id'] = $id;
}
foreach( $products as $product){
$sql = "UPDATE products
SET
`id` = '$id',
`weight` = '$weight',
`price` = '$price',
`name` = '$name',
`supplier_id` = '$supplier_id',
`description` = '$description'
WHERE `id` = '" . $product['id'] . "'";
}
can anyone help? my code doesn't seem to store the value of product id here in my code have a look I am also getting the ID from another table
<?php
include("Connection.php");
$dTime = time();
$myValue = $_REQUEST['dValue'];
echo "<p>
The time is: {$dTime}<br/>
The choice is {$myValue}
</p>
";
$sql = "Select ID from product where NAME = '$myValue'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
$pid=$row["PRODUCT_ID"];
$sql2 = "INSERT INTO `starbucks`.`order_details` (
`ID` ,
`ORDER_ID` ,
`PRODUCT_ID` ,
`QTY`
)
VALUES (
NULL , '', '$pid', '1'
)";
$result2 = mysql_query($sql2);
?>
updated the code
$id = $row["ID"]
instead of:
$id = $row;
You have an incorrect array value for $id instead of the array's ID key:
$id = $row;
// Should be
$id = $row['ID'];
in your original code there is no error handling,you should do something like this:
$sql = "Select ID from product where NAME = '$myValue'";
if ($sql) {
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
$pid = $row["PRODUCT_ID"];
$sql2 = "INSERT INTO `starbucks`.`order_details` (
`ID` ,
`ORDER_ID` ,
`PRODUCT_ID` ,
`QTY`
)
VALUES (
NULL , '', '$pid', '1'
)";
$result2 = mysql_query($sql2);
if (!$result2) {
echo mysql_error();
break;
}
} else {
echo mysql_error();
}
And see what error you get.
I am deleting a row (user) from the table Accounts and I am trying to set the values "Following Count" and "Follower Count" to their value with one subtracted. But for some reason that does not happen. The account deletes successfully, but the decrement doesn't happen.
Please can you tell me what I am doing wrong:
$query = mysql_query("SELECT * FROM `Accounts` WHERE `Username` = '$username' AND `Password` = '$password' AND `Email Address` = '$emailAdd'");
if (mysql_num_rows($query) < 1) {
exit("Account doesn't exist");
}
$row = mysql_fetch_assoc($query);
$id = $row["id"];
$query = NULL;
mysql_query("DELETE FROM `Comments` WHERE `accountID` = '$id'");
mysql_query("DELETE FROM `Likes` WHERE `accountID` = '$id'");
mysql_query("DELETE FROM `Posts` WHERE `accountID` = '$id'");
mysql_query("DELETE FROM `Accounts` WHERE `id` = '$id'");
$arg = mysql_query("SELECT * FROM Following WHERE followingUserID = '$id'");
if (mysql_num_rows($arg) >= 1) {
for ($i = 0; $i < mysql_num_rows($arg); $i++) {
$arr = mysql_fetch_assoc($arg);
$followingUserID = $arr['followingUserID'];
$followedUserID = $arr['followedUserID'];
$art = mysql_fetch_assoc(mysql_query("SELECT `Following Count` FROM Accounts WHERE `id` = '$followedUserID'"));
$followingCount = $art['Following Count'];
$followingCount = $followingCount-1;
$arts = mysql_fetch_assoc(mysql_query("SELECT `Follower Count` FROM Accounts WHERE `id` = '$followingUserID'"));
$followedCount = $arts['Followed Count'];
$followedCount = $followedCount-1;
mysql_query("UPDATE Accounts SET `Following Count` = '$followingCount' WHERE `id` = '$followingUserID'");
mysql_query("UPDATE Accounts SET `Follower Count` = '$followedCount' WHERE `id` = '$followedUserID'");
mysql_query("DELETE FROM Following WHERE followingUserID = '$id'");
}
}
exit("Closed");
Why not simply do
mysql_query("UPDATE Accounts SET `Following Count` = (`Following Count` - 1) WHERE `id` = '$followingUserID'");
mysql_query("UPDATE Accounts SET `Follower Count` = (`Following Count` - 1) WHERE `id` = '$followedUserID'");
this way you wont need the 2 selects.