rondog Posted April 30, 2009 Share Posted April 30, 2009 I have a position field and when I upload a file, I need the position of that item to be +1 higher than its previous. I cant use auto increment on this field. I am doing this right now and it putting all uploaded files at 1: <?php $sql = mysql_query("INSERT INTO projectData (proj_id,position,type,path,title,description) VALUES ('$projID',position+1,'$type','$fname','$title','$description')") or die(mysql_error()); ?> any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/156336-help-increment-field/ Share on other sites More sharing options...
mikesta707 Posted May 1, 2009 Share Posted May 1, 2009 the select max mysql command should help you. just do a query, something like $query = mysql_query("SELECT MAX(position) AS position FROM projectData"); $row = mysql_fetch_assoc($query); $pos = $row['position'] + 1; than use that pos variable as the value to input into your position column. You may want to check my syntax first tho Quote Link to comment https://forums.phpfreaks.com/topic/156336-help-increment-field/#findComment-823150 Share on other sites More sharing options...
rondog Posted May 1, 2009 Author Share Posted May 1, 2009 Is that really the only way? I have to do a whole other query to increment it? Quote Link to comment https://forums.phpfreaks.com/topic/156336-help-increment-field/#findComment-823170 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2009 Share Posted May 1, 2009 INSERT INTO projectData (`proj_id`,`position`,`type`,`path`,`title`,`description`) VALUES ('$projID',`position`+1,'$type','$fname','$title','$description') Does that work? Quote Link to comment https://forums.phpfreaks.com/topic/156336-help-increment-field/#findComment-823514 Share on other sites More sharing options...
Daniel0 Posted May 1, 2009 Share Posted May 1, 2009 Why can't you use auto increment? Quote Link to comment https://forums.phpfreaks.com/topic/156336-help-increment-field/#findComment-823516 Share on other sites More sharing options...
rondog Posted May 1, 2009 Author Share Posted May 1, 2009 Why can't you use auto increment? Because I have an id field already that is auto increment. My table is called projectData It contains data for more than one project so there can be fields that look like: id proj_id position 111 212 313 421 522 623 724 so that is why auto increment wont work Quote Link to comment https://forums.phpfreaks.com/topic/156336-help-increment-field/#findComment-823618 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2009 Share Posted May 1, 2009 But if you just use position + 1, it will work exactly like AUTO_INCREMENT. How did you get the 1 after the 3? Quote Link to comment https://forums.phpfreaks.com/topic/156336-help-increment-field/#findComment-823621 Share on other sites More sharing options...
rondog Posted May 1, 2009 Author Share Posted May 1, 2009 right ken..I just thought about that..I am going to have to use mikesta's suggestions with a little modification <?php $query = mysql_query("SELECT MAX(position) AS position FROM projectData WHERE proj_id = '$projID'"); $row = mysql_fetch_assoc($query); $pos = $row['position'] + 1; ?> Quote Link to comment https://forums.phpfreaks.com/topic/156336-help-increment-field/#findComment-823623 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.