Location: PHPKode > scripts > phpWords > phpWordsJapanese/contact.php
<?php
require( "functions.php" );
require( "config.php" );

/*
    Authenticate routine
    first if block
    if $action=="setcookie" this page was called from login forms
    $userid and $passwd are checked against values in config.php file
    if credentials match, set cookies on user's browser and set
    the authorized flag to true.
    Else doLogin()

    second if block
    if authorized above then drop though
    else if cookies were already set on user's browser drop through
    else doLogin()
*/

//if ( $action == "setcookie" ) {
//    if ( ($userid == $auth_user) && ($passwd == $auth_passwd ) ) {
//        setcookie("cookie_user",$userid);  //set cookies on user browser
//        setcookie("cookie_passwd",$passwd);
//        $authorized = 1;
//    }
//    else {
//        $titleMsg = "Authentication Failed - Try Again";
//        doLogin($titleMsg); // authentication failed show login again
//        exit();
//    }
//}

//if ( $authorized ) {
//    //echo "authorized<br>";
//    //break; illegal to use it here comment just for clarity
//}
//elseif ( (isset($cookie_user)) && (isset($cookie_passwd)) ) {
//    //echo "auth cookies previously set<br>";
//    //break;
//}
//else {
//    $titleMsg = "Authenticate";
//    doLogin($titleMsg);
//}


/* Script Name:    contact.php3
Version chaged to 2.1.1 10-11-00
Script Purpose:  Contact Database

Files:    index.html -frameset description
          contact.desc - table create file
          contact.php3 - main program displays in right-side window
          leftframe.php3 - controls in left-side window
          functions.php3  - contacts was starting to get too big
          logout.php     - unset cookies and redisplay login frame
          config.php3     - change variables for local conditions here



Modified: Barry Logan (hide@address.com)
         4-16-01  change README to recommend
                  that config.php3 be moved out of DocRoot tree
                  edit top of contact.php3 to reflect absolute
                  path where config.php3 has been placed


         10-11-00 Thanks to Bernd Casimir (hide@address.com)
                  corrected some typos, added required
                  global variables to some functions, changed
                  mysql_connect to mysql_pconnect.

          10-1-00 comments about authentication added
                  cleaned up authentication/cookie routine
                  added logout routine (clears cookies)
                  tested with PHP4, Apache 1.3.12

          7-14-2000 fixed missing title field in contact.desc
          4-16-2000 substantially rewritten - added frames,
          added alpha buttons in left frame for record output in right frame,
          added search function for last name or company name searches,
          added more contact info columns to Mysql table,
          added password authentication via cookies.
          Comments now limited to one comment attached to contact table.
          See contact.desc for table description.

          01-25-2000 cleaned up some syntax errors for function
          definitions and control statements. Name in DB create
          script did not match script names.

         Original Script Author:  Michael J. Miller Jr. (hide@address.com)
         See: http://www.webtechniqes.com/archives/1998/01/note
         Script Purpose: Simple Contact Database Script Version: 1.1
*/


?>
<HTML>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<title>List Words</title>
</head>

<BODY bgcolor="#ffffff">
<?


$new_wid = 0;

/* Now to define functions. */
/* The main screen.  Printed when $state is empty. */

function jet_sql_espape_string( $string ) {
    $temp = str_replace( "'", "''", $string );
    $temp = str_replace( "|", "'" . chr(124) . "'", $temp );

    return $temp;
}

Function Main_Menu () {
global $vanity_msg;
?>

<H1><CENTER><? echo $vanity_msg; ?></CENTER></H1>
<p><center><i>Words can be searched by entering<br>
a search term in the query box<br>
or click on a letter to display<br>
Words beginning with that letter<br>
or click on the add button to add a Word
</i></center></p>

<?
}


Function List_Contacts( $searchtype, $level, $un )
{
    global $vanity_msg, $hostname, $dbname, $tablename, $user, $password;

//    echo "\$searchtype = $searchtype<br>";
//    echo "\$level = $level<br>";

//    mysql_pconnect( $hostname, $user, $password );
    mysql_connect($hostname, $user, $password) OR DIE ("Unable to connect to database! Please try again later.");
    mysql_select_db($dbname);
	mysql_set_charset('utf8');

    if ( $searchtype == "level" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY level, BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY level, BINARY(word), BINARY(root), transliteration, wid";
        }
    }
    elseif ( $searchtype == "weight" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY weight DESC, BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY weight DESC, BINARY(word), BINARY(root), transliteration, wid";
        }
    }
    elseif ( $searchtype == "root" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY BINARY(root), BINARY(word), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY BINARY(root), BINARY(word), transliteration, wid";
        }
    }
    elseif ( $searchtype == "word" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
    }
    else {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
    }

    $title_msg = "List Words";

//    $result1 = mysql_db_query($dbname, $sql1);
//    $result = mysql_db_query( $dbname, $sql );
    $result = mysql_query($sql);
    $numrecs = mysql_numrows( $result );
    $recs = $numrecs;
?>

<table border="0" align="left" cellpadding="3" cellspacing="0" width="100%" cellspacing="0">
    <!-- row 1 -->
    <tr bgcolor="#ffffff">
        <td colspan="2">
            <center><font size="+1">
            <b><? echo $title_msg; ?></b>
            </font></center>
        </td>
    </tr>
    <!-- row 2 -->
    <tr bgcolor="#ffffff">
        <td colspan="2">
<?
    echo "<font size=\"-2\">";
    echo "<p>SQL: $sql</p>";
    echo "</font>";
    if ( !($numrecs) ) {
        echo "<center><p><b>No Matches Found</b></p></center>";
    }
?>
        </td>
    </tr>

<?
    if ( $numrecs < 1 ) {
        exit();
        return;
    }

    if ( $numrecs > 10000 ) {
        $recs = 10000;
    }
    else {
        $recs = $numrecs;
    }

?>
    <!-- row 3 -->
    <tr bgcolor="#ffffff">
        <td colspan="2">
            <font size="-1">
            <p><? echo $numrecs; ?> Record(s) Found, <? echo $recs; ?> Record(s) Displayed</p>
            </font>
        </td>
    </tr>
<?
    if ( $un == "guest" ) {
        echo( "Listing not allowed!" );

        exit();
        return;
    }
?>
    <!-- row 4 -->
    <tr>
        <td width="100%" bgcolor="#9999cc" colspan="2">&nbsp;</td>
    </tr>
<?

    $counter = 0;
    while ( $counter < $recs ) {
        $row = mysql_fetch_array($result);
?>
    <tr>
        <td bgcolor="#c8d8f8" colspan="2">
            <p><font size="-1">
            <b>Word ID:</b>&nbsp;<?echo $row['wid']; ?><br>
            <b>Transliteration:</b>&nbsp;<?echo $row['transliteration']; ?>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Kanji:</b>&nbsp;<font size="+1"><?echo $row['root']; ?></font>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Written:</b>&nbsp;<font size="+1"><?echo $row['written']; ?></font>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Word:</b>&nbsp;<font size="+1"><?echo $row['word']; ?></font><br>
            <b>Meaning:</b>&nbsp;<?echo $row['meaning']; ?>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>More Meanings:</b>&nbsp;<?echo $row['meaning2']; ?><br>
            <b>Language:</b>&nbsp;<?echo $row['language']; ?>&nbsp;&nbsp;
            <b>Part:</b>&nbsp;<?echo $row['part']; ?>&nbsp;&nbsp;
            <b>Gender:</b>&nbsp;<?echo $row['gender']; ?>&nbsp;&nbsp;
            <b>Number:</b>&nbsp;<?echo $row['number']; ?><br>
            <b>Weight:</b>&nbsp;
<?
    if ( $row['weight'] != "" ) {
?>
            <? echo $row['weight']; ?>
<?
    }
?>
            &nbsp;&nbsp;
            <b>Level:</b>&nbsp;
<?
    if ( $row['level'] != "" ) {
?>
            <? echo $row['level']; ?>
<?
    }
?>
            </font></p>
        </td>
    </tr>
    <tr>
        <td align="left" bgcolor="#c8d8f8">
            <font size="-1">
            <form method="post" action="contact.php">
                <input type="hidden" name="wid" value=<? echo $row['wid']; ?> >
                <input type="hidden" name="state" value="Update">
                <input type="submit" name="Edit1" value=" Edit ">
            </form>
            </font>
        </td>
        <td align="right" bgcolor="#c8d8f8">
            <font size="-1">
            <form method="post" action="contact.php">
                <input type="hidden" name="wid" value=<? echo $row['wid']; ?> >
                <input type="hidden" name="state" value="Delete">
                <input type="submit" name="Delete1" value="Delete">
            </form>
            </font>
        </td>
    </tr>
    <tr>
        <td width="100%" bgcolor="#9999cc" colspan="2">&nbsp;</td>
    </tr>

<?
        $counter++;
    }
