$result= mysql_query("select MAX(id) from agrup where rubro='$rubro'"); if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; } $row = mysql_fetch_row($result); $agrupid = $row[0]; $agrupid++;