leeandrew Posted March 30, 2008 Share Posted March 30, 2008 I have a code thats adds a number '1' to a field on submit in a form. I want to modify it so that if the field is not null it just adds another '1' onto the number. e.g. $query="UPDATE `database` SET `column` = '1' WHERE `name` = '$username';"; If the column contains the number 934, i want it to then read 935. Just add 1. How can i do this by modifying the above code? Link to comment https://forums.phpfreaks.com/topic/98692-sql-update-help/ Share on other sites More sharing options...
BlueSkyIS Posted March 30, 2008 Share Posted March 30, 2008 $query="UPDATE `database` SET column = (column + 1) WHERE name = '$username'"; Link to comment https://forums.phpfreaks.com/topic/98692-sql-update-help/#findComment-505076 Share on other sites More sharing options...
leeandrew Posted March 30, 2008 Author Share Posted March 30, 2008 Thanks, i figured it out myself accidentally by modifying the code i have shortly after i posted! I have this code now: $query="UPDATE `voting_items` SET `top".$_POST['bookmark_number']."` = `top".$_POST['bookmark_number']."` + 1 WHERE `name` = '".$_POST['page']."';"; mysql_query($query); $query2="UPDATE `pluserdata` SET `top".$_POST['bookmark_number']."` = '".$_POST['page']."' WHERE `userid` = '".$User->userid."';"; mysql_query($query2); } This adds a +1 to whatever number exists in the field. the second query bookmarks the name of the page in the users table of whoever has bookmarkd it. So essentially a +1 goes into the page table field when someone bookmarks it, it allows me to check which pages are the most popular. Problem is when a user overwrites it doesnt substract, so only works as an overall bookmark number and not current. Any ideas on how to substract in the same query? Link to comment https://forums.phpfreaks.com/topic/98692-sql-update-help/#findComment-505097 Share on other sites More sharing options...
BlueSkyIS Posted March 30, 2008 Share Posted March 30, 2008 use - 1 instead. Link to comment https://forums.phpfreaks.com/topic/98692-sql-update-help/#findComment-505105 Share on other sites More sharing options...
leeandrew Posted March 30, 2008 Author Share Posted March 30, 2008 it uses the same form. i would need to check if xyz existed in the field before adding 1 to it. And if so it cannot be updated because it would be there. Currently click update multiple times adds constantly to the field. Link to comment https://forums.phpfreaks.com/topic/98692-sql-update-help/#findComment-505120 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.