Jump to content

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

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.