Increase a Value Like = +1 in MySQL and PHP

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



Leave a reply



Submit