MySQL rows are not being updated upon deletion - php

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.

Related

Updating beginning and ending values in mysql

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'");
}

If value from database on user is 0 then execute php code

Hi guys i'm quite new to PHP but I'm currently working on this site and I want it to check if the total_online_time for the user is 0 or less then it should execute the script which will create a room for the user. However after trying with this script the .php page won't load.
http://pastebin.com/XDLhFvtE
<?php
$getStats = mysql_query("SELECT total_online_time FROM `users` WHERE id = '".$_SESSION['user']['id']."'");
if ($getStats > 0)
$getRoomKit = mysql_fetch_assoc(mysql_query("SELECT * FROM room_starterkit ORDER BY RAND() LIMIT 1"));
$getRoom = mysql_fetch_assoc(mysql_query("SELECT * FROM `rooms` WHERE id = '".$randomRoomID['id']."' LIMIT 1"));
$welcomeMessage = "Welcome " . $_SESSION['user']['username'];
mysql_query("INSERT INTO `rooms` (caption,owner_id,description,model_name,wallpaper,floor) VALUES ('".$welcomeMessage."', '".$_SESSION['user']['id']."', 'Welcome to Hablore', '".$getRoomKit['room_Model']."', '".$getRoomKit['room_Wallpaper']."', '".$getRoomKit['room_Floor']."') ");
$theRoomID = mysql_fetch_assoc(mysql_query("SELECT id FROM `rooms` ORDER BY `id` DESC LIMIT 1"));
mysql_query("UPDATE `users` SET `home_room` = '".$theRoomID['id']."' WHERE `id` = '".$_SESSION['user']['id']."'");
$getRoomItems = mysql_query("SELECT * FROM room_itemkits WHERE roomKit = '".$getRoomKit['id']."'");
while($CurItem = mysql_fetch_assoc($getItems)){
mysql_query("INSERT INTO `items` (user_id, room_id, base_item, extra_data, x, y, z, rot, wall_pos) VALUES ('".$_SESSION['user']['id']."','".$lastRoomID['id']."','".$CurItem['base_item']."','".$CurItem['extra_data']."','".$CurItem['x']."','".$CurItem['y']."','".$CurItem['z']."','".$CurItem['rot']."','".$CurItem['wall_pos']."')");
}
}
?>

Adding session in while (mysql)

