I want to add the rows that is being query in database. There is an amount paid, previous balance, new balance.. I want the previous balance to be added by paid amount.
Num|pay|old|new
1 |100|500|600
2 |120|600|720
3 |200|720|920
4 |300|720|920
5 |350|720|920
6 |500|720|920
The database query has data of amountPaid (pay), previous balance (old), new balance (new), but the data in column(new) is wrong.I want to correct it in forloop.Is there a way to help me in this? to get the column(new) value is
column(pay)100 + column(old)500 = column(new)600.
the column(new) in number two will be put in column(old)600 then added by column(pay)120 is equal to column(new)720 and so on.
Hope you can help me in this...
I suppose you are adding the amounts when user submits it
1) Use mysql UPDATE clause in order to change the value in the coloumns
2) use while loop to get the previous value selecting it with a specific id which I suppose you have in your table
3) add it with a new value and update the new amount coloumn
PHP
<?php
$id=$_POST['id'];
$amount=$_POST['amount'];
include("connection.php");
$sql="SELECT * FROM `tablename` WHERE `user_id` ='$id'";
$res=mysql_query($sql);
$row=mysql_fetch_array($res){
$t2 = $row['previous_amount'];
$t3=$t2+$amount;
$sql="UPDATE `bank`.`tablename` SET `new_amount' = '$t3'";
}
?>
Related
column name = receipts (txtbox)
if user enter receipts = 5 then insert 5 rows in database.
data insert in table like this..
receipts
1
2
3
4
5
if user enter again receipts = 3 then again insert 3 rows in database.
6
7
8
like wise....
plz suggest me how to do this....
below is my code to save in php pdo...
i tried below code this is working but not inserting no in sequential order....
if(isset($_POST['save']))
{
$book_no = $_POST['book_no'];
$receipt= $_POST['receipt'];
for($row=1;$row<=$receipts ;$row++)
{
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$receipt));
}
}
First you have to change the following code:
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$receipt));
like this (the receipt number is the $row var, not the $receipt var)
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$row));
If you want that, the next time, it start from the last number inserted (that is, for example, start from 6, if the previous time you have inserted 5 receipts), you have to query the DB to get the the current max value, and then start from it. To get the max, a query like this should work:
SELECT MAX(receipt) FROM scheme_master where book_no = :book_no
I almost finished a site which stores and lets admins manipulate user information.
The last function I implemented was the ability to modify rankings: every user has a rank, and you can use a menu to manually adjust it by moving users up or down.
It works perfectly: I can modify it, the database stores the new rankings correctly; I can add a new user and it becomes the lowest ranked one. The problem is when I try to delete a user.
I wrote a PHP script, which should do the following:
Receive the user's ID
Remove the user's data from two table by using a WHERE statement
Remove the user's uploaded file from the server
Update the other users' rank, so if I deleted the 3rd user, the previously 4th becomes the 3rd, the 5th will be the new 4th and so on. This last part is where my code doesn't work.
Here is the whole PHP:
<?php
// Connecting to the server.
$con=mysqli_connect("connection data");
$rangcounter = 1;
//deleting the user's data - works: the user's data is deleted from both tables
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$id = $_POST['playerid'];
mysqli_query($con,"DELETE FROM Player WHERE ID=$id");
mysqli_query($con,"DELETE FROM Troops WHERE ID=$id");
}
//deleting the user's image from the server - works: the user's file is deleted
$directory = 'uploads/';
unlink($directory . $id . "picture.jpg");
//updating the database - does not work: the other users' ranks stay the same as before
$result = mysqli_query($con,"SELECT * FROM Player ORDER BY rank ASC");
while($row = mysqli_fetch_array($result)) {
$updateid = $row['ID'];
mysqli_query($con,"UPDATE Player SET 'rank' = $rangcounter WHERE ID='$updateid'");
$rangcounter++; //this variable should always be correctly high rank for any given loop. This way I can remove the highest ranked user (1st), then set every other one's rank to one less: the previously 2nd becomes the new 1st (when `$rangcounter` is 1); the previous 3rd will be the new 2nd (when `$rangcounter` is 2), and so on for every row.
}
header("redirect to main page");
exit();
?>
My idea was to create a new variable, which starts at 1, then, with every UPDATE I increment it. Because $result is ordered by the rank, it shouldn't be a problem, right? But it does not work, and I'm fairly sure it's because of a simple reason, but I just can't put my finger on it.
Could any of you help?
Update: TJ- solved it: MySQL update in a PHP While loop
Add quotes '' around $rangcounter in your mysqli_query.
mysqli_query($con,"UPDATE Player SET 'rank' = '$rangcounter' WHERE ID='$updateid'");
Hope this helps you
Change to
mysqli_query($con,"UPDATE Player SET rank = $rangcounter WHERE ID='$updateid'");
from
mysqli_query($con,"UPDATE Player SET 'rank' = $rangcounter WHERE ID='$updateid'");
From your explanation I understood that you could have
userId | rank
1 1
2 2
3 5
4 6
5 3
6 4
and after deleting user with with id=3 you want to have:
userId | rank
1 1
2 2
4 5
5 3
6 4
If the above is correct then all what you need is to excute after deleting the user
UPDATE Player SET rank = rank-1 WHERE rank > $theDeletedsRank
should not use single quotation '' for column names. Also always use php variables outside of query string like below.
Please try this.
mysqli_query($con,"UPDATE Player SET rank=".$rangcounter." WHERE ID=".$updateid."");
Hope this works fine.
ok so I have a table called voting with 3 columns (idnum, name, numvotes). I created radio buttons in an array based on the number of rows in this table, all with the same name (name="preference") I need to update the numvotes field ( add 1) depending on the radio button selected. Cant seem to get this working with the following code...
<?php
$uquery = "update voting set numvotes='". ($_POST['preference'] + 1) ."' where idnum=" .$_POST['idnum'];
$uresults = mysql_query( $uquery);
?>
You are adding 1 to the value returned for $_POST['preference'], not +1 on the value in the database. You also are setting the value as a string, not a number due to the single quotes.
Assuming $_POST['preference'] is the current vote count:
$uquery = "update voting set numvotes=". ($_POST['preference'] + 1) ." where idnum=".$_POST['idnum'];
This question is bit complex atleast for me. Well, I am working on a project and need some bit more help..
Actually i have a module , where when 1 user login he get limited data from 1 table and he update its records. That is done !
second thing, the issue : i have 5 user who will login and will work on data.
but no user should get same data, like we have 1000 records.
Then when 1st user login: he get 10 records from 1 - 10.
2nd user login : he get next 10 ; 11- 20.
same so on..
and when next day they login ; they get records from 51.
because 5 user last day worked on first 50 data records.
My issue is how to achieve that 2 goals ?
do i need a framework for this ?
or it can be done using simple php n sql ?
any support will be helpful for me. :)
Ok. This is just a raw answer to give you a better idea. This is how you will insert the login .
Consider having a table containing following fields,
Table Name: Temp_Table
user, assigned_rows_last_no, date_assigned
<?php
$con=mysqli_connect("example.com","hiren","abc123","my_db");
// These things can be included into a single file say config.php and including in every file so that you dont need to specify connection string everytime.
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//This query required to be included into the file, which is exactly after login.php
$sql = mysqli_query($con,"SELECT assigned_rows_last_no FROM Temp_Table ORDER BY assigned_rows_last_no DESC LIMIT 1");
// This is because I want the last row number assigned to the user.
IF ($sql == 0) // Check whether the return answer is NULL or not.
{
// If the result is empty than add new entry of the user with defined row number.
// Suppose that current username can be retrieved from SESSION and stored into a variable.
$insertquery = mysqli_query($con, "INSERT INTO Temp_Table Values ('" . $username . $"', 10, CURDATE()");
mysqli_close($con);
}
else
{
// Select the last entry of row and add 10 to it. Ex. User2 has been assigned to 11-20, table contains 20 in the row of user2, now user3 comes, this query will select the row_no, add 10 and insert again into the table (i.e. value 30 which means 21-30.)
settype($sql, "int");
$sql = $sql + 10;
$insertquery = mysqli_query($con, "INSERT INTO Temp_Table Values ('" . $username . $"', '" . $sql . "', CURDATE()");
mysqli_close($con);
}
mysqli_close($con);
?>
The field Date will help you to recognize the entries of today, so that you can set your logic for "There should be no duplicate entries for the same user on same day"
Now, Make you own page, which check the above mentioned things, and assign the rows to the users.
Note: This code will only be able to clear out the logic for you, I am not sure whether it will work in your code without any changes.
You don't need a extra framework. Simply done with php 'n' sql!
Why you don't save the last edited lines (linenumbers) in a extra SQL-Table? Maybe with the username / userid.
iam having a table with columns like
id || counter
if i do something (some event) i want the counter's value(at a particular id) to increase by one , currently iam doing this :
//get current value
current_value = select counter from myTable where id='someValue'
// increase value
current_value++
//update table with current value
update myTable set counter=current_value where id='someValue';
currently iam running 2 queries for this, please suggest me some way do it in one step.
Just run the math in the database:
update myTable set counter = counter + 1 where id = 'someValue';