Increment value in MySQL update query
Simply increment the value that already exists in the database
$sql = "UPDATE member_profile SET points = points + 1 WHERE user_id = ?";
$db->prepare($sql)->execute([$userid]);
This code would work for both PDO and mysqli in the modern PHP versions
Increase and decrease row value by 1 in MySQL
Two queries to increase/decrease field value are not necessary:
mysql_query("UPDATE table SET field = field + 1 WHERE id = $number");
is a perfectly valid query as you can see next:
mysql> describe points;
+--------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| uid | int(11) | NO | PRI | NULL | |
| points | int(11) | YES | | 0 | |
+--------+---------+------+-----+---------+-------+
2 rows in set (0.05 sec)
mysql> insert into points VALUES (1,0),(2,0);
Query OK, 2 rows affected (0.14 sec)
mysql> select * from points;
+-----+--------+
| uid | points |
+-----+--------+
| 1 | 0 |
| 2 | 0 |
+-----+--------+
2 rows in set (0.05 sec)
mysql> update points set points = points+1 where uid = 1;
Query OK, 1 row affected (0.27 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from points;
+-----+--------+
| uid | points |
+-----+--------+
| 1 | 1 |
| 2 | 0 |
+-----+--------+
2 rows in set (0.00 sec)
Having that tested, are you sure you get into your if (loggedin())
clause?
I have to agree with KM
, would be nice to see output of echo $query1;
or echo $query2;
How to increment value in MySQL with PHP mysqli
This can be done very simply, just execute a query like this
$sql = "UPDATE tablename SET col1=col1+1 WHERE key=99";
Or any value you like
$sql = "UPDATE tablename SET col1=col1+3 WHERE key=99";
Increase one by one php variable in sql query
You can post with a form and increment the id
<?php
if (isset($_GET["increment"]) && $_GET["increment"] == 1)
$n++;
$sql = "Select * from mytable where id=$n;";
// display data as usual
?>
<form>
<input type="hidden" name="increment" value="1"/>
<button type="submit">increment</button>
</form>
How to increment MySQL field by one if one or more items are checked in form?
I think you should use whereIn
instead of where
$test = Platform::whereIn('name', $value)->get();
Update:
You can do this by DB::raw():
DB::table('Platforms')
->whereIn('name', $value)
->update([
'count' => DB::raw('count + 1')
]);
Incrementing a string by one in PHP
As @axiac mentions this is probably not a good idea but it's pretty easy to manage.
$memberid = 'ABC000001';
list($mem_prefix,$mem_num) = sscanf($memberid,"%[A-Za-z]%[0-9]");
echo $mem_prefix . str_pad($mem_num + 1,6,'0',STR_PAD_LEFT);
Split your current member number into the alpha and numeric parts then put them back together bumping the number when you do it. I use this as a function and pass the previous ID and what I get back is the next ID in the sequence.
How do I increment a value with mysql update query (php)
UPDATE tbl SET amt_field = amt_field + 1 WHERE ...
If you use the single quotes '
, you're telling the enclosed value to be interpreted as a string You were probably thinking about the tick marks. This is also valid:
UPDATE tbl SET `amt_field` = `amt_field` + 1 WHERE ...
This must be used when the column (or table etc.) has a reserved name.
Related Topics
How to Resolve a Http 414 "Request Uri Too Long" Error
Detect a Quotation in a String
How to Do File Upload Using Jquery Serialization
Sql Get an Id from a Column Where Ids Separated by Commas
Laravel Unique Validation on Multiple Columns
Patch and Put Request Does Not Working With Form-Data
Php: How to Detect If a Session Has Expired Automatically
How to Stop My PHP Form Resubmitting Input After Page Refresh
How to Flatten a Multidimensional Array
What Does the PHP Error Message "Notice: Use of Undefined Constant" Mean
How to Count Columns With the Same Value in a Specific Row in MySQL
Laravel How to Return Single Column Value Using Query Builder
Regex for Only Allowing Letters, Numbers, Space, Commas, Periods
How to Compare Comma Separated Ids on Left Join
Codeigniter Session Is Not Working on PHP 7
How to Update Stock Quantity Using MySQL in PHP
Dynamically Display a CSV File as an HTML Table on a Web Page