Jump to content

Code is giving variable results


kfm50380

Recommended Posts

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'];



}

 

 

Link to comment
Share on other sites

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'];



}

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.