Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php Inputs only 1/2 my data

Status
Not open for further replies.

Billybonga

IS-IT--Management
Jun 22, 2002
72
0
0
GB
I'm working on a project where i have a "Census" database running in Mysql. Its fields are:

ID|Name|Surname|Townland|Area|county|code|group|tel|mobile|email|notes

I'm having a problem in that when I submit and new record using my php form, only ID|Name|Surname|Townland|Area|county get added to the database.

The submit code is as follows:

Code:
<?
 echo "<table width=\"100%\"><tr>\n"; // Table 1 - SHOW DATABASE
 echo "<td align=center class=big>Parish Census List</td></tr><tr>\n";
 //echo "<td width=\"20%\" valign=top align=center>\n";
 $view="dname";
 $prim="";

 echo "<td width=\"100%\" valign=top align=center>";// Table 3 - SHOW TABLE Desription
 $view="fields";
 if (isset($table)&&$table!="none") {
   if ($usort=="ID") { $sql = "SELECT * FROM ".$table." ORDER BY ID ASC"; }
   elseif ($usort=="christian_name") { $sql = "SELECT * FROM ".$table." ORDER BY christian_name ASC"; }
   elseif ($usort=="surname") { $sql = "SELECT * FROM ".$table." ORDER BY surname ASC"; }
   elseif ($usort=="townland") { $sql = "SELECT * FROM ".$table." ORDER BY townland ASC"; }
   elseif ($usort=="area") { $sql = "SELECT * FROM ".$table." ORDER BY area ASC"; }
   elseif ($usort=="county") { $sql = "SELECT * FROM ".$table." ORDER BY county ASC"; }
   elseif ($usort=="code") { $sql = "SELECT * FROM ".$table." ORDER BY code ASC"; }
   elseif ($usort=="group") { $sql = "SELECT * FROM ".$table." ORDER BY group ASC"; }
   elseif ($usort=="Tel") { $sql = "SELECT * FROM ".$table." ORDER BY Tel ASC"; }
   elseif ($usort=="Mobile") { $sql = "SELECT * FROM ".$table." ORDER BY Mobile ASC"; }
   elseif ($usort=="eMail") { $sql = "SELECT * FROM ".$table." ORDER BY eMail ASC"; }
   elseif ($usort=="Notes") { $sql = "SELECT * FROM ".$table." ORDER BY Notes ASC"; }
   elseif ($usort=="") { $sql= "SELECT * FROM ".$table; }
   else { $sql= "SELECT * FROM ".$table; }
   $r = mysql_query ($sql);
   if(!$r) {    mysql_error(); exit;   }
   if (mysql_num_rows($r)) {
     $numFields = mysql_num_fields( $r );
     $Table = new ShowTable ($r,$sql); // View Table 3
     $Table->DrawBody();
   $view="field";
        srand ((double) microtime() * 100000000);
        $randval = rand();
   echo $HeadTable."<tr><td align=center bgcolor=$bg1 class=head>\n";
   echo "<a href=\"?dname=$dname&table=$table&act=show&ran=".$randval."\" class=head>[ Select one Record to Modify  ]</a> |\n";
   echo "<a href=\"?dname=$dname&table=$table&act=add&ran=".$randval."\" class=head>[ Add Record ]</a>\n";
   echo "</td></tr></table>\n";// Table 4 - SHOW TABLE
   if ($act=="show") ViewTable ($dname,$table); // View Table 4
   elseif ($act=="add") AddRec ($dname,$table);
   elseif ($act=="edit") EditRec ($dname,$table,$pfname,$pfvalue);
   elseif ($act=="query") NewQuery ($dname,$table);
   elseif ($act=="doquery") DoQuery ($dname,$table,$sqlquery);
   elseif ($act=="del") {
   $sql="DROP TABLE $table";
   $re1 = mysql_query ($sql);
   if ($re1) echo "<br>Delete complete!\n";
   else {echo $sql."<br>\n".mysql_error(); exit; }
   }
   elseif ($act=="editnow") {
       if ($Submit=="Delete") {
       $sql = "DELETE FROM $table WHERE ".$pfname."=".$pfvalue;
       $re1 = mysql_query ($sql);
       if ($re1) echo "<br>Delete complete!\n";
       else {echo $sql."<br>\n".mysql_error(); exit; }
       }
       else {
       $sql = "SELECT * FROM ".$table." LIMIT 1";
       $re1 = mysql_query ($sql);
       for ($j=0;$j<mysql_num_fields($re1);$j++) {
       $an[$j]=mysql_field_name($re1,$j);
       $at[$j]=mysql_field_type($re1,$j);
       }
       $sql1 = "UPDATE $table SET ";
       for ($j=0;$j<mysql_num_fields($re1);$j++) {
          if (!ereg("auto_inc",mysql_field_flags($re1,$j)) && $$an[$j]!="") {
          $sql1.= $an[$j]."=";
             if (ereg("int",$at[$j]) || ereg("float",$at[$j]))
                  $sql1.= $$an[$j].",";
             else $sql1.= "'".$$an[$j]."',"; } }
       $sql = substr($sql1,0,(strlen($sql1)-1))." WHERE ".$pfname."=".$pfvalue;
       $re2 = mysql_query ($sql);
       if ($re2) echo "<br>Update complete!\n";
       else {echo $sql."<br>\n".mysql_error(); exit; }
   } }
   elseif ($act=="addnow") {
     $sql = "SHOW fields FROM $table FROM $dname";
     $r2 = mysql_query ($sql);
     for ($j=0;$j<mysql_num_rows($r2);$j++)
      if ($F=mysql_fetch_array($r2)) {
      $an[$j]= $F['Field'];
      $at[$j]= ereg_replace("([()0-9]+)","",$F['Type']);
      $af[$j]= $F['Extra'];
      if ($F['Key']=="PRI") $nam=$F['Field'];
      }
      $sql1 = "INSERT INTO $table (";
      for ($j=0;$j<mysql_num_fields($r2);$j++)
          if (!ereg("auto_inc",$af[$j]) && $$an[$j]!="")
          $sql1.= $an[$j].",";
      $sql = substr($sql1,0,(strlen($sql1)-1)).") VALUES (";
      for ($j=0;$j<mysql_num_fields($r2);$j++)
          if (!ereg("auto_inc",$af[$j]) && $$an[$j]!="")
            if (ereg("int",$at[$j]) || ereg("float",$at[$j]))
            $sql.= $$an[$j].",";
            else $sql.= "'".$$an[$j]."',";
      $sql = substr($sql,0,(strlen($sql)-1)).")";
      $re2 = mysql_query ($sql);
      if ($re2) echo "<br>Save complete!\n";
      else {echo $sql."<br>\n".mysql_error(); exit; }
   }
   }
   else echo "<br><br><font color=red>No fields found in table <b>$table</b> in DB <b>$dname</b>!</font><br>";
 }
 echo "</td></tr>";
 echo "<tr><td class=sml align=center><br><br>";
 //echo "Admin: <a href=\"mailto:\" class=sml></a><br>";
 echo "</td>";
 echo "<td></td></tr>";
 echo "</table>";
