What I would do....
$min=$ppp*$page; // FIND THIS TO BEGIN EDITS...
// write SQL queries as separate strings, so you can see what they actually are,
// not what you think they are
if($id){
$sqlquery1= "SELECT p.*,text$sfields,edited,tagval,u.id uid,name,$ufields,regdate FROM...