Jump to content

Help increment field


rondog

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/156336-help-increment-field/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/156336-help-increment-field/#findComment-823150
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/156336-help-increment-field/#findComment-823618
Share on other sites

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/156336-help-increment-field/#findComment-823623
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.