PHP/MYSQL

DCT Jared Smith jared at dctkc.com
Wed Apr 10 22:32:30 CDT 2002


Here is a little tiny script that does what you're asking.
Another coder would do it differently... Eh. It's all good.

You may want to browse the PHP forums for better help...

----<snip everything below this line except the LUG .sig>----

<?
// REQUEST DATA
$announce = "Enter data!";

// this variable $data will exist if
// you have filled in the HTML form below
// otherwise this code will be skipped.
if ($data) {

    // CONNECT TO DATABASE
    $connection = mysql_connect ("localhost", "", "")
        or die ("connection is broke");
    mysql_select_db("teamscores", $connection)
        or die ("db is broke");

    // INSERT DATA
    for ($i=0; $i<count($data); $i++) {
        $sql = "UPDATE tblscores SET score=$data[$i] WHERE
teamname="$team[$i]";";
        if (!mysql_query($sql)) {
            echo "Query failed: <br><font color=red>$sql</font><br>";
        }
        echo $sql . "<br>";
    }

    // CLOSE YOUR DB CONNECTION
    mysql_close($connection);

    // ANNOUNCE YOUR SUCCESS
    $announce = "Data Updated!";

    echo "<br>";
}
?>

<html><head><title>Update Teams</title></head><body>
<b><? echo $announce; ?></b><br><br>
<form method=get action="<? echo $PHP_SELF; ?>">
<input type=hidden name="team[]" value="Team1"> Team 1:
    <input type=text size=5 name="data[]" value="<? echo $data[0]; ?>"><br>
<input type=hidden name="team[]" value="Team2"> Team 2:
    <input type=text size=5 name="data[]" value="<? echo $data[1]; ?>"><br>
<input type=hidden name="team[]" value="Team3"> Team 3:
    <input type=text size=5 name="data[]" value="<? echo $data[2]; ?>"><br>
<input type=hidden name="team[]" value="Team4"> Team 4:
    <input type=text size=5 name="data[]" value="<? echo $data[3]; ?>"><br>
<br>
<input type="submit" value=" Update ">
<br>
<br>
This is extreme skeleton view. You'll want to make it a little
more robust, but this should get you started. Look at the HTML. Notice
how the data above is being submitted with the square brackets. PHP
creates an array for each 'name[]', incrementing array elements for each
time the name is submitted by a form. In this example, we submit 4 'data'
elements, and create an array called $data in PHP. Take a look at
your URL to see how it is posted. Change the form "GET" to "POST"
when you're done debugging, to hide the URL data....<br>
<br>
Become very familiar with the PHP online function reference. It's
the best part of PHP! Here is a link that might come in handy:
<a href="javascript:void(q=prompt('PHP
Reference:',''));if(q)location.href='http://www.php.net/manual-lookup.php?fu
nction='+escape(q)">Jump to PHP Function Reference</a>
</body></html>




More information about the Kclug mailing list