?>
</table>
<?
    exit();
}


Function Export_Contacts( $searchtype, $level, $un )
{
    global $vanity_msg, $hostname, $dbname, $tablename, $user, $password;

//    echo "\$letter= $letter<br>";
//    echo "\$word= $word<br>";

//    mysql_pconnect( $hostname, $user, $password );
    mysql_connect($hostname, $user, $password) OR DIE ("Unable to connect to database! Please try again later.");
    mysql_select_db($dbname);
	mysql_set_charset('utf8');

    if ( $searchtype == "level" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY level, BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY level, BINARY(word), BINARY(root), transliteration, wid";
        }
    }
    elseif ( $searchtype == "weight" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY weight DESC, BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY weight DESC, BINARY(word), BINARY(root), transliteration, wid";
        }
    }
    elseif ( $searchtype == "root" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY BINARY(root), BINARY(word), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY BINARY(root), BINARY(word), transliteration, wid";
        }
    }
    elseif ( $searchtype == "word" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
    }
    else {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
    }

    $title_msg = "Export Words";

//    $result1 = mysql_db_query($dbname, $sql1);
//    $result = mysql_db_query( $dbname, $sql );
    $result = mysql_query($sql);
    $numrecs = mysql_numrows( $result );
    $recs = $numrecs;
?>

<table border="0" align="left" cellpadding="3" cellspacing="0" width="100%" cellspacing="0">
    <!-- row 1 -->
    <tr bgcolor="#ffffff">
        <td>
            <center><font size="+1">
            <b><? echo $title_msg; ?></b>
            </font></center>
        </td>
    </tr>
    <!-- row 2 -->
    <tr bgcolor="#ffffff">
        <td>
<?
    echo "<font size=\"-2\">";
    echo "<p>SQL: $sql</p>";
    echo "</font>";
    if ( !($numrecs) ) {
        echo "<center><p><b>No Matches Found</b></p></center>";
    }
?>
        </td>
    </tr>

<?
    if ( $numrecs < 1 ) {
        exit();
        return;
    }

    if ( $numrecs > 10000 ) {
        $recs = 10000;
    }
    else {
        $recs = $numrecs;
    }

?>
    <!-- row 3 -->
    <tr bgcolor="#ffffff">
        <td>
            <font size="-1">
            <p><? echo $numrecs; ?> Records Found, <? echo $recs; ?> Records Displayed</p>
            </font>
        </td>
    </tr>
<?
    if ( $un == "guest" ) {
        echo( "Exporting not allowed!" );

        exit();
        return;
    }
?>
    <!-- row 4 -->
    <tr>
        <td width="100%" bgcolor="#9999cc" colspan="2">&nbsp;</td>
    </tr>
    <tr>
        <td width="100%">
<?
    if ( 1 ) {
?>
"wid","transliteration","word","written","root","meaning","meaning2","language","part","gender","number","weight","level","encoding","exp"<br>
<?
    	$counter = 0;
    	while ( $counter < $recs ) {
        	$row = mysql_fetch_array($result);
?>
<?echo $row['wid']; ?>,"<?echo $row['transliteration']; ?>","<?echo $row['word']; ?>","<?echo $row['written']; ?>","<?echo $row['root']; ?>","<?echo $row['meaning']; ?>","<?echo $row['meaning2']; ?>","<?echo $row['language']; ?>","<?echo $row['part']; ?>","<?echo $row['gender']; ?>","<?echo $row['number']; ?>","<? echo $row['weight']; ?>","<? echo $row['level']; ?>","<? echo $row['encoding']; ?>","<? echo $row['exp']; ?>"<br>
<?
        	$counter++;
		}
    }
    else {
    	$counter = 0;
    	while ( $counter < $recs ) {
        	$row = mysql_fetch_array($result);
?>
INSERT INTO japanese ( wid, transliteration, word, written, root, meaning, meaning2, language, part, gender, [number], weight, [level], [encoding], exp ) VALUES (
<?echo $row['wid']; ?>,"<?echo jet_sql_espape_string( $row['transliteration'] ); ?>","<?echo $row['word']; ?>","<?echo $row['written']; ?>","<?echo $row['root']; ?>","<?echo jet_sql_espape_string( $row['meaning'] ); ?>","<?echo jet_sql_espape_string( $row['meaning2'] ); ?>","<?echo $row['language']; ?>","<?echo $row['part']; ?>","<?echo $row['gender']; ?>","<?echo $row['number']; ?>","<? echo $row['weight']; ?>","<? echo $row['level']; ?>","<? echo $row['encoding']; ?>","<? echo jet_sql_espape_string( $row['exp'] ); ?>"
 );<br>
<?
        	$counter++;
    	}
    }
?>
        </td>
    </tr>
</table>
<?
    exit();
}


