This part of PHP and SQL is totally new to me where I need to use maths to update a table.
I currently have a table avis_credits with one row only in this table
The table deducts credits as the user uses the system from qty_credits, but i want to be able to add additional credits to his account as per example:
Client has currently 10 credits left he purchases 100 credits so the system must take the current credits and add the 100 to it giving me the 110 credits. I am doing this currently manual adding it my side and updating the table with this expresion.
"UPDATE avis_credits SET credits='$qty_credit'";
What I would like is that the system looks up the current credits and adds the new credits to the row.
I have tried this
"UPDATE avis_credits SET credits='<?php echo qty_credit?> + $qty_credit'";
But it is not working at all. Is it because I am trying to do too many functions in the one string? Or is it because The maths equation is wrong as I have no idea how to write it?
On your first query you're not doing any math you are purely changing it to the value of your variable:
"UPDATE avis_credits SET credits='$qty_credit'";
So credits becomes the value of $qty_credit.
On your second query <?php echo qty_credit?> this is wrong, you're trying to open another php tag on an already open one and you don't use the $ on the variable and then you try to sum the variable so it gives you an error.
"UPDATE avis_credits SET credits='<?php echo qty_credit?> + $qty_credit'";
This will sanitize your input and sum the value of credits with the addition of value of your variable $qty_credit:
$sql = sprintf("UPDATE avis_credits SET credits = credits + '%s'",
mysql_real_escape_string($qty_credit));
As a side note, you may want to specify an ID of the account you want to deposit the additional credit otherwise the above rule will increase that credit for ALL rows.
$sql = sprintf("UPDATE avis_credits SET credits = credits + '%s' WHERE id = '%s'",
mysql_real_escape_string($qty_credit),
mysql_real_escape_string($id));
SQL support arthmetic operations.
So you can do the following:
"UPDATE avis_credits SET credits=credits + $qty_credit";
Related
I've got a database with question numbers between 1 and 21 all with answers / values etc.
But I want to be able to delete for example question 7.
And if I delete that question that is between other numbers that the question numbers will automatically change from 1-21 to 1-20 instead of 1-6, 8-21.
Is this possible? I tried searching on the web but I couldn't find what I was looking for.
With kind regards,
How do I make this shorter? I know it can be done with innerjoin but I dont know how to correctly use it because the explanations are unclear to me..
`$sql = "DELETE FROM insertquestion
WHERE nummer='".$questionNumber."'";
$deleteFromQuestion = $db->prepare($sql);
$deleteFromQuestion->execute();
$updateSql = "UPDATE insertquestion SET nummer = nummer - 1 WHERE nummer >= '".$questionNumber."'";
$updateSql = $db->prepare($updateSql);
$updateSql->execute();`
and that another time but then with another table (vraag)
You can use Triggers to do this operation ref:
http://www.mysqltutorial.org/mysql-triggers.aspx
or use a update query to set the values (however this method is costly, if tthe number of rows is great in number)
Update table set q_no=q_no-1 where q_no> 5 //(if you delete a question with number 5)
There may be many ways for this requirement. One of them is you can achieve it by trigger. Write an after delete trigger which will update all question numbers of the table.
CREATE TRIGGER tblquestion_delete AFTER DELETE on tblquestion
FOR EACH ROW
BEGIN
UPDATE tblquestion
SET qno = qno - 1
WHERE qno > old.qno;
END
Another way is simply you can write this query immediately after delete query.
$sql = "DELETE FROM insertquestion WHERE number='".$questionNumber."' ;
UPDATE tblquestion SET number = number - 1 WHERE number > '".$questionNumber."' ; ";
I am creating a job number system that a few users will be using at the same time. I have created a job number on the php page and then it saves the number to the job sheet and uses this to link other tables to the job.
I take the job number from a table called numbers which then should increment the number by 1 each time the job is submitted ready to create the next job.
But the numbers are not working correctly.
As an example I get 1,2,3,4,8, then 43,44,45,then 105
I cant see why they would jump so much
$job_number_query = "SELECT * FROM numbers";
$job_result =($mysqli-> query($job_number_query)) ;
$job_num = mysqli_fetch_assoc($job_result);
$increment_job_number = $job_num[job_number];
$update_job_number_query = "UPDATE numbers SET job_number = $increment_job_number +1 ";
$mysqli-> query($update_job_number_query);
//echo ($customer_id);
Then I simply insert the $increment_job_number into the jobsheet table.
I am using int for the Job_number field in the table numbers
I cant think of a way to test the numbers. I guess a way is to look through the jobsheets and add another number to there but because more than one user might have a job that hasn't been submitted yet would this also cause problems.
Just increase the value without the first SELECT:
UPDATE numbers SET job_number = job_number +1
You have no where clause on your update query, so you're incrementing the job_number field in ALL records in the table.
It was me that was the technical failure in the end. I have got the incremental numbers on the create page but then unfortunately I had also got the incremental number on the edit pages so every time I edited the pages I then added 1 to the number field in the numbers table.
I have made an online Police Dispatching program and I am trying to add a new feature.
I want to add a button to set all Police Officer's status to 'On Duty'.
This is my current action for the form/submit button:
<?php
mysqli_query($con,"UPDATE users SET status='0' WHERE code='$code'");
mysqli_close($con);
printf("<script>location.href='../units.php'</script>");
?>
The 'On Duty' is the same as 0, busy is 1, and unavailable is 2. The code is basically the Police Department's unique code that all Officers have.
Right now, this is affecting any rows! Please help!
If you want ALL to be set to Zero, as you say, just Drop your WHERE statement.
"UPDATE users SET status='0'";
You should drop the Single Quotes as well, but it's not hurting anything.
"UPDATE users SET status=0";
Database has this security that don't allow users to update all data when you are using a UPDATE statement without WHERE clause, you can do a hacky query like this
UPDATE users SET status = 0 WHERE code != 'any value'; // if code is string
UPDATE users SET status = 0 WHERE code <> 0; // if integer, just makes sure 0 is not a code in your DB
all rows that didn't match in the value will be updated, in this case, all rows will be updated
In our cart process we don't want to remove items from the store inventory until after the payment gateway has returned a approved code for the credit card purchase. Thus assuming the payment was approved we need the cart items to deduct from the inventory.
In a mysqli UPDATE query how if possible can i write it so that the new value being set is the result of the rows current value - the quantity stored in the carts Item_qty field?
mysqli_query($con,"UPDATE table1 SET Qty='`**current-value**` - $proc[Item_Qty]' WHERE buyer='$user'");
I do understand that I can accomplish this using multiple queries, perform some math and then write the value back to the database, but I am hoping there is a cleaner way with less code by doing all in a single mysqli query.
You can use current values of the table in an update.
mysqli_query($con,"UPDATE table1 SET Qty = Qty - $proc[Item_Qty] WHERE buyer='$user'");
Easy. You can use the field itself in the calculation:
UPDATE table1 SEt Qty=Qty - $proc[Item_Qty]
^^^--- this is 100% perfectly valid
This should do it:
$query = "UPDATE table1 SET Qty = Qty - $proc[Item_Qty] WHERE buyer='$user'";
You are simply, using the field name again to get its current value.
Using PHP's msqli is it possible to update two records with one query?
First I do a SELECT to check that $pay_user has enough game currency in his account, if he does then I do the following...
My update query is:
"UPDATE account SET money= money - ".$money." WHERE User_id=".$pay_user
"UPDATE account SET money= money + ".$money." WHERE User_id=".$recieve_user
It is transactional to stop dirty read's.
I was hoping to save on a query and call it as one... is it possible?
Since the two where clauses are not the same, you cannot combine these queries into one statement.
You can mark it as a TRANSACTION so they both execute at the same time.
refer to http://dev.mysql.com/doc/refman/5.0/en/update.html
code sample
update account a1, account a2
set a1.money = a1.money - $money , a2.money = a2.money + $money
where a1.user_id = $pay_user and a2.user_id = $recv_user
I recommend using a stored procedure to do this. it will be one call from php, and if it fails in the middle the SP should issue a rollback.