if(isset($_SESSION['username']))
{
$con = mysql_connect('localhost','root','');
mysql_select_db('chatbox',$con);
$result = mysql_query("SELECT id FROM users");
mysql_query("UPDATE users SET `online` = 0 WHERE `username` = ".$username."");
}
Can you make the username is session (that is logged in) to be equal to
mysql_query("UPDATE users SET `online` = 0 WHERE `username` = "to_put_it_here");
Simple and cute trick
$username = $_SESSION['username'];
if(isset($username))
{
$con = mysql_connect('localhost','root','');
mysql_select_db('chatbox',$con);
$result = mysql_query("SELECT id FROM users");
mysql_query("UPDATE users SET `online` = 0 WHERE `username` = '$username' ");
}
If I was you, I would be using PDO. Although I think this would help you
mysql_query("UPDATE users SET `online` = 0 WHERE `username` = $_SESSION['username']);

SQL update row if key exists else update a default row

Here is what I am trying to do (in pseudo code)
if (dB entry exists) {
UPDATE the dB entry
}else{
UPDATE a default dB entry
}
This is not a "INSERT INTO ... ON DUPLICATE" question.
I'm hoping there is some kind of UPDATE ... ON DUPLICATE type of slick code to do this in one line.
My code creates $userName from the $_GET['U'] request. If $userName exists in the database (it's a unique key), then increment a counter in the database. Else, increment a counter for a default entry.
Here is my current code to update the counter:
$userName = $_GET['U'];
$sql = "UPDATE `stats` SET `count`= `count` + 1 WHERE `userName` = '" . $userName . "'";
mysqli_query($conn,$sql);
And if this particular username doesn't exist, I want this to happen:
$sql = "UPDATE `stats` SET `count`= `count` + 1 WHERE `userName` = 'default'";
mysqli_query($conn,$sql);
Are you looking for this,
$userName = $_GET['U'];
$query_uname_exists = "SELECT count(*) FROM stats WHERE `userName` = '" . $userName . "'";
$uname_count = mysqli_query($conn,$query_uname_exists);
if($uname_count > 0){
#update
$sql = "UPDATE `stats` SET `count`= `count` + 1 WHERE `userName` = '" . $userName . "'";
mysqli_query($conn,$sql);
}
else{
#update default
$sql = "UPDATE `stats` SET `count`= `count` + 1 WHERE `userName` = 'default'";
mysqli_query($conn,$sql);
}
Use the following query
$sql = "Update stats SET count = count + 1 where username = (case when username= ". $userName ". then username else 'default' end)";
This is the most I can shrink it for you:
$userName = mysqli_query($conn, "SELECT count(*) FROM stats WHERE `userName` = '".$_GET['U']."' LIMIT 1") == 0 ? 'default' : $_GET['U'];
mysqli_query($conn, "UPDATE `stats` SET `count`= `count` + 1 WHERE `userName` = '".$userName."' LIMIT 1");

MySQL not decrementing values

I can't get the values in the table Accounts to decrement when I try to delete a post. The values I would like to decrement are "PostCount", "Likes Count" and "CommentsCount". Currently only the "PostCount" works.
Please can you tell me what I am doing wrong:
$arg = mysql_query("SELECT `numberOfLikes` FROM Posts WHERE `id` = '$postID'") or die(mysql_error());
$query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'");
while ($row = mysql_fetch_assoc($query)) {
$b = $row['accountID'];
mysql_query("UPDATE Accounts SET `numberOfLikes` = (`numberOfLikes` - 1) WHERE `id` = '$b'");
}
$arg = mysql_query("SELECT `numberOfComments` FROM Posts WHERE `id` = '$postID'");
$query = mysql_query("SELECT * FROM Posts WHERE `id` = '$postID'");
while($row = mysql_fetch_assoc($arg)) {
$b = $row['accountID'];
mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` = '$b'");
}
$arg = mysql_query("SELECT `PostCount` FROM Accounts WHERE `id` = '$accountID'");
while ($row = mysql_fetch_assoc($arg)) {
mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'");
}
mysql_query("DELETE FROM Likes WHERE `postID` = '$postID'");
mysql_query("DELETE FROM Comments WHERE `postID` = '$postID'");
mysql_query("DELETE FROM Posts WHERE `id` = '$postID' AND `accountID` = '$accountID'") or die(mysql_error());
exit("Deleted post");
First up looping queries kills your execution time. You want to limit it to as little queries as possible, this is where implode comes into play:
// what is this for? I do not see it being used. $arg = mysql_query("SELECT `numberOfLikes` FROM Posts WHERE `id` = '$postID'") or die(mysql_error());
$query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'");
while ($row = mysql_fetch_assoc($query)) {
$b[] = $row['accountID'];
}
mysql_query("UPDATE Accounts SET `numberOfLikes` = (`numberOfLikes` - 1) WHERE `id` IN(" . implode(',', $b) . ")") or trigger_error('MySQL Update Failed: ' . mysql_error());
This will be much more efficient and have to same effect.
The next query, should be similar:
$arg = mysql_query("SELECT `numberOfComments` FROM Posts WHERE `id` = '$postID'");
// again an extra unnecessary query not being used. $query = mysql_query("SELECT * FROM Posts WHERE `id` = '$postID'");
$b=array();
while($row = mysql_fetch_assoc($arg)) {
$b[] = $row['accountID'];
}
mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` IN(".implode(',', $b) . ")") or trigger_error(mysql_error());
This next one, I do not even know why you are looping through it:
//$arg = mysql_query("SELECT `PostCount` FROM Accounts WHERE `id` = '$accountID'");
//while ($row = mysql_fetch_assoc($arg)) {
mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'") or trigger_error(mysql_error());
//}
As you are not utilizing that data anywhere, just run the update query.
Implementing the above should speed up your application and reduce redundancy. implode is very handy and running 1 query vs several is almost always preferred. I am not sure if that will solve your problem, but it is a huge step in the right direction in fixing your code.
I re-coded my if statement containing the code I posted and it is now working perfectly:
mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'");
$query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'") or die(mysql_error());
while($row = mysql_fetch_assoc($query)) {
$accID = $row['accountID'];
mysql_query("UPDATE Accounts SET `Likes Count` = (`Likes Count` - 1) WHERE `id` = '$accID'");
}
$query = mysql_query("SELECT * FROM Comments WHERE `postID` = '$postID'");
while($row = mysql_fetch_assoc($query)) {
$accID = $row['accountID'];
mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` = '$accID'");
}
mysql_query("DELETE FROM Likes WHERE `postID` = '$postID'");
mysql_query("DELETE FROM Comments WHERE `postID` = '$postID'");
mysql_query("DELETE FROM Posts WHERE `id` = '$postID' AND `accountID` = '$accountID'") or die(mysql_error());
exit("Deleted post");

Categories