26

how do I add a +1 too the c_request field. every time I do and insert I want to add a 1 to the current number (ex. like a hit counter)

mysql_query("INSERT INTO ed_names (com_id, c_date, c_time, c_type, c_request, c_by)
        VALUES ($id, CURRENT_DATE, CURRENT_TIME, '.($type == 'normal' ? 1 : 2).',0,$user)");     

$rid = mysql_insert_id();
acctman
  • 4,229
  • 30
  • 98
  • 142

2 Answers2

62
mysql_query("UPDATE ed_names SET c_request = c_request+1 WHERE id = 'x'");
tbleckert
  • 3,763
  • 4
  • 32
  • 39
2

use update if you want to add to an existing, if not, just enter 1

INSERT INTO ed_names (com_id, c_date, c_time, c_type, c_request, c_by)
    VALUES ($id, CURRENT_DATE, CURRENT_TIME, '.($type == 'normal' ? 1 : 2).',1,$user) 

if you want to update you can do

update ed_names set c_date = CURRENT_DATE, C_time = CURRENT_TIME, c_type = '.($type == 'normal' ? 1 : 2).''.($type == 'normal' ? 1 : 2).', c_request = c_request + 1, c_by = $user where com_id = $id
Kennethvr
  • 2,660
  • 5
  • 26
  • 35
  • if I used Update instead of Insert would I still be able to use mysql_insert_id(); to get the row insert_id? – acctman Nov 18 '10 at 11:59
  • if you don't know what Id should be increased, how can you increase it then? and no, mysql_insert_id doesn't work on update. – Kennethvr Nov 18 '10 at 12:00
  • take a look at this to know how mysql_insert_id works: http://stackoverflow.com/questions/1388025/how-to-get-id-of-the-last-updated-row-in-mysql – Kennethvr Nov 18 '10 at 12:05