Function Print_Contacts( $searchtype, $level, $un )
{
    global $vanity_msg, $hostname, $dbname, $tablename, $user, $password;

//    echo "\$letter= $letter<br>";
//    echo "\$word= $word<br>";

//    mysql_pconnect( $hostname, $user, $password );
    mysql_connect($hostname, $user, $password) OR DIE ("Unable to connect to database! Please try again later.");
    mysql_select_db($dbname);
	mysql_set_charset('utf8');

    if ( $searchtype == "level" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY level, BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY level, BINARY(word), BINARY(root), transliteration, wid";
        }
    }
    elseif ( $searchtype == "weight" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY weight DESC, BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY weight DESC, BINARY(word), BINARY(root), transliteration, wid";
        }
    }
    elseif ( $searchtype == "root" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY BINARY(root), BINARY(word), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY BINARY(root), BINARY(word), transliteration, wid";
        }
    }
    elseif ( $searchtype == "word" ) {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
    }
    else {
        if ( is_numeric( $level ) ) {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 AND level = $level ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid > 0 ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
    }

    $title_msg = "Print Words";

//    $result1 = mysql_db_query($dbname, $sql1);
//    $result = mysql_db_query( $dbname, $sql );
    $result = mysql_query($sql);
    $numrecs = mysql_numrows( $result );
    $recs = $numrecs;
?>

<table border="0" align="left" cellpadding="3" cellspacing="0" width="100%" cellspacing="0">
    <!-- row 1 -->
    <tr bgcolor="#ffffff">
        <td colspan="2">
            <center><font size="+1">
            <b><? echo $title_msg; ?></b>
            </font></center>
        </td>
    </tr>
    <!-- row 2 -->
    <tr bgcolor="#ffffff">
        <td colspan="2">
<?
    echo "<font size=\"-2\">";
    echo "<p>SQL: $sql</p>";
    echo "</font>";
    if ( !($numrecs) ) {
        echo "<center><p><b>No Matches Found</b></p></center>";
    }
?>
        </td>
    </tr>

<?
    if ( $numrecs < 1 ) {
        exit();
        return;
    }

    if ( $numrecs > 10000 ) {
        $recs = 10000;
    }
    else {
        $recs = $numrecs;
    }

?>
    <!-- row 3 -->
    <tr bgcolor="#ffffff">
        <td colspan="2">
            <font size="-1">
            <p><? echo $numrecs; ?> Records Found, <? echo $recs; ?> Records Displayed</p>
            </font>
        </td>
    </tr>
<?
    if ( $un == "guest" ) {
        echo( "Printing not allowed!" );

        exit();
        return;
    }
?>
    <!-- row 4 -->
    <tr>
        <td width="100%" colspan="2">&nbsp;</td>
    </tr>
<?

    $counter = 0;
    while ( $counter < $recs ) {
        $row = mysql_fetch_array($result);
?>
    <tr>
        <td colspan="2">
            <p><font size="-1">
<!--
            <b>Word ID:</b>&nbsp;<?echo $row['wid']; ?><br>
 -->
            <b>Transliteration:</b>&nbsp;<?echo $row['transliteration']; ?>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Kanji:</b>&nbsp;<font size="+1"><?echo $row['root']; ?></font>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Written:</b>&nbsp;<font size="+1"><?echo $row['written']; ?></font>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Word:</b>&nbsp;<font size="+1"><?echo $row['word']; ?></font><br>
            <b>Meaning:</b>&nbsp;<?echo $row['meaning']; ?>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>More Meanings:</b>&nbsp;<?echo $row['meaning2']; ?><br>
            <b>Language:</b>&nbsp;<?echo $row['language']; ?>&nbsp;&nbsp;
            <b>Part:</b>&nbsp;<?echo $row['part']; ?>&nbsp;&nbsp;
            <b>Gender:</b>&nbsp;<?echo $row['gender']; ?>&nbsp;&nbsp;
            <b>Number:</b>&nbsp;<?echo $row['number']; ?><br>
<!--
            <b>Weight:</b>&nbsp;
<?
    if ( $row['weight'] != "" ) {
?>
            <? echo $row['weight']; ?>
<?
    }
?>
            &nbsp;&nbsp;
            <b>Level:</b>&nbsp;
<?
    if ( $row['level'] != "" ) {
?>
            <? echo $row['level']; ?>
<?
    }
?>
 -->
            </font></p>
        </td>
    </tr>
    <tr>
        <td width="100%" colspan="2">&nbsp;</td>
    </tr>

<?
        $counter++;
    }
?>
</table>
<?
    exit();
}


