Jump to content

[SOLVED] Help with PHP MySql query


tqla

Recommended Posts

hello.

 

I want UPDATE SET a particular row's 'position' field to 1 higher than the highest record.

 

In other words, I want to look at a numeric field in my DB called 'position'.

"SELECT position FROM $content"

 

I then want to find out what the highest number is of all of the records.

Whats the highest number in the 'position' field?

 

I then want to update the current row to 1 higher than the highest number in 'position':

"UPDATE $content SET position = (some code here I think) WHERE page_id = $ID

 

I know this aint the right code but I was wondering if someone could help out. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/140748-solved-help-with-php-mysql-query/
Share on other sites

Thanks thorpe but it seems to be ignoring the highest number and just increments by 1. Here's the code:

 

$ID = $_GET['page_id'];
$sql = "UPDATE $content SET position = (SELECT MAX(position)+1) WHERE page_id = $ID;";
$result = mysql_query($sql) or die (mysql_error());

 

My highest number in position is 6 but the $ID doesn't go from 1 to 7, it just goes to 2.  ???

Solved! Thanks for pointing me in the right direction thorpe! In case anyone needs to know, here's the solution:

 

<?php

$result = mysql_query("SELECT MAX(position) AS maxposition FROM $content");

$row = mysql_fetch_array($result);

$p = $row['maxposition'];

$ID = $_GET['page_id'];

$sql = "UPDATE $content SET position = $p+1 WHERE page_id = $ID;";

$result = mysql_query($sql2) or die (mysql_error());

?>

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.