?>

Can anyone see what is wrong ??

Thanks for your help
 
I suggest you use echo and output to the screen one of the SQL queries you are using... so you can actually see the statement that is failing. Echoing out the mysql_error() directly after a query has been "run" will give you feedback on any sql error as well:
Code:
echo mysql_error();
Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
I really like to help, I really do, but your code is a bit of a mess. My eys hurt trying to follow it.

There are a few things you forgot to submit. I assume this is all through a webpage via a HTML form, right? Before I'd start anywhere else, I'd have the page var_dump your $POST and see what's in your arrays. If all the data is there and correct, then have it return your mysql insert command and see if that checks out. Copy and paste that insert command into your mysql database to see if the syntax is correct. Make sure your mysql link is using an username and password that has permission to *all* your columns in that table. If all that checks out, then there's no reason why you should be having this problem.

I've always done forms like this:

mysql data -> form -> user -> form -> mysql. Remember that mysql connections cost quite a bit of resource if you open and close it too much. If you're editting the same data that's going back in there's no need to requery the table for the same data. The data you have should be the data that's going back in. So it minimizes the need to do mysql calls and the need to keep select datat from mysql, especially since it's all one table. I'm not the best PHP programmer, but that's what I got from my experiences.

Most often, what you'll find is that you'll find your own mistakes if you optimize your code.
 
Thanks everyone for your help.
I agree with you sleepingkirby - the code is very much a brain killer.
I'm not a great php programmer and more a code hacker.
With regard to the user rights : I have full rights to write to all colums- a mate of mine has also tested it on his server and still has same problem.

What i can't figure out is when you add the new record, only the first 5/6 fields get added, but if you then go and edit that entry (the other 6 records are black) enter the data into these 6 fields and save and then the entire record is saved correctly ???!!!??