Function List_Contacts_Alpha( $letter, $word, $wid, $searchtype )
{
    global $vanity_msg, $hostname, $dbname, $tablename, $user, $password;

//    echo "\$letter= $letter<br>";
//    echo "\$word= $word<br>";
//    echo "\$wid = $wid<br>";
//    echo "\$searchtype = $searchtype<br>";

//    mysql_pconnect( $hostname, $user, $password );
    mysql_connect($hostname, $user, $password) OR DIE ("Unable to connect to database in function List_Contacts_Alpha.");
    mysql_select_db($dbname);
	mysql_set_charset('utf8');

    // called from update or add
    if ( $letter == 'wid' ) {
        if ( $word != "" ) {
            $sql = "SELECT * FROM $tablename WHERE wid = $wid AND word LIKE BINARY '$word%' ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE wid = $wid ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
    }
    // is query on meaning or part?
    elseif ( $searchtype == "meaning" ) {
        if ( $word != "" ) {
            $sql = "SELECT * FROM $tablename WHERE (meaning LIKE '%$letter%' OR meaning2 LIKE '%$letter%') AND word LIKE BINARY '$word%' ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE (meaning LIKE '%$letter%' OR meaning2 LIKE '%$letter%') ORDER BY BINARY(word), BINARY(root), transliteration, wid";
        }
        $title_msg = $vanity_msg;
    }
    elseif ( $searchtype == "part" ) {
        if ( $letter != "" AND $word != "" ) {
            $sql = "SELECT * FROM $tablename WHERE part LIKE '$letter%' AND word LIKE BINARY '$word%' ORDER BY BINARY(word), BINARY(root), transliteration, wid";
            $title_msg = $vanity_msg;
        }
        elseif ( $transliteration != "" ) {
            $sql = "SELECT * FROM $tablename WHERE part LIKE '$letter%' AND transliteration LIKE BINARY '%$transliteration%' ORDER BY BINARY(word), BINARY(root), transliteration, wid";
            $title_msg = $vanity_msg;
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE part LIKE '$letter%' ORDER BY BINARY(word), BINARY(root), transliteration, wid";
            $title_msg = $vanity_msg;
        }
    }
    elseif ( $searchtype == "transliteration" ) {
        if ( $letter != "" AND $word != "" ) {
            $sql = "SELECT * FROM $tablename WHERE transliteration LIKE BINARY '%$letter%' AND word LIKE BINARY '$word%' ORDER BY BINARY(word), BINARY(root), transliteration, wid";
            $title_msg = $vanity_msg;
        }
        elseif ( $word != "" ) {
            $sql = "SELECT * FROM $tablename WHERE word LIKE BINARY '$word%' ORDER BY BINARY(word), BINARY(root), transliteration, wid";
            $title_msg = $vanity_msg;
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE transliteration LIKE BINARY '%$letter%' ORDER BY BINARY(word), BINARY(root), transliteration, wid";
            $title_msg = $vanity_msg;
        }
    }
    else {
        if ( $letter != "" AND $word != "" ) {
            $sql = "SELECT * FROM $tablename WHERE transliteration LIKE BINARY '$letter%' AND word LIKE BINARY '$word%' ORDER BY BINARY(word), BINARY(root), transliteration, wid";
            $title_msg = $vanity_msg;
        }
        elseif ( $word != "" ) {
            $sql = "SELECT * FROM $tablename WHERE word LIKE BINARY '$word%' ORDER BY BINARY(word), BINARY(root), transliteration, wid";
            $title_msg = $vanity_msg;
        }
        else {
            $sql = "SELECT * FROM $tablename WHERE word LIKE BINARY '$letter%' ORDER BY BINARY(word), BINARY(root), transliteration, wid";
            $title_msg = $vanity_msg;
        }
    }

    if ( $searchtype == "add" ) {
        $title_msg = "Word Added";
    }
    elseif ( $searchtype == "update" ) {
        $title_msg = "Word Updated";
    }

//    $result1 = mysql_db_query($dbname, $sql1);
    $result = mysql_db_query( $dbname, $sql );
    $numrecs = mysql_numrows( $result );
    $recs = $numrecs;
?>

<p>
<table border="0" cellpadding="3" cellspacing="0" width="100%" cellspacing="0">
    <!-- row 1 -->
    <tr bgcolor="#ffffff">
        <td colspan="4">
            <center><font size="+1">
            <b><? echo $title_msg; ?></b>
            </font></center>
        </td>
    </tr>
    <!-- row 2 -->
    <tr bgcolor="#ffffff">
        <td colspan="4">
<?
    echo "<font size=\"-2\">";
    echo "<p>SQL: $sql</p>";
    echo "</font>";
    if ( !($numrecs) ) {
        echo "<center><p><b>No Matches Found</b></p></center>";
    }
?>
        </td>
    </tr>

<?
    if ( $numrecs < 1 ) {
//        exit();
        return;
    }

    if ( $numrecs > 1000 ) {
        $recs = 1000;
    }
    else {
        $recs = $numrecs;
    }

    // suppress records found msg when displaying add or update record
    if ( !( ( $searchtype == "update" ) || ( $searchtype == "add" ) ) ) {
?>
    <!-- row 3 -->
    <tr bgcolor="#ffffff">
        <td colspan="4">
            <font size="-1">
            <p><? echo $numrecs; ?> Record(s) Found, <? echo $recs; ?> Record(s) Displayed</p>
            </font>
        </td>
    </tr>
<?
    }

?>
    <!-- row 4 -->
    <tr>
        <td width="100%" bgcolor="#9999cc" colspan="4"><font size="-2">&nbsp;</font></td>
    </tr>
<?
    $counter = 0;

    while ( $counter < $recs ) {
        $row = mysql_fetch_array($result);

        if ( $row['language'] != "" ) {
            unset( $cwid );

            $word = $row["wid"];
            $lang = $row["language"];
            $sql = "SELECT * FROM word_connect WHERE language = '$lang' AND wid = $word ORDER BY wid";

            $result2 = mysql( $dbname, $sql );

            $i = 1;

            while ($link = mysql_fetch_array($result2, MYSQL_ASSOC)) {
                $cwid[$i] = $link["cwid"];
                $clang[$i] = $link["clanguage"];

//                printf( "<p>wid: %s, lang: %s </p>", $cwid[$i], $clang[$i] );

                $i++;
            }

        }
?>
    <tr>
        <td bgcolor="#c8d8f8" colspan="4">
            <p><font size="-1">
            <b>Word ID:</b>&nbsp;<?echo $row['wid']; ?><br>
            <b>Transliteration:</b>&nbsp;<?echo $row['transliteration']; ?>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Kanji:</b>&nbsp;<font size="+1"><?echo $row['root']; ?></font>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Written:</b>&nbsp;<font size="+1"><?echo $row['written']; ?></font>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Word:</b>&nbsp;<font size="+1"><?echo $row['word']; ?></font><br>
            <b>Meaning:</b>&nbsp;<?echo $row['meaning']; ?>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>More Meanings:</b>&nbsp;<?echo $row['meaning2']; ?><br>
            <b>Language:</b>&nbsp;<?echo $row['language']; ?>&nbsp;&nbsp;
            <b>Part:</b>&nbsp;<?echo $row['part']; ?>&nbsp;&nbsp;
            <b>Gender:</b>&nbsp;<?echo $row['gender']; ?>&nbsp;&nbsp;
            <b>Number:</b>&nbsp;<?echo $row['number']; ?><br>
            <b>Weight:</b>&nbsp;
<?
    if ( $row['weight'] != "" ) {
?>
            <? echo $row['weight']; ?>
<?
    }
?>
            &nbsp;&nbsp;
            <b>Level:</b>&nbsp;
<?
    if ( $row['level'] != "" ) {
?>
            <? echo $row['level']; ?>
<?
    }
?>
            <br>
            <b>Explanation:</b>&nbsp;<br>
<?
    if ( $row['exp'] != "" ) {
?>
            <? echo $row['exp']; ?>
<?
    }
?>
            </font></p>
        </td>
    </tr>
<?
//    if ( ($cwid[1] != "") or ($cwid[2] != "") ) {
?>
    <tr bgcolor="#c8d8f8">
        <td width="15%">
<?
        if ( $cwid[1] != "" ) {
?>
            <font size="-2">
            link #1 lang: <? echo $clang[1]; ?>
            </font>
<?
        }
        else {
            echo( "&nbsp;" );
        }
?>
        </td>
        <td width="35%">
<?
        if ( $cwid[1] != "" ) {
?>
            <font size="-2">
            wid: <? echo $cwid[1]; ?>&nbsp;&nbsp;
            <a href="/phpWords/list.php?wid=<? echo $cwid[1]; ?>" target="_blank">list.php?wid=<? echo $cwid[1]; ?></a>
            </font>
<?
        }
        else {
            echo( "&nbsp;" );
        }
?>
        </td>
        <td width="15%">
<?
        if ( $cwid[2] != "" ) {
?>
            <font size="-2">
            link #2 lang: <? echo $clang[2]; ?>
            </font>
<?
        }
        else {
            echo( "&nbsp;" );
        }
?>
        </td>
        <td width="35%">
<?
        if ( $cwid[2] != "" ) {
?>
            <font size="-2">
            wid: <? echo $cwid[2]; ?>&nbsp;&nbsp;
            <a href="/phpWords/list.php?wid=<? echo $cwid[2]; ?>" target="_blank">list.php?wid=<? echo $cwid[2]; ?></a>
            </font>
<?
        }
        else {
            echo( "&nbsp;" );
        }
?>
        </td>
    </tr>
<?
//    }
?>
<?
    if ( ($cwid[3] != "") or ($cwid[4] != "") ) {
?>
    <tr bgcolor="#c8d8f8">
        <td>
<?
        if ( $cwid[3] != "" ) {
?>
            <font size="-2">
            link #3 lang: <? echo $clang[3]; ?>
            </font>
<?
        }
        else {
            echo( "&nbsp;" );
        }
?>
        </td>
        <td>
<?
        if ( $cwid[3] != "" ) {
?>
            <font size="-2">
            wid: <? echo $cwid[3]; ?>&nbsp;&nbsp;
            <a href="/phpWords/list.php?wid=<? echo $cwid[3]; ?>" target="_blank">list.php?wid=<? echo $cwid[3]; ?></a>
            </font>
<?
        }
        else {
            echo( "&nbsp;" );
        }
?>
        </td>
        <td>
<?
        if ( $cwid[4] != "" ) {
?>
            <font size="-2">
            link #4 lang: <? echo $clang[4]; ?>
            </font>
<?
        }
        else {
            echo( "&nbsp;" );
        }
?>
        </td>
        <td>
<?
        if ( $cwid[4] != "" ) {
?>
            <font size="-2">
            wid: <? echo $cwid[4]; ?>&nbsp;&nbsp;
            <a href="/phpWords/list.php?wid=<? echo $cwid[4]; ?>" target="_blank">list.php?wid=<? echo $cwid[4]; ?></a>
            </font>
<?
        }
        else {
            echo( "&nbsp;" );
        }
?>
        </td>
    </tr>
<?
    }

    if ( $cwid[5] != "" ) {
?>
    <tr bgcolor="#c8d8f8">
        <td>
            <font size="-2">
            link #5 lang: <? echo $clang[5]; ?>
            </font>
        </td>
        <td>
            <font size="-2">
            wid: <? echo $cwid[5]; ?>&nbsp;&nbsp;
            <a href="/phpWords/list.php?wid=<? echo $cwid[5]; ?>" target="_blank">list.php?wid=<? echo $cwid[5]; ?></a>
            </font>
        </td>
        <td colspan="2">
            &nbsp;
        </td>
    </tr>
<?
    }
?>
    <tr bgcolor="#c8d8f8">
        <td colspan="4">
            <p><font size="-1">
            <p><b>Last Updated:</b>&nbsp;<?echo $row['datelast']; ?>&nbsp;&nbsp;
            <b>By Username:</b>&nbsp;<?echo $row['username']; ?>
            </font></p>
        </td>
    </tr>
    <tr bgcolor="#c8d8f8">
        <td align="left">
            <font size="-1">
            <form method="post" action="contact.php">
                <input type="hidden" name="wid" value="<? echo $row['wid']; ?>" >
                <input type="hidden" name="state" value="Update">
                <input type="submit" name="Edit1" value=" Edit ">
            </form>
            </font>
        </td>
        <td align="right">
            <font size="-1">
            <form method="post" action="contact.php">
                <input type="hidden" name="wid" value="<? echo $row['wid']; ?>" >
                <input type="hidden" name="state" value="Copy_Contact">
                <input type="submit" name="Copy1" value="Copy">
            </form>
            </font>
        </td>
        <td>&nbsp;</td>
        <td align="right">
            <font size="-1">
            <form method="post" action="contact.php">
                <input type="hidden" name="wid" value="<? echo $row['wid']; ?>" >
                <input type="hidden" name="state" value="Delete">
                <input type="submit" name="Delete1" value="Delete">
            </form>
            </font>
        </td>
    </tr>
    <tr>
        <td width="100%" bgcolor="#9999cc" colspan="4"><font size="-2">&nbsp;</font></td>
    </tr>

<?
        $counter++;
    }
?>
</table>
</p>

<?
//exit();
}


Function List_Contact_Delete( $wid, $un )
{
    global $vanity_msg, $hostname, $dbname, $tablename, $user, $password;

//    echo "\$letter= $letter<br>";
//    echo "\$word= $word<br>";

//    mysql_pconnect( $hostname, $user, $password );
    mysql_connect($hostname, $user, $password) OR DIE ("Unable to connect to database in function List_Contacts_Alpha.");
    mysql_select_db($dbname);
	mysql_set_charset('utf8');

    // called from update or add
    $sql = "SELECT * FROM $tablename WHERE wid = $wid ORDER BY BINARY(word), BINARY(root), transliteration, wid";

//    $result1 = mysql_db_query($dbname, $sql1);
    $result = mysql_db_query( $dbname, $sql );
    $numrecs = mysql_numrows( $result );
    $recs = $numrecs;
?>

<p>
<table border="0" cellpadding="3" cellspacing="0" width="100%" cellspacing="0">
    <!-- row 1 -->
    <tr bgcolor="#ffffff">
        <td colspan="4">
            <center><font size="+1">
            <b><? echo $title_msg; ?></b>
            </font></center>
        </td>
    </tr>
    <!-- row 2 -->
    <tr bgcolor="#ffffff">
        <td colspan="4">
<?
    echo "<font size=\"-2\">";
    echo "<p>SQL: $sql</p>";
    echo "</font>";
    if ( !($numrecs) ) {
        echo "<center><p><b>No Matches Found</b></p></center>";
    }
?>
        </td>
    </tr>

<?
    if ( $numrecs < 1 ) {
//        exit();
        return;
    }

    if ( $numrecs > 1000 ) {
        $recs = 1000;
    }
    else {
        $recs = $numrecs;
    }
?>
    <!-- row 4 -->
    <tr>
        <td width="100%" bgcolor="#9999cc" colspan="4"><font size="-2">&nbsp;</font></td>
    </tr>
<?
    $counter = 0;

    while ( $counter < $recs ) {
        $row = mysql_fetch_array($result);

        if ( $row['language'] != "" ) {
            unset( $cwid );

            $word = $row["wid"];
            $lang = $row["language"];
            $sql = "SELECT * FROM word_connect WHERE language = '$lang' AND wid = $word ORDER BY wid";

            $result2 = mysql( $dbname, $sql );

            $i = 1;

            while ($link = mysql_fetch_array($result2, MYSQL_ASSOC)) {
                $cwid[$i] = $link["cwid"];
                $clang[$i] = $link["clanguage"];

//                printf( "<p>wid: %s, lang: %s </p>", $cwid[$i], $clang[$i] );

                $i++;
            }

        }
?>
    <tr>
        <td bgcolor="#c8d8f8" colspan="4">
            <p><font size="-1">
            <b>Word ID:</b>&nbsp;<?echo $row['wid']; ?><br>
            <b>Transliteration:</b>&nbsp;<?echo $row['transliteration']; ?>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Kanji:</b>&nbsp;<font size="+1"><?echo $row['root']; ?></font>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Written:</b>&nbsp;<font size="+1"><?echo $row['written']; ?></font>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>Word:</b>&nbsp;<font size="+1"><?echo $row['word']; ?></font><br>
            <b>Meaning:</b>&nbsp;<?echo $row['meaning']; ?>&nbsp;&nbsp;&nbsp;&nbsp;
            <b>More Meanings:</b>&nbsp;<?echo $row['meaning2']; ?><br>
            <b>Language:</b>&nbsp;<?echo $row['language']; ?>&nbsp;&nbsp;
            <b>Part:</b>&nbsp;<?echo $row['part']; ?>&nbsp;&nbsp;
            <b>Gender:</b>&nbsp;<?echo $row['gender']; ?>&nbsp;&nbsp;
            <b>Number:</b>&nbsp;<?echo $row['number']; ?><br>
            <b>Weight:</b>&nbsp;
<?
    if ( $row['weight'] != "" ) {
?>
            <? echo $row['weight']; ?>
<?
    }
?>
            &nbsp;&nbsp;
            <b>Level:</b>&nbsp;
<?
    if ( $row['level'] != "" ) {
?>
            <? echo $row['level']; ?>
<?
    }
?>
            <br>
        </td>
    </tr>
    <tr bgcolor="#c8d8f8">
        <td colspan="4">
            <p><font size="-1">
            <p><b>Last Updated:</b>&nbsp;<?echo $row['datelast']; ?>&nbsp;&nbsp;
            <b>By Username:</b>&nbsp;<?echo $row['username']; ?>
            </font></p>
        </td>
    </tr>
    <tr bgcolor="#c8d8f8">
        <td align="left">
            <font size="-1">
            <form method="post" action="contact.php">
                <input type="hidden" name="wid" value="<? echo $row['wid']; ?>" >
                <input type="hidden" name="state" value="Update">
                <input type="submit" name="Edit1" value=" Edit ">
            </form>
            </font>
        </td>
        <td align="right" valign="top">
            &nbsp;Confirm?
        </td>
        <td>&nbsp;</td>
        <td align="right">
            <font size="-1">
            <form method="post" action="contact.php">
                <input type="hidden" name="wid" value="<? echo $row['wid']; ?>" >
                <input type="hidden" name="state" value="Confirm_Delete">
                <input type="submit" name="Delete2" value="Delete">
            </form>
            </font>
        </td>
    </tr>
    <tr>
        <td width="100%" bgcolor="#9999cc" colspan="4"><font size="-2">&nbsp;</font></td>
    </tr>

<?
        $counter++;
    }
?>
</table>
</p>

<?
//exit();
}


/* Save updates to database. Call List_Contacts_Alpha to display new information for updated contact entry. */

Function Commit_Update( $wid, $tr, $wd, $wr, $gd, $nm, $mn, $m2, $lg, $pt, $rt, $ex, $wt, $lv, $un, $cl, $cw, $lw, $dw )
{
    global $hostname, $dbname, $tablename, $user, $password;

    //echo "fc_u<br>";
    //echo "fn=$fn, ln=$ln, wid=$wid<br>";
//    mysql_pconnect($hostname,$user,$password) or die ("Connect Failed in function Commit Update");
    mysql_connect($hostname, $user, $password) OR DIE ("Unable to connect to database in function Commit_Update.");
    mysql_select_db($dbname);
	mysql_set_charset('utf8');

	if ( $wt == "" ) {
		$wt = "0";
	}

	if ( $lv == "" ) {
		$lv = "0";
	}

    $sql = "UPDATE $tablename SET transliteration = '$tr', word = '$wd', written = '$wr', gender = '$gd', number = '$nm', meaning ='$mn', meaning2 ='$m2', language = '$lg', part = '$pt', root = '$rt', weight='$wt', level='$lv', encoding='UTF-8', username = '$un', datelast = NOW(), exp = '$ex' WHERE wid = $wid";
    if ( $un != "guest" ) {
        $result = mysql_db_query( $dbname, $sql )
            or die( "Update failed in function Commit_Update, sql = '$sql'" );
    }
    else {
        echo( "Updating not allowed!" );

        $result = 0;
    }

    if ( $un != "guest" ) {
        $i = 1;

        foreach ( $cw as $cwid ) {
            if ( $cwid >= 0 ) {
                if ( $cwid > 0 and $lw[$i] > 0 and $cl[$i] != "" ) {
                    $sql = "UPDATE word_connect SET cwid = $cwid, clanguage = '$cl[$i]' WHERE wid = $wid AND language = '$lg' AND cwid = $lw[$i]";

//                    echo "<p> sql: $sql </p>\n";

                    $result = mysql_db_query( $dbname, $sql )
                        or die( "Update failed in function Commit_Update, sql = '$sql'" );
                }
                elseif ( $cwid != "" and $cwid == 0 ) {
                    $sql = "DELETE FROM word_connect WHERE wid = $wid AND language = '$lg' AND cwid = $lw[$i]";

//                    echo "<p> sql: $sql </p>\n";

                    $result = mysql_db_query( $dbname, $sql )
                        or die( "Update failed in function Commit_Update, sql = '$sql'" );
                }
                elseif ( $cl[$i] != "" ) {
                    $sql = "INSERT INTO word_connect ( wid, language, cwid, clanguage ) VALUES ( '$wid', '$lg', '$cwid', '$cl[$i]' )";

//                    echo "<p> sql: $sql </p>\n";

                    $result = mysql_db_query( $dbname, $sql )
                        or die( "Update failed in function Commit_Update, sql = '$sql'" );
                }
            }

            $i++;
        }
    }

    if ( $result ) {
        $Form_Msg ="Record Updated";
    }
    else {
        $Form_Msg ="Update Failed!";
    }

    $searchtype = "update"; // flag to list
    $letter = "wid";
    List_Contacts_Alpha( $letter, $word, $wid, $searchtype );

} // end of function Commit_Update


/* Save new contact information to database. Call List_Contacts_Alpha to display new contact entry. */

Function Commit_Contact( $tr, $wd, $wr, $gd, $nm, $mn, $m2, $lg, $pt, $rt, $ex, $wt, $lv, $un, $cl, $cw, $dw )
{
    global $hostname, $dbname, $tablename, $user, $password;

//    mysql_pconnect( $hostname,$user,$password )
//        or die ("DB connect failed in function Commit_Contact");
    mysql_connect($hostname, $user, $password) OR DIE ("Unable to connect to database in function Commit_Contact.");
    mysql_select_db($dbname);
	mysql_set_charset('utf8');

	if ( $wt == "" ) {
		$wt = "0";
	}

	if ( $lv == "" ) {
		$lv = "0";
	}

    $sql = "INSERT INTO $tablename ( transliteration, word, written, gender, number, meaning, meaning2, language, part, root, weight, level, encoding, username, datelast, exp ) VALUES ( '$tr', '$wd', '$wr', '$gd', '$nm', '$mn', '$m2', '$lg', '$pt', '$rt', '$wt', '$lv', 'UTF-8', '$un', NOW(), '$ex' )";
    if ( $un != "guest" ) {
        $result = mysql_db_query( $dbname, $sql )
            or die ("Insert failed in function Commit_Contact, sql = '$sql'");

        $new_wid = mysql_insert_id();
    }
    else {
        echo( "Inserting not allowed!" );

        $result = 0;
    }

    if ( $un != "guest" ) {
        $i = 1;

        foreach ( $cw as $cwid ) {
            if ( $cwid >= 0 ) {
                if ( $cwid > 0 and $lw[$i] > 0 and $cl[$i] != "" ) {
                    $sql = "UPDATE word_connect SET cwid = $cwid, clanguage = '$cl[$i]' WHERE wid = $wid AND language = '$lg' AND cwid = $lw[$i]";

//                    echo "<p> sql: $sql </p>\n";

                    $result = mysql_db_query( $dbname, $sql )
                        or die( "Update failed in function Commit_Update, sql = '$sql'" );
                }
                elseif ( $cwid != "" and $cwid == 0 ) {
                    $sql = "DELETE FROM word_connect WHERE wid = $wid AND language = '$lg' AND cwid = $lw[$i]";

//                    echo "<p> sql: $sql </p>\n";

                    $result = mysql_db_query( $dbname, $sql )
                        or die( "Update failed in function Commit_Update, sql = '$sql'" );
                }
                elseif ( $cl[$i] != "" ) {
                    $sql = "INSERT INTO word_connect ( wid, language, cwid, clanguage ) VALUES ( '$wid', '$lg', '$cwid', '$cl[$i]' )";

//                    echo "<p> sql: $sql </p>\n";

                    $result = mysql_db_query( $dbname, $sql )
                        or die( "Update failed in function Commit_Update, sql = '$sql'" );
                }
            }

            $i++;
        }
    }

    if ( $result ) {
        $Form_Msg ="Record Inserted";
    } else {
        $Form_Msg ="Insert Failed!";
    }

    $letter = "wid";
    $searchtype = "add";
    List_Contacts_Alpha( $letter, $word, $new_wid, $searchtype );

//    exit();
}


Function Copy_Contact( $wid, $username )
{
    global $hostname, $dbname, $tablename, $user, $password, $new_wid;

    mysql_connect($hostname, $user, $password) OR DIE ("Unable to connect to database in function Copy_Contact.");
    mysql_select_db($dbname);
	mysql_set_charset('utf8');

    $result = mysql( $dbname, "SELECT * FROM $tablename WHERE wid = $wid ORDER BY BINARY(word), BINARY(root), transliteration, wid" );
    $row = mysql_fetch_array($result);

    $tr = str_replace( "'", "\\'", $row['transliteration'] );
    $mn = str_replace( "'", "\\'", $row['meaning'] );
    $m2 = str_replace( "'", "\\'", $row['meaning2'] );
    $ex = str_replace( "'", "\\'", $row['exp'] );

	$wt = $row['weight'];
	if ( $wt == "" ) {
		$wt = "0";
	}

	$lv = $row['level'];
	if ( $lv == "" ) {
		$lv = "0";
	}

    $sql = "INSERT INTO $tablename ( transliteration, word, written, gender, number, meaning, meaning2, language, part, root, weight, level, encoding, username, datelast, exp ) VALUES ( '" .
        $tr . "', '" . $row['word'] . "', '" . $row['written'] . "', '" . $row['gender'] . "', '" .
        $row['number'] . "', '" . $mn . "', '" . $m2 . "', '" . $row['language'] . "', '" .
        $row['part'] . "', '" . $row['root'] . "', '" . $wt . "', '" . $lv . "', 'UTF-8', '" . $username . "', NOW(), '" . $ex . "' )";
    if ( $username != "guest" ) {
        $result = mysql_db_query( $dbname, $sql )
            or die ("DB Insert failed in function Copy_Contact, sql = '$sql'");

        $new_wid = mysql_insert_id();
    }
    else {
        echo( "Copying not allowed!" );

        $result = 0;
    }

    if ($result) {
        $Form_Msg ="Record Inserted";
    } else {
        $Form_Msg ="Insert Failed!";
    }

//    $letter = "wid";
//    $searchtype = "add";
//    List_Contacts_Alpha( $letter, $word, $new_wid, $searchtype );

//    exit();
}


Function Backup_Contact( $wid, $username )
{
    global $hostname, $dbname, $tablename, $user, $password, $new_wid;

    mysql_connect($hostname, $user, $password) OR DIE ("Unable to connect to database in function Backup_Contact.");
    mysql_select_db($dbname);
	mysql_set_charset('utf8');

    $result = mysql( $dbname, "SELECT * FROM $tablename WHERE wid = $wid ORDER BY BINARY(word), BINARY(root), transliteration, wid" );
    $row = mysql_fetch_array($result);

    $tr = str_replace( "'", "\\'", $row['transliteration'] );
    $mn = str_replace( "'", "\\'", $row['meaning'] );
    $m2 = str_replace( "'", "\\'", $row['meaning2'] );
    $ex = str_replace( "'", "\\'", $row['exp'] );

	$wt = $row['weight'];
	if ( $wt == "" ) {
		$wt = "0";
	}

	$lv = $row['level'];
	if ( $lv == "" ) {
		$lv = "0";
	}

    $sql = "INSERT INTO test_history ( wid, transliteration, word, written, gender, number, meaning, meaning2, language, part, root, weight, level, encoding, username, datelast, exp ) VALUES ( '" .
        $wid . "', '" . $tr . "', '" . $row['word'] . "', '" . $row['written'] . "', '" . $row['gender'] . "', '" .
        $row['number'] . "', '" . $mn . "', '" . $m2 . "', '" . $row['language'] . "', '" .
        $row['part'] . "', '" . $row['root'] . "', '" . $wt . "', '" . $lv . "', 'UTF-8', '" . $username . "', NOW(), '" . $ex . "' )";
    if ( $username != "guest" ) {
        $result = mysql_db_query( $dbname, $sql )
            or die ("DB Insert failed in function Backup_Contact, sql = '$sql'");

//        $new_wid = mysql_insert_id();
    }
    else {
        echo( "Backing up not allowed!" );

        $result = 0;
    }

    if ($result) {
        $Form_Msg ="Record Backed up";
    } else {
        $Form_Msg ="Backup Failed!";
    }

//    $letter = "wid";
//    $searchtype = "add";
//    List_Contacts_Alpha( $letter, $word, $new_wid, $searchtype );

//    exit();
}


/* This function displays the contact information in a form. It is used for both the add and update contact options. */

Function Contact_Form( $state, $wid ) {
    global $hostname, $dbname, $tablename, $user, $password;

//    print "<p>state = $state </p>\n";
//    print "<p>wid = $wid </p>\n";

    if ( $state == "Update" ) {
        $title_msg = "Update Word";
        $state = "Commit_Update";
//        mysql_pconnect( $hostname, $user, $password )
//            or die ("DB connect failed in function Commit_Contact");
        mysql_connect($hostname, $user, $password) OR DIE ("Unable to connect to database in function Contact_Form.");
        mysql_select_db($dbname);
		mysql_set_charset('utf8');

        $result = mysql( $dbname, "SELECT * FROM $tablename WHERE wid = $wid ORDER BY BINARY(word), BINARY(root), transliteration, wid" );
        $row = mysql_fetch_array($result);

        if ( $row['language'] != "" ) {
            unset( $cwid );

            $word = $row["wid"];
            $lang = $row["language"];
            $sql = "SELECT * FROM word_connect WHERE language = '$lang' AND wid = $word ORDER BY wid";

            $result2 = mysql( $dbname, $sql );

            $i = 1;

            while ($link = mysql_fetch_array($result2, MYSQL_ASSOC)) {
                $cwid[$i] = $link["cwid"];
                $clang[$i] = $link["clanguage"];

//                printf( "<p>wid: %s, lang: %s </p>", $cwid[$i], $clang[$i] );

                $i++;
            }

        }
    }
    else {
        $title_msg = "Add Word";
        $state = "Commit_Contact";
    }
?>

<font size="+1"><p align="center"><b><? echo $title_msg; ?></b></p></font>
<!-- <font size="-1"><hr></font> -->
<p>
<form method="post" action="contact.php">
<table border="0" cellpadding="3" cellspacing="0" width="100%" cellspacing="0">
    <tr>
        <td colspan="4">
            <font size="-2"><p>Word ID: <? echo $row['wid']; ?></p></font>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <font size="-2"><p>Transliteration</p></font>
        </td>
        <td colspan="2">
            <font size="-2"><p>Word</p></font>
        </td>
    <tr>
    <tr>
        <td colspan="2">
            <input type="text" name="transliteration" size="26" maxlength="100" value="<? echo $row['transliteration']; ?>" >
        </td>
        <td colspan="2">
            <font size="+1"><input type="text" name="word" size="22" maxlength="100" style="direction: ltr; font-size: larger" value="<? echo $row['word']; ?>" ></font>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <font size="-2"><p>Kanji</p></font>
        </td>
        <td colspan="2">
            <font size="-2"><p>Written</p></font>
        </td>
    <tr>
    <tr>
        <td colspan="2">
            <font size="+1"><input type="text" name="root" size="22" maxlength="25" style="direction: ltr; font-size: larger" value="<? echo $row['root']; ?>" ></font>
        </td>
        <td colspan="2">
            <font size="+1"><input type="text" name="written" size="22" maxlength="100" style="direction: ltr; font-size: larger" value="<? echo $row['written']; ?>" ></font>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <font size="-2"><p>Meaning</p></font>
        </td>
        <td colspan="2">
            <font size="-2"><p>More Meanings</p></font>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="text" name="meaning" size="50" maxlength="100" value="<? echo $row['meaning']; ?>" >
        </td>
        <td colspan="2">
            <input type="text" name="meaning2" size="50" maxlength="250" value="<? echo $row['meaning2']; ?>" >
        </td>
    </tr>
    <tr>
        <td>
            <font size="-2"><p>Language</p></font>
        </td>
        <td>
            <font size="-2"><p>Part</p></font>
        </td>
        <td>
            <font size="-2"><p>Gender</p></font>
        </td>
        <td>
            <font size="-2"><p>Number</p></font>
        </td>
    </tr>
    <tr>
        <td>
            <input type="text" name="tlanguage" size="10" maxlength="10" value="<? if ( $row['language'] != "" ) { echo( $row['language'] ); } else { echo( "japanese" ); } ?>" >
        </td>
        <td>
            <input type="text" name="part" size="10" maxlength="20" value="<? echo $row['part']; ?>" >
        </td>
        <td>
            <input type="text" name="gender" size="3" maxlength="2" value="<? echo $row['gender']; ?>" >
        </td>
        <td>
            <input type="text" name="number" size="4" maxlength="3" value="<? echo $row['number']; ?>"  >
        </td>
    </tr>
    <tr>
        <td align="right">
            <font size="-2"><p>Weight:</p></font>
        </td>
        <td>
            <input type="text" name="weight" size="4" maxlength="3" value="<? echo $row['weight']; ?>" >
        </td>
        <td align="right">
            <font size="-2"><p>Level:</p></font>
        </td>
        <td>
            <input type="text" name="level" size="4" maxlength="3" value="<? echo $row['level']; ?>" >
        </td>
    </tr>
    <tr>
        <td colspan="4">
            <font size="-2"><p>Explanation:<br></font>
<textarea name="exp" rows="3" cols="80" wrap="virtual" dir="ltr" style="direction: ltr">
<? echo $row['exp']; ?>
</textarea>
            </p>
        </td>
    </tr>
    <tr>
        <td width="18%">
            <font size="-2">
            link #1 lang: <input type="text" name="clang1" size="4" maxlength="10" value="<? echo $clang[1]; ?>" >
            </font>
        </td>
        <td width="32%">
            <font size="-2">
            wid: <input type="text" name="cwid1" size="4" maxlength="5" value="<? echo $cwid[1]; ?>" >
<?
    if ( $cwid[1] != "" ) {
?>
            <a href="/phpWords/list.php?wid=<? echo $cwid[1]; ?>" target="_blank">list.php?wid=<? echo $cwid[1]; ?></a>
            <!--del: <input type="checkbox" name="dwid1" value="1"> -->
<?
    }
?>
            <input type="hidden" name="lwid1" value="<? if ( $cwid[1] > 0 ) { echo( $cwid[1] ); } ?>" >
            </font>
        </td>
        <td width="18%">
            <font size="-2">
            link #4 lang: <input type="text" name="clang4" size="4" maxlength="10" value="<? echo $clang[4]; ?>" >
            </font>
        </td>
        <td width="32%">
            <font size="-2">
            wid: <input type="text" name="cwid4" size="4" maxlength="5" value="<? echo $cwid[4]; ?>" >
<?
    if ( $cwid[4] != "" ) {
?>
            <a href="/phpWords/list.php?wid=<? echo $cwid[4]; ?>" target="_blank">list.php?wid=<? echo $cwid[4]; ?></a>
            <!--del: <input type="checkbox" name="dwid4" value="1"> -->
<?
    }
?>
            <input type="hidden" name="lwid4" value="<? if ( $cwid[4] > 0 ) { echo( $cwid[4] ); } ?>" >
            </font>
        </td>
    </tr>
    <tr>
        <td>
            <font size="-2">
            link #2 lang: <input type="text" name="clang2" size="4" maxlength="10" value="<? echo $clang[2]; ?>" >
            </font>
        </td>
        <td>
            <font size="-2">
            wid: <input type="text" name="cwid2" size="4" maxlength="5" value="<? echo $cwid[2]; ?>" >
<?
    if ( $cwid[2] != "" ) {
?>
            <a href="/phpWords/list.php?wid=<? echo $cwid[2]; ?>" target="_blank">list.php?wid=<? echo $cwid[2]; ?></a>
            <!--del: <input type="checkbox" name="dwid2" value="1"> -->
<?
    }
?>
            <input type="hidden" name="lwid2" value="<? if ( $cwid[2] > 0 ) { echo( $cwid[2] ); } ?>" >
            </font>
        </td>
        <td>
            <font size="-2">
            link #5 lang: <input type="text" name="clang5" size="4" maxlength="10" value="<? echo $clang[5]; ?>" >
            </font>
        </td>
        <td>
            <font size="-2">
            wid: <input type="text" name="cwid5" size="4" maxlength="5" value="<? echo $cwid[5]; ?>" >
<?
    if ( $cwid[5] != "" ) {
?>
            <a href="/phpWords/list.php?wid=<? echo $cwid[5]; ?>" target="_blank">list.php?wid=<? echo $cwid[5]; ?></a>
            <!--del: <input type="checkbox" name="dwid5" value="1" /> -->
<?
    }
?>
            <input type="hidden" name="lwid5" value="<? if ( $cwid[5] > 0 ) { echo( $cwid[5] ); } ?>" >
            </font>
        </td>
    </tr>
    <tr>
        <td>
            <font size="-2">
            link #3 lang: <input type="text" name="clang3" size="4" maxlength="10" value="<? echo $clang[3]; ?>" >
            </font>
        </td>
        <td>
            <font size="-2">
            wid: <input type="text" name="cwid3" size="4" maxlength="5" value="<? echo $cwid[3]; ?>" >
<?
    if ( $cwid[3] != "" ) {
?>
            <a href="/phpWords/list.php?wid=<? echo $cwid[3]; ?>" target="_blank">list.php?wid=<? echo $cwid[3]; ?></a>
            <!--del: <input type="checkbox" name="dwid3" value="1"> -->
<?
    }
?>
            <input type="hidden" name="lwid3" value="<? if ( $cwid[3] > 0 ) { echo( $cwid[3] ); } ?>" >
            </font>
        </td>
        <td colspan="2">
            &nbsp;
        </td>
    </tr>
    <tr>
        <td colspan="4">
            &nbsp;
        </td>
    </tr>
    <tr>
        <td colspan="4" align="left">
            <p>
            <input type="hidden" name="wid" value="<? echo $row['wid']; ?>" >
            <input type="hidden" name="state" value="<? echo $state; ?>" >
            <input type="submit" name="Submit" value="<? echo $title_msg; ?>" >
            </p>
        </td>
    </tr>
</table>
</form>
</p>

<?
} // end of function


/* The main loop.  Call functions based on the value of $state, which gets set via a hidden INPUT TYPE. */

// from leftframe - state contain search term

$state = $_REQUEST["state"];
$wid = $_REQUEST["wid"];
$transliteration = $_POST["transliteration"];
$word = $_POST["word"];
$written = $_POST["written"];
$gender = $_POST["gender"];
$number = $_POST["number"];
$meaning = $_POST["meaning"];
$meaning2 = $_POST["meaning2"];
$tlanguage = $_POST["tlanguage"];
$part = $_POST["part"];
$root = $_POST["root"];
$weight = $_POST["weight"];
$level = $_POST["level"];
$image = $_POST["image"];
$exp = $_POST["exp"];
$searchtype = $_POST["searchtype"];
$username = $_SERVER['REMOTE_USER'];
$clang[1] = $_POST["clang1"];
$cwid[1] = $_POST["cwid1"];
$lwid[1] = $_POST["lwid1"];
$dwid[1] = $_POST["dwid1"];
$clang[2] = $_POST["clang2"];
$cwid[2] = $_POST["cwid2"];
$lwid[2] = $_POST["lwid2"];
$dwid[2] = $_POST["dwid2"];
$clang[3] = $_POST["clang3"];
$cwid[3] = $_POST["cwid3"];
$lwid[3] = $_POST["lwid3"];
$dwid[3] = $_POST["dwid3"];
$clang[4] = $_POST["clang4"];
$cwid[4] = $_POST["cwid4"];
$lwid[4] = $_POST["lwid4"];
$dwid[4] = $_POST["dwid4"];
$clang[5] = $_POST["clang5"];
$cwid[5] = $_POST["cwid5"];
$lwid[5] = $_POST["lwid5"];
$dwid[5] = $_POST["dwid5"];

//$temp2 = $dwid[5];
//echo( "<p>temp2: $temp2 </p>\n" );
//echo( "<p>state: $state </p>\n" );

switch ( $state ) {
//    case "";
//        Main_Menu();
//echo "\$level = $level<br>";
//echo "\$state = $state<br>";
//echo "\$word = $word<br>";
//echo "\$wid = $wid<br>";
//echo "\$searchtype = $searchtype<br>";
//        break;

    case "List";
        List_Contacts( $searchtype, $level, $username );
        break;

    case "Export";
        Export_Contacts( $searchtype, $level, $username );
        break;

    case "Print";
        Print_Contacts( $searchtype, $level, $username );
        break;

    case "Create";
        $found = 1;
        Contact_Form( $state, $wid );
        break;

    case "Update";
        $found = 1;
        Contact_Form( $state, $wid );
        break;

    case "Commit_Update";
        $found = 1;
        Backup_Contact( $wid, $username );
        Commit_Update( $wid, $transliteration, $word, $written, $gender, $number, $meaning, $meaning2, $tlanguage, $part, $root, $exp, $weight, $level, $username, $clang, $cwid, $lwid, $dwid );
        Contact_Form( $state, $wid );
        break;

    case "Commit_Contact";
        $found = 1;
        Commit_Contact( $transliteration, $word, $written, $gender, $number, $meaning, $meaning2, $tlanguage, $part, $root, $exp, $weight, $level, $username, $clang, $cwid, $lwid, $dwid );
        Contact_Form( $state, $wid );
        break;

    case "Copy_Contact";
        $found = 1;
        Copy_Contact( $wid, $username );
        $state = "Update";
        $wid = $new_wid;
        Contact_Form( $state, $wid );
        break;

    case "Delete";
        $found = 1;
        List_Contact_Delete( $wid, $username );
        break;

    case "Confirm_Delete";
        $found = 1;
        Backup_Contact( $wid, $username );
        Delete_Contact( $wid, $username );
        break;

//    case "";
//        Contact_Form( $state, $wid );
//        break;

//    default;
//        List_Contacts_Alpha( $state, $word, $wid, $searchtype );
//        break;
}

if ( $found != 1 && $wid > 0 ) {
    $state = "Update";
    Contact_Form( $state, $wid );
}
elseif ( $found != 1 && ( $state != "" || $word != "" ) ) {
    List_Contacts_Alpha( $state, $word, $wid, $searchtype );
}
elseif ( $found != 1 ) {
//    List_Contacts_Alpha( $state, $word, $wid, $searchtype );
    if ( $wid > 0 ) {
        $state = "Update";
        Contact_Form( $state, $wid );
    }
    else {
        Contact_Form( $state, $wid );
    }
}
?>

</BODY>
</HTML>
Return current item: phpWords