Jump to content

bbowler86

New Members
  • Posts

    1
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

bbowler86's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hey my name is Brian I am new to this form (and PHP) I am trying to write a simple inventory management application for my work but I cant get this code to work. Any ideas on this would be greatly appreciated. I use Eclipse and I noticed that right at the function tToEdit() the first time I use <<< HERE all the following variables have turned black indicating to me that for some reason they are not being identified. I have mainpage.php that uses this as an include file and have even made a test include file that just printed "THIS WORKED" so I know its not that I am calling it wrong. Any ideas? Thanks <?php //variables $reachlib = "test complete"; $userName = "user"; $password = "password"; $serverName = "localhost"; $dbName = "web_db"; $dbConn = ""; $adminPassword = "absolute"; $mainProgram = "mainpage.php"; function connectToDB(){ //connects to the DB global $serverName, $userName, $password; $dbConn = mysql_connect($serverName, $userName, $password) or die(mysql_error()); if(!$dbConn){ print "<h3>problem connecting to database...</h3>\n"; }//end if if($dbConn){ print "<h3>Connected to database...</h3>\n"; }//end if $select = mysql_select_db("$dbName"); if(!$select){ print mysql_error() . "<br>\n"; }//end if return $dbConn; }//end connectToDB function qToList($query){ //given a query, makes a quick list of data global $dbConn; $output = ""; $result = mysql_query($query, $dbConn); //print "dbConn is $dbConn<br>"; //print "result is $result<br>"; while ($row = mysql_fetch_assoc($result)){ foreach ($row as $col => $val){ $output .= "$col: $val:<br>\n"; }//end while return $output; }//end qToList function qToTable($query){ //given a query, automatically creates an HTML table output global $dbConn; $output = ""; $result = mysql_query($query, $dbConn); //get column headings $output .= "<table border = 1>\n"; //get field names $output .= "<tr>\n"; while ($field = mysql_fetch_field($result)){ $output .= " <th>$field ->name</th>\n"; } //end while $output .= "</tr>\n\n"; //get row data as an associative array while ($row = mysql_fetch_assoc($result)){ $output .= "<tr>\n"; //look at each field foreach )$row as $col=>$val){ $output .= " <td>$val</td>\n"; } //end foreach $output .= " <td>$val</td>\n\n"; } //end while $output .= "</table>\n"; return $output; }//end qToTable function tToEdit($tableName){ //given a table name, generates HTML table including //ass, delete and edit buttons global $dbConn; $ouput = ""; $query = "SELECT * FROM $tableName"; $result = mysql_query($query, $dbConn); $output .= "<table border = 1>\n"; //get column headings //get field names $output .= "<tr>\n"; while($field = mysql_fetch_field($result)){ $output .= " <th>$field-> </th>\n"; } //end while //get name of index field (presuming it's first field) $keyField = mysql_fetch_field($result, 0); $keyName = $keyField->name; //add empty columns for add, edit, and delete $output .= "<th></th><th></th>\n"; $output .= "</tr>\n\n"; //get row data as an associative ray while ($row = mysql_fetch_assoc($result)){ $output .= "<tr>\n"; //look at each field foreach ($row as $col=>$val){ $output .= " <td>$val</td>\n"; } //end foreach //build little forms for add, delete and edit //delete = DELETE FROM <table> WHERE <key> = <keyval> $keyVal = $row["$keyName"]; $output .= <<<HERE <td> <form action = "deleteRecord.php"> <input type = "hidden" name = "tableName" value = "$tableName"> <input type = "hidden" name = "keyName" value = "$keyName"> <input type = "hidden" name = "keyVal" value = "$keyVal"> <input type = "submit" value = "delete"></form> </td> HERE; // update: won't update yet, but set up edit form $output .= <<< HERE <td> <form action = editRecord.php> <input type = "hidden" name = "tableName" value = "$tableName"> <input type = "hidden" name = "keyName" value = "$keyName"> <input type = "hidden" name = "keyVal" value = "$keyVal"> <input type = "submit" value = "edit"></form> </td> HERE; $output .= "</tr>\n\n"; } //end while //add = INSERT INTO <table> {values} //set up insert form send table name $keyVal = $row["$keyName"]; $output .= <<< HERE <td colspan = "5"> <center> <form action = "addRecord.php"> <input type = "hidden" name = "tableName" value = "$tableName"> <input type = "submit" value = "add a reord"></form> </center> </td> HERE; $output .= "</table>\n"; return $output; } //end tToEdit function rToEdit ($query){ //given a one-record query, creates a form to edit that record //works on any table, but allows direct editing of keys //use smarRToEdit instead if you can global $dbConn; $output = ""; $result = mysql_query($query, $dbConn); $row = mysql_fetch_assoc($result); //get table name from field object $fieldObj = mysql_fetch_field($result, 0); $tableName = $fieldObj->table; $output .= <<< HERE <form action = "updateRecord.php" method = "post"> <input type = "hidden" name = "tableName" value = "$tableName"> <table border = 1> HERE; foreach ($row as $col=>$val){ $output .= <<< HERE <tr> <th>$col</th> <td> <input type = "text" name = "$col" value = "$val"> </td> </tr> HERE; } //end foreach $output .= <<< HERE <tr> <td colspan = 2> <center> <input type = "submit" value = "update this record"> </center> </td> </table> HERE; return $output; }// end rToEdit function smartRtoEdit ($query){ //given a one-record query, creates a form to edit that record //Doesn't let user edit first (primary key) field //genertes dropdown list for foreign keys //MUCH safer than ordinary rToEdit function //--restrictions on table design-- //foreign keys MUST be named tableID where 'table' is table name //(because mySQL doesn't recognize foreign key indicators) //I also expect a 'name' field in any table used as a foreign key //(for same reason) global $dbConn; $output = ""; $result = mysql_query($query, $dbConn); $row = mysql_fetch_assoc($result); //get table name from field object $fileObj = mysql_fetch_field($result, 0); $tableName = $fieldObj->table; $output .= <<< HERE <form action = "updateRecord.php" method = "post"> <input type = "hidden" name = "tableName" value = "$tableName"> <table border = 1> HERE; $fieldNum = 0; foreach ($row as $col=>$val) if ($fieldNum = 0){ //it's primary key, don't make textbox //but store value in hidden field instead //user shouldn't be able to edit primary keys $output .= <<< HERE <tr> <th>$col</th> <td>$val <input thype = "hidden" name = "$col" value = $val> </td> </tr> HERE; } else if (preg_match("/(.*)ID$/". $col, $match)){ //its a foreign key reference //get table name (match[1]) //create a listbox based on table name and its name field $valList = fieldToList($match[1],$col, $fieldNum, "name"); $output .= <<< HERE <tr> <th>$col</th> <td>$val</td> </tr> HERE; } else { $output .= <<< HERE <tr> <th> $col</th> <td> <input type = "text" name = "$col" value = "$val> </td> </tr> HERE; } //end if $output .= <<< HERE <tr> <td colspan = 2> <center> <input type = "submit" value = "update this record"> </center> </td> </tr> </table> </form> HERE; return $output; } //end SmartRToEdit function updateRec($tableName, $fields, $vals){ //expects name of a record, fields array values array //updates database with new values global $dbConn; $output = ""; $keyName = $fields[0]; $keyValue = $vals[0]; $query = ""; $query = ""; $query .= "UPDATE $tableName SET \n"; for ($i; $i < count($fields); $i++){ $query .= $fields[$i]; $query .= " = '"; $query .= $vals[$i]; $query .= "',\n"; } //end for loop //remove last comma from output $query = substr($query, 0, strlen($query) - 2); $query .= "\nWHERE $keyName = '$keyVal'"; $result = mysql_query($query, $dbConn); if ($result) { $query = "SELECT * FROM $tableName WHERE $keyName = '$keyVal'"; $output .-= "<h3>update successful</h3>\n"; $output .= "new value of record:<br>"; $output .= qToTable($query); } else { $output .= "<h3>there was a problem...</h3><pre>$query</pre>\n"; } //end if return $output; } //end updateRec function delRec ($table, $keyName, $keyVal){ //delete $keyVal record from $table global $dbConn; $output = ""; $query = "DELETE FROM $table WHERE $keyName = '$keyVal'": print "query is $query<br>\n"; $result = mysql_query($query, $dbConn); if($result){ $output = "<h3>Record successfully deleted</h3>\n"; } else { $out = "<h3>Error deleting record</h3>\n"; } //end if return output; } //end delRec function tToAdd($tableName){ //given table name, generates HTML form to add an entry to the //table. Works like smartRToEdit in recognizing foreign keys global $dbConn; $output = ""; //process a query just to get field names $query = "SELECT * FROM $tableName"; $result = mysql_query($query, $dbConn); $output .= <<< HERE <form action = "processAdd.php" method = "post"> <table border = "1"> <tr> <th>Field</th> <th>Value</th> </tr> HERE; $fieldNum = 0; while ($theField = mysql_fetch_field($result)){ $fieldName = $theField->name; if ($fileNum == 0){ //it's the primary key field. It'll be autoNumber $output .= <<< HERE <tr> <td>$fieldNum</td> <td>AUTONUMBER <input type = "hidden" name = "$fieldNum" value = "null"> </td> </tr> HERE; } else if (preg_match("/(.*)ID$/", $fieldName, $match)){ //it's a foreign key reference. Use fieldToList to get a select //object for this field $valList = fieldToList($match[1],$fieldName, 0, "name"); $output .= <<< HERE <tr> <td>$fieldName</td> <td>$valList</td> </tr> HERE; } else { //it's an ordinary field. Print a text box $output .= <<< HERE <tr> <td>$fieldName</td> <td><input type = "text name = "$fieldName" value = ""> </td> </tr> HERE; } //end if $fieldNum++ }// end while $output .= <<< HERE <tr> <td colspan = 23> <input type = "hidden" name = "tableName" value = "$tableName"> <input type = "submit" value = "add record> </td> </tr> </table> </form> HERE; return $output; } //end tToAdd function procAdd($tableName, $fields, $vals){ //generates INSERT query, applies to database global $dbConn; $output = ""; foreach ($vals as $theValue){ $query .= "'$theValue', "){ }// end foreach //trim off trailing space and comma $query = substr($query, 0, strlen($query) - 2); $query .= ")"; $output = "query is $query<br>\n"; $result = mysql_query($query, $dbConn); if($result){ $output .= "<h3>Record added</h3>\n"; } else { $output .= "<h3>There was an error</h3>\n"; } //end if return $output; } //end procAdd function fieldToList($tableName, $keyName, $keyVal, $fieldName){ //given table and field, generates an HTML select structure //named $keyName, values will be key field of table, but //text will come from the $fieldName value global $dbConn; $output = ""; $query = "SELECT $keyName, $fieldName FROM $tableName"; $result = mysql_query($query, $dbConn); $output .= "<select name = $keyName>\n"; $recNum = 1; while ($row = mysql_fetch_assoc($result)){ $theIndex = $row["$keyName"]; $theValue = $row["$fieldName"]; $output .= <<< HERE right now, theIndex is $theIndex and keyVal is $keyVal <option value = "$theIndex" HERE; //make it currently slected item if ($theIndex == $keyVal){ $output .= " selected"; } //end if $output .= ">$theValue</option>\n"; $recNum++; }//end while $output .= "</select>\n"; return $output; } //end fieldToList function mainButton(){ //creates a button to return to the main program global $mainProgram; $output .= <<< HERE <form action = "$mainProgram" method = "get"> <input type = "submit" value = "return to main screen"> </form> HERE; return $output; } //end mainButton ?> Mod edit: added code tags
×
×
  • 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.