It's really stumped me. Here is the full code from the .php file. If you want the sql file just let me know.


Code:
<?
//  *********************************************************************
//  *    
//  *********************************************************************

//  *********************************************************************
//  *     Update: 01.11.2005
//  *********************************************************************


// Database Connection

$Host="localhost"; // MySQL HOST
$User=""; // MySQl User
$Passwrd=""; // MySQL Password
 if (!isset ($dname)) $dname="parish"; // MySQL DataBase

 if (!isset ($rows)) $rows=0;
 if (!isset($limit)) $limit=100;    // Limit rows records per page
 $TableW="99%";                     // Table Width
 $TableA="center";                  // Table Align
 $TCellP=1;                         // Table CellPadding
 $TCellS=1;                         // Table CellSpasing
 $bg1 = "#6699CC";                  // Table BGColor
 $bg2 = "#E0F0FF";                  // Table header BGColor
 $bgred = "#FFF0E0";                // Selected rows BGColor
 $bglig1 = "#FCFCFC";               // Not Selected rows BGColor 1
 $bglig2 = "#F4F4F4";               // Not Selected rows BGColor 2
 if (!isset($table)) $table="census";

$link = mysql_connect ($Host,$User,$Passwrd)
        or die ("Could not connect");
$link = mysql_select_db ($dname,$link)
        or die ("Could not selected DB");

if ($dname != 'parish') die ("Database Access Denied");

//******** The End Block of Variables********************************
 $HeadTable = "<table width=".$TableW." align=".$TableA." cellpading=".$TCellP." cellspacing=".$TCellS." border=0 bgcolor=$bg1>\n";
