kfm50380 Posted June 29, 2012 Share Posted June 29, 2012 The following code should increment 1 to post_no column in the post_count table when the page is loaded however sometimes there is no entry or the entry always remains at a value of 1. Can someone explain why this might be so? Thanks $sl="select * from post_count where user_id='".$_SESSION['user_id']."' order by id DESC limit 0,1"; $or=MySQLQuery($sl); $rws=mysql_fetch_array($or); $rws['post_no']; $rws['post_no']=$rws['post_no']+1; $post_no=$rws['post_no']; $swl="insert into post_count set user_id = '$_SESSION[user_id]', entery_date = '".date("Y-m-d")."', post_no = '$rws[post_no]'"; MySQLQuery($swl); } if( $rws['post_no']==''){ $sll="select * from post_count where user_id='".$_SESSION['user_id']."' order by id DESC limit 0,1"; $orr=MySQLQuery($sll); $rwss=mysql_fetch_array($orr); $post_no = $rwss['post_no']; } Quote Link to comment https://forums.phpfreaks.com/topic/264987-code-is-giving-variable-results/ Share on other sites More sharing options...
memfiss Posted June 29, 2012 Share Posted June 29, 2012 cuz u use wrong INSERT syntax , should be INSERT INTO `table` ( `here` , `are` , `columns` ) VALUES ( 'here' , 'are' , 'values' ); Quote Link to comment https://forums.phpfreaks.com/topic/264987-code-is-giving-variable-results/#findComment-1357920 Share on other sites More sharing options...
kfm50380 Posted June 29, 2012 Author Share Posted June 29, 2012 Thanks, i will try this. Can you also tell me what the relevance of this part of the code is? What i mean if i was missing what would be different? if( $rws['post_no']==''){ $sll="select * from post_count where user_id='".$_SESSION['user_id']."' order by id DESC limit 0,1"; $orr=MySQLQuery($sll); $rwss=mysql_fetch_array($orr); $post_no = $rwss['post_no']; } Quote Link to comment https://forums.phpfreaks.com/topic/264987-code-is-giving-variable-results/#findComment-1357937 Share on other sites More sharing options...
Barand Posted June 29, 2012 Share Posted June 29, 2012 @memfiss INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [iGNORE] [iNTO] tbl_name [PARTITION (partition_name,...)] [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ] Or: INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [iGNORE] [iNTO] tbl_name [PARTITION (partition_name,...)] SET col_name={expr | DEFAULT}, ... [ ON DUPLICATE KEY UPDATE col_name=expr [, col_name=expr] ... ] RTFM The second option above allows the SET syntax. Quote Link to comment https://forums.phpfreaks.com/topic/264987-code-is-giving-variable-results/#findComment-1357961 Share on other sites More sharing options...
DavidAM Posted June 29, 2012 Share Posted June 29, 2012 For the record, mySql allows an alternate syntax for INSERT that uses the SET phrase. So, the original code is valid for an insert. The following code should increment 1 to post_no column in the post_count table when the page is loaded however sometimes there is no entry or the entry always remains at a value of 1. The provided code does NOT increment the post_no. The code INSERTs a record, period. If you want to increment a column in an existing row, you have to use UPDATE. It looks like you want to check to see if the user has an entry. If found, then UPDATE it (incrementing the post_no). If no row is found, then do an INSERT with post_no set to 1. Quote Link to comment https://forums.phpfreaks.com/topic/264987-code-is-giving-variable-results/#findComment-1357963 Share on other sites More sharing options...
Barand Posted June 29, 2012 Share Posted June 29, 2012 Assuming user_id is unique key you can INSERT INTO post_count (user_id, entry_date, post_no) VALUES ({$_SESSION['user_id']}, CURDATE(), 1) ON DUPLICATE KEY SET post_no = post_no+1 Quote Link to comment https://forums.phpfreaks.com/topic/264987-code-is-giving-variable-results/#findComment-1357965 Share on other sites More sharing options...
kfm50380 Posted June 29, 2012 Author Share Posted June 29, 2012 Thanks all, Just to clarify, There is another 'auto increment' column called 'id'. The required result is an insert (not an update of existing). So every time the user cycles through the questions it stores the number of times he did this with the post_no (and adds 1 after each time) + the date + his user_id - The problem is that sometimes the user has cycled through a few times but there is either no entries in the post_count table or the entry for post_no = 1 for that user. If anyone may know why this is causing problems that would be good for my learning..once again thanks for you feedback with this Quote Link to comment https://forums.phpfreaks.com/topic/264987-code-is-giving-variable-results/#findComment-1357973 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.