/********************** ListPages CLASS ****************************/
class ListPages {
var $pnum;
var $lim;
var $allp;
function ListPages ($pnum,$lim,$allp)
{
  $this->PNumber = $pnum;
  $this->PLimit = $lim;
  $this->APages = $allp;
}
Function Pages()
{
   $this->PHeader();
   $this->PBody();
   $this->PFooter($this->APages);
}
function PHeader()
{global $HeadTable,$dname,$table,$act;
 echo $HeadTable."<form action=\"?dname=$dname&table=$table&act=$act\" method=post>\n";
 echo "<tr><td align=center>\n";
}
function PBody()
{ echo $this->Adres(0)." - ";
  for ($i=1;$i<=ceil($this->APages/$this->PLimit);$i++)
  if (abs((ceil($this->PNumber/$this->PLimit)+1)-$i)<6)
    if ($this->PNumber!=$this->PLimit*($i-1)) echo $this->Adres($this->PLimit*($i-1));
      else echo $this->Adres($this->PLimit*($i-1));
  if ($this->APages>$this->PLimit) $maxl=($this->APages-$this->PLimit);
   else $maxl=0;
  echo " - ".$this->Adres($maxl);
}
function PFooter($rw)
{ echo "<input type=hidden name=\"rows\" value=\"".$rw."\"></td></tr>\n";
  echo "</form></table>\n";
}
function Adres($nnn)
{ return "<input type=Submit name=\"num\" value=\"".$nnn."\">"; }
}// end class
/********************** 2 ListPages CLASS ****************************/
class QListPages extends ListPages {
function PHeader()
{global $HeadTable,$dname,$table,$act;
 echo $HeadTable."<form action=\"?dname=$dname&table=$table&act=$act\" method=post>\n";
 echo "<tr><td align=center>\n";
}
function PFooter($rw)
{ GLOBAL $sqlquery;
  echo "<input type=hidden name=\"sqlquery\" value=\"$sqlquery\">";
  echo "<input type=hidden name=\"rows\" value=\"".$rw."\"></td></tr>\n";
  echo "</form></table>\n";
}
};
/***************************** ShowTable CLASS************************************/
 class ShowTable {
 var $Res;                      // Query result
 var $QSql;
 function ShowTable ($Res,$QSql) {
  $this->Res = $Res;
  $this->Sql = $QSql;
 }
 function DrawBody () {
  GLOBAL $bg1,$bg2,$bglig1,$bglig2,$bgred,$view,$dname,$table,$HeadTable,$limit,$num,$act,$prim;
  $sum=0;$st3="";
  echo "<br>".$HeadTable."<tr><td align=center bgcolor=$bg1 class=head>".$this->Sql."</td></tr></table>";
  echo $HeadTable."<tr><td bgcolor=$bg2 width=\"1%\">#</td>\n";
  $st3=""; $pj=0;
  for ($j=0;$j<mysql_num_fields($this->Res);$j++) {  // Get column width
       $arlen[$j]=mysql_field_len($this->Res,$j); $sum+=$arlen[$j];
       }
  for ($j=0;$j<mysql_num_fields($this->Res);$j++) {  // Draw Header Table
      if ($view=="field")  $st3=" class=sml width=\"".round($arlen[$j]*100/$sum)."%\"";
      echo "<td bgcolor=$bg2 $st3><a href=\"?usort=".mysql_field_name($this->Res,$j)."\" class=\"sml\">".mysql_field_name($this->Res,$j)."</a>\n"; }
      //echo "<a href=\"?usort=".mysql_field_name($this->Res,$j)."\"><img src=\"up.gif\" border=\"0\"></a>\n";
      //echo "<a href=\"?usort=".mysql_field_name($this->Res,$j)."_d\"><img src=\"down.gif\" border=\"0\"></a></td>\n";
  echo "</tr>\n";
  $bg=$bglig1; $ic=0; $rp=0;
  while (($F=mysql_fetch_array($this->Res))  && $ic<$limit) {
    if ($view=="fields" && $F['Key']=="PRI") {$prim.=$F['Field']; $rp=$ic; }
    if ($bg!=$bglig2) $bg=$bglig2;  else $bg=$bglig1; // Get color of row
    if($F[0]=="$dname" || $F[0]=="$table") $bg=$bgred;// Draw Active Table or DB
    if ($view=="field") $cntr = (++$ic)+$num;
    else $cntr = (++$ic);
    echo "<tr><td bgcolor=white class=sml width=\"1%\">".$cntr."</td>\n"; // Echo count
    for ($j=0;$j<mysql_num_fields($this->Res);$j++) {
       $st1=">"; $st2="</a>";
       if     ($view=="dname") $st1="><a href=\"?$view=".$F[0]."\">";
       elseif ($view=="table") $st1="><a href=\"?dname=$dname&$view=".$F[0]."\">";
       elseif ($view=="field")
             { $st1="><a href=\"?dname=$dname&table=$table&act=edit&pfname=ID&pfvalue=".$F[$rp]."\" class=link>";
               $st3=" class=sml width=\"".round($arlen[$j]*100/$sum)."%\""; }
       else $st2="";
       if ($j==0) echo "<td bgcolor=$bg $st3 $st1".$F[$j]."$st2</td>\n";
       else       echo "<td bgcolor=$bg $st3>".$F[$j]."</td>\n";
       }
    echo "</tr>\n";
  }
  echo "</table>\n";
 }
} // end class show table
/***************************** ShowBlank CLASS************************************/
class ShowBlank {
 var $Nam;                      // Field Name
 var $Typ;                      // Field Type
 var $Res;                      // Field
 var $Flg;
 var $Pkn;                      // PRI Filed name
function ShowBlank ($Nam,$Typ,$Len,$Flg,$Res,$Pkn) {
  $this->Nam = $Nam;
  $this->Typ = $Typ;
  $this->Len = $Len;
  $this->Res = $Res;
  $this->Flg = $Flg;
  $this->Pkn = $Pkn;
}
function ShowBody () {
 GLOBAL $HeadTable,$bglig1,$bglig2,$dname,$table,$pfvalue,$act;
 $Nams = explode("|", substr($this->Nam,0,(strlen($this->Nam)-1)));
 $Type = explode("|", substr($this->Typ,0,(strlen($this->Typ)-1)));
 $Leng = explode("|", substr($this->Len,0,(strlen($this->Len)-1)));
 $Flag = explode("|", substr($this->Flg,0,(strlen($this->Flg)-1)));
 $Fild = explode("|", substr($this->Res,0,(strlen($this->Res)-1)));
 echo "<br><form action=\"?dname=$dname&table=$table&act=".$act."now&pfname=".$this->Pkn."&pfvalue=$pfvalue\" method=post>".$HeadTable;
 echo "<tr><td class=head>".strtoupper("$act")." this record!</td></tr>\n";
 for ($j=0;$j<count($Nams);$j++) {
   echo "<tr><td bgcolor=$bglig1>$Nams[$j] <div class=sml>$Type[$j] $Leng[$j] $Flag[$j]</div></td>";
   if (ereg("auto_inc",$Flag[$j])) { $sk="disabled"; $Fild[$j]="AUTO";} else $sk="";
   if ($Leng[$j]<40) $ln=$Leng[$j];
   elseif ($Leng[$j]<=127) $ln=40;
   if ($Leng[$j]>127 || $Type[$j]=="text")
      echo "<td bgcolor=$bglig2><textarea name=\"$Nams[$j]\" cols=\"40\" rows=\"7\">$Fild[$j]</textarea></td></tr>\n";
   else
      echo "<td bgcolor=$bglig2><input type=text $sk name=\"$Nams[$j]\" value=\"$Fild[$j]\" size=\"$ln\" maxlength=$Leng[$j]></td></tr>\n";
 }
 echo "<tr><td align=center><input type=Submit name=Submit value=\"Update\"> ";
 if ($act=="edit") echo "<input type=Submit name=Submit value=\"Delete\">\n";
 echo "</td></tr></table></form>\n";
}
} // end class show blank
function EditRec ($dbn,$tabl,$nam,$idt)
{
  $sql = "SELECT * FROM ".$tabl." WHERE $nam=$idt";
  $r2 = mysql_query ($sql)
      or die ("Invalid query. ".mysql_error());
  if ($F=mysql_fetch_array($r2))  {
  for ($j=0;$j<mysql_num_fields($r2);$j++) {
      $an.=mysql_field_name($r2,$j)."|";
      $at.=mysql_field_type($r2,$j)."|";
      $al.=mysql_field_len($r2,$j)."|";
      $af.=mysql_field_flags($r2,$j)."|";
      $az.=$F[$j]."|";
  }
  $Blank = new ShowBlank ($an,$at,$al,$af,$az,$nam);
  $Blank->ShowBody();
  }
}
function AddRec ($dbn,$tabl)
{
  $an = $at = $af = $az = $al = '';
  $sql = "SHOW fields FROM $tabl FROM $dbn";
  $r2 = mysql_query ($sql)
      or die ("Invalid query ".mysql_error());
      for ($j=0;$j<mysql_num_rows($r2);$j++)
      if ($F=mysql_fetch_array($r2)) {
      $an.= $F['Field']."|";
      $at.= ereg_replace("([()0-9]+)","",$F['Type'])."|";
      if (ereg ("([0-9]+)", $F['Type'], $str))  $al.=$str[0]."|";
      else $al.="|";
      $af.= $F['Extra']."|";
      $az.= $F['Default']."|";
      if ($F['Key']=="PRI") $nam=$F['Field'];
      }
  $Blank = new ShowBlank ($an,$at,$al,$af,$az,$nam);
  $Blank->ShowBody();
}
function NewQuery ($dbn,$tabl)
{ GLOBAL $HeadTable,$randval;
  echo "<br><form action=?dname=$dbn&table=$tabl&act=doquery&ran=$randval method=post>\n";
  echo $HeadTable."<tr><td align=center>";
  $sql = "SELECT * FROM $tabl";
  echo "<input type=text size=\"60\" name=\"sqlquery\" value=\"$sql\">";
  echo "<input type=Submit value=\"OK\">\n";
  echo "</td></tr></table></form>\n";
}
function DoQuery ($dbn,$tabl,$sq1)
{ GLOBAL $HeadTable,$bglig1,$limit,$bg1,$bg2,$table,$num,$rows;
  $deflim=0;$vw1="";
  $sq1= ereg_replace("[\\]","",$sq1);
  $operation = explode(" ",$sq1);
  for ($i=count($operation);$i>=0;$i--) {
    $oper=strtoupper(trim($operation[$i]));
    if ($oper=="FROM") $table=$operation[$i+1];
    if ($oper=="LIMIT") ++$deflim;
    if ($oper=="SELECT" || $oper=="SHOW" || $oper=="EXPLAIN") $vw1.=$oper;
}
  if (!isset($num) && (!$deflim))  {
     $num=0;
     $r2 = mysql_query ($sq1);
     $rows=0;
     if ($r2&&$vw1!="") $rows = mysql_num_rows($r2);  else $rows=0;
  }
  else {
     $sq1.=" LIMIT ".$num.", ".$limit;
     $r2 = mysql_query ($sq1);
  }
if ($r2) {
  if ($vw1=="") {
  echo "<br>".$HeadTable."";
  echo "<tr><td align=center>Operation completed successful!</td></tr>";
  echo "</table>\n";
  }
  else {
  echo "<br>".$HeadTable;
  echo "<tr><td class=head bgcolor=$bg2><font color=\"$bg1\">TOTAL: ".$rows." rows in this Query</font></td></tr>";
  echo "</table>";
  $Table2 = new ShowTable ($r2,$sq1);
  $Table2->DrawBody();
  if ($rows>$limit) {
       $Listing = new QListPages($num,$limit,$rows);
       $Listing -> Pages(); }
       }
  }
  else {
  echo "<br>".$HeadTable."";
  echo "<tr><td align=center>".mysql_error()."</td></tr>";
  echo "</table>\n";
  }
}
function ViewTable ($dbn,$tabl)
{    GLOBAL $HeadTable,$num,$limit,$rows,$bg1,$bg2;
     $sql= "SELECT * FROM ".$tabl;
     if (!isset($num))
     { $num=0;
        $r2 = mysql_query ($sql)
             or die ("Invalid query ".mysql_error());
       $rows = mysql_num_rows($r2);  }
     else
     { $sql1.=$sql." LIMIT ".$num.", ".$limit;
       $r2 = mysql_query ($sql1)
             or die ("Invalid query ".mysql_error());
       }
     if ($rows) {
        echo "<br>".$HeadTable;
        echo "<tr><td class=head bgcolor=$bg2><font color=\"$bg1\">TOTAL: ".$rows." rows in table </font><font color=navy><i>$tabl</i></font></td></tr>";
        echo "</table>";
        $Table2 = new ShowTable ($r2,$sql);
        $Table2->DrawBody();
        if ($rows>$limit) {
         $Listing = new ListPages($num,$limit,$rows);
         $Listing -> Pages(); }
     }
   else echo "<br><br><font color=red>Empty table <b>$table</b> in DB <b>$dbn</b>!</font><br>";
}
?>
<html>
<head>
<title>Parish Census List</title>
<style type="text/css"><!--
        body, td, p {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt}
        .head {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 8pt;
                font-weight:bold;
                color:white;
        }
        .big  {  font-size: 16pt; font-weight:bold; color:#6699CC}
        .sml  {  font-family: Arial, Helvetica, sans-serif; font-size: 8pt; color:#336699}
        .link {  font-size: 12pt; font-weight:bold; color:#6699CC}
        -->
</style>
</head>
<body bgcolor=#FFFFFF>
<?
 echo "<table width=\"100%\"><tr>\n"; // Table 1 - SHOW DATABASE
 echo "<td align=center class=big>Parish Census List</td></tr><tr>\n";
 //echo "<td width=\"20%\" valign=top align=center>\n";
 $view="dname";
 $prim="";

 //$sql= "SHOW DATABASES";
 //$r = mysql_query ($sql);
 //if(!$r) { echo mysql_error();  }
 //if (mysql_num_rows($r)>0) {
 //   $Table = new ShowTable ($r,$sql); // View Table 1
 //   $Table->DrawBody(); }

 //$view="table";
 //if (isset($dname)) { // // Table 2 - SHOW TABLES
  //$sql= "SHOW TABLES FROM ".$dname;
  //$r = mysql_query ($sql);
  //if(!$r) {  mysql_error();    }
  //$numFields = mysql_num_fields( $r );
  //if (mysql_num_rows($r)>0) {
    //$Table = new ShowTable ($r,$sql);// View Table2
    //$Table->DrawBody(); }
  //else {
      //$sqlquery="DROP%20DATABASE%20$dname";
      //$table="none";
      //echo "<br><a href=?dname=$dname&table=$table&act=doquery&sqlquery=$sqlquery>DROP THIS DATABSE</a><br>\n";
      //if ($act=="doquery") {
         //echo rawurldecode($sqlquery)."<br>\n";
         //$r = mysql($dname,rawurldecode($sqlquery));
         //if ($r) echo "DROP COMPLETE!<br>\n";
         //else echo "Problem!";//die(mysql_error());
         //}
      //}
//}
 echo "<td width=\"100%\" valign=top align=center>";// Table 3 - SHOW TABLE Desription
 $view="fields";
 if (isset($table)&&$table!="none") {
   //$sql= "SHOW fields FROM ".$table." FROM ".$dname;
   if ($usort=="ID") { $sql = "SELECT * FROM ".$table." ORDER BY ID ASC"; }
   elseif ($usort=="christian_name") { $sql = "SELECT * FROM ".$table." ORDER BY christian_name ASC"; }
   elseif ($usort=="surname") { $sql = "SELECT * FROM ".$table." ORDER BY surname ASC"; }
   elseif ($usort=="townland") { $sql = "SELECT * FROM ".$table." ORDER BY townland ASC"; }
   elseif ($usort=="area") { $sql = "SELECT * FROM ".$table." ORDER BY area ASC"; }
   elseif ($usort=="county") { $sql = "SELECT * FROM ".$table." ORDER BY county ASC"; }
   elseif ($usort=="code") { $sql = "SELECT * FROM ".$table." ORDER BY code ASC"; }
   elseif ($usort=="group") { $sql = "SELECT * FROM ".$table." ORDER BY group ASC"; }
   elseif ($usort=="Tel") { $sql = "SELECT * FROM ".$table." ORDER BY Tel ASC"; }
   elseif ($usort=="Mobile") { $sql = "SELECT * FROM ".$table." ORDER BY Mobile ASC"; }
   elseif ($usort=="eMail") { $sql = "SELECT * FROM ".$table." ORDER BY eMail ASC"; }
   elseif ($usort=="Notes") { $sql = "SELECT * FROM ".$table." ORDER BY Notes ASC"; }
   elseif ($usort=="") { $sql= "SELECT * FROM ".$table; }
   else { $sql= "SELECT * FROM ".$table; }
   $r = mysql_query ($sql);
   if(!$r) {    mysql_error(); exit;   }
   if (mysql_num_rows($r)) {
     $numFields = mysql_num_fields( $r );
     $Table = new ShowTable ($r,$sql); // View Table 3
     $Table->DrawBody();
   $view="field";
        srand ((double) microtime() * 100000000);
        $randval = rand();
   echo $HeadTable."<tr><td align=center bgcolor=$bg1 class=head>\n";
   echo "<a href=\"?dname=$dname&table=$table&act=show&ran=".$randval."\" class=head>[ Select one Record to Modify  ]</a> |\n";
   echo "<a href=\"?dname=$dname&table=$table&act=add&ran=".$randval."\" class=head>[ Add Record ]</a>\n";
   echo "</td></tr></table>\n";// Table 4 - SHOW TABLE
   if ($act=="show") ViewTable ($dname,$table); // View Table 4
   elseif ($act=="add") AddRec ($dname,$table);
   elseif ($act=="edit") EditRec ($dname,$table,$pfname,$pfvalue);
   elseif ($act=="query") NewQuery ($dname,$table);
   elseif ($act=="doquery") DoQuery ($dname,$table,$sqlquery);
   elseif ($act=="del") {
   $sql="DROP TABLE $table";
   $re1 = mysql_query ($sql);
   if ($re1) echo "<br>Delete complete!\n";
   else {echo $sql."<br>\n".mysql_error(); exit; }
   }
   elseif ($act=="editnow") {
       if ($Submit=="Delete") {
       $sql = "DELETE FROM $table WHERE ".$pfname."=".$pfvalue;
       $re1 = mysql_query ($sql);
       if ($re1) echo "<br>Delete complete!\n";
       else {echo $sql."<br>\n".mysql_error(); exit; }
       }
       else {
       $sql = "SELECT * FROM ".$table." LIMIT 1";
       $re1 = mysql_query ($sql);
       for ($j=0;$j<mysql_num_fields($re1);$j++) {
       $an[$j]=mysql_field_name($re1,$j);
       $at[$j]=mysql_field_type($re1,$j);
       }
       $sql1 = "UPDATE $table SET ";
       for ($j=0;$j<mysql_num_fields($re1);$j++) {
          if (!ereg("auto_inc",mysql_field_flags($re1,$j)) && $$an[$j]!="") {
          $sql1.= $an[$j]."=";
             if (ereg("int",$at[$j]) || ereg("float",$at[$j]))
                  $sql1.= $$an[$j].",";
             else $sql1.= "'".$$an[$j]."',"; } }
       $sql = substr($sql1,0,(strlen($sql1)-1))." WHERE ".$pfname."=".$pfvalue;
       $re2 = mysql_query ($sql);
       if ($re2) echo "<br>Update complete!\n";
       else {echo $sql."<br>\n".mysql_error(); exit; }
   } }
   elseif ($act=="addnow") {
     $sql = "SHOW fields FROM $table FROM $dname";
     $r2 = mysql_query ($sql);
     for ($j=0;$j<mysql_num_rows($r2);$j++)
      if ($F=mysql_fetch_array($r2)) {
      $an[$j]= $F['Field'];
      $at[$j]= ereg_replace("([()0-9]+)","",$F['Type']);
      $af[$j]= $F['Extra'];
      if ($F['Key']=="PRI") $nam=$F['Field'];
      }
      $sql1 = "INSERT INTO $table (";
      for ($j=0;$j<mysql_num_fields($r2);$j++)
          if (!ereg("auto_inc",$af[$j]) && $$an[$j]!="")
          $sql1.= $an[$j].",";
      $sql = substr($sql1,0,(strlen($sql1)-1)).") VALUES (";
      for ($j=0;$j<mysql_num_fields($r2);$j++)
          if (!ereg("auto_inc",$af[$j]) && $$an[$j]!="")
            if (ereg("int",$at[$j]) || ereg("float",$at[$j]))
            $sql.= $$an[$j].",";
            else $sql.= "'".$$an[$j]."',";
      $sql = substr($sql,0,(strlen($sql)-1)).")";
      $re2 = mysql_query ($sql);
      if ($re2) echo "<br>Save complete!\n";
      else {echo $sql."<br>\n".mysql_error(); exit; }
   }
   }
   else echo "<br><br><font color=red>No fields found in table <b>$table</b> in DB <b>$dname</b>!</font><br>";
 }
 echo "</td></tr>";
 //echo "<tr><td class=sml align=center><br><br>";
 //echo "Admin: <a href=\"mailto:\" class=sml></a><br>";
 //echo "</td>";
 //echo "<td></td></tr>";
 echo "</table>";
?>
</body>
</html>
 
I guess you missed the suggestion(s) to echo out the actual mysql query you are trying to perform. That would seem to be the most practical way forward at this stage.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Ok - cheers for your help BabyJeffy
I'll give this a shot
 
Billybonga:
If I could make a couple of constructive criticisms.....

First, I strongly recommend that you pick an indentation style and stick to it. Your code sample uses at least 3 that I can see.

Second, elseif is evil. I have yet to see a PHP code application where the use of a switch statement doesn't improve readability over elseif.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
We all build upon constructive criticism sleipnir
If i could re-write the lot I would, like I mentioned, I'm more of a programming hacker than a programmer.

All i want is a nice form to input into my sql, display my records in nice rows/columns.

I've been trying to integrate the printing of the data to avory labels but trying for format it really sucks, so i'm going to export the data to a csv file and just use word mail merge.

I was hoping to have the project complete by the weekend but this is looking highly unlikely
 
Can I make a suggestion,

Code:
  if ($usort=="ID") { $sql = "SELECT * FROM ".$table." ORDER BY ID ASC"; }
   elseif ($usort=="christian_name") { $sql = "SELECT * FROM ".$table." ORDER BY christian_name ASC"; }
   elseif ($usort=="surname") { $sql = "SELECT * FROM ".$table." ORDER BY surname ASC"; }
   elseif ($usort=="townland") { $sql = "SELECT * FROM ".$table." ORDER BY townland ASC"; }
   elseif ($usort=="area") { $sql = "SELECT * FROM ".$table." ORDER BY area ASC"; }
   elseif ($usort=="county") { $sql = "SELECT * FROM ".$table." ORDER BY county ASC"; }
   elseif ($usort=="code") { $sql = "SELECT * FROM ".$table." ORDER BY code ASC"; }
   elseif ($usort=="group") { $sql = "SELECT * FROM ".$table." ORDER BY group ASC"; }
   elseif ($usort=="Tel") { $sql = "SELECT * FROM ".$table." ORDER BY Tel ASC"; }
   elseif ($usort=="Mobile") { $sql = "SELECT * FROM ".$table." ORDER BY Mobile ASC"; }
   elseif ($usort=="eMail") { $sql = "SELECT * FROM ".$table." ORDER BY eMail ASC"; }
   elseif ($usort=="Notes") { $sql = "SELECT * FROM ".$table." ORDER BY Notes ASC"; }
   elseif ($usort=="") { $sql= "SELECT * FROM ".$table; }
   else { $sql= "SELECT * FROM ".$table; }

could be served just as well by using:
Code:
   if (isset($usort)){
   	$sql = "SELECT * FROM ".$table." ORDER BY ".$usort." ASC"; 
   }else{
   	$sql= "SELECT * FROM ".$table;
   }

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Cheers for that improvement KarveR - it works nicely and has really shortened the code up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top