View Single Post
Old 08-06-2005, 05:15 PM   #2 (permalink)
sandman
Junior Member
 
Join Date: Jul 2005
Posts: 34
Default

These two files may help. Of course you should replace the server connection fields and other data like your_table, field names etc. with your own information.

//insert save as -> new_record.php
PHP Code:
<?
    $usr 
"username";
    
$pwd "password";
    
$db "database_name";
    
$host "localhost";

    
# connect to database
    
$cid mysql_connect($host,$usr,$pwd);
    
mysql_select_db($db);
    if (
mysql_error()) { print "Database ERROR: " mysql_error(); }

?>
<html>
<head>
<title>Insert New Record</title>
</head>
<body bgcolor="#ffffff">

<h2>Insert New Record</h2>

<?
    
# this is processed when the form is submitted
    # back on to this page (POST METHOD)
    
if ($_SERVER['REQUEST_METHOD'] == "POST"
    {
        
# escape data and set variables
        
$FIRST_NAME addslashes($_POST["FIRST_NAME"]);
        
$LAST_NAME addslashes($_POST["LAST_NAME"]);
        
$SOME_FIELD addslashes($_POST["SOME_FIELD"]);
    
$NOTES addslashes($_POST["NOTES"]);

        
# setup SQL statement
$sql  " INSERT INTO your_table ";
$sql .= " (FIRST_NAME, LAST_NAME, SOME_FIELD, NOTES) VALUES ";
$sql .= " ('$FIRST_NAME','$LAST_NAME','$SOME_FIELD','$NOTES') ";

        
#execute SQL statement
        
$result mysql_query($sql$cid);

        
# check for error
        
if (mysql_error()) { print "Database ERROR: " mysql_error(); }

print 
"<h3><font color=red>New Job Added  - View it <a href=show_last_record.php>HERE</a></font></h3>";
}

?>

    <form name="fa" action="new_record.php" method="POST">     
    <table>                                                                                                        
    <tr><td><b>FIRST NAME: </b> </td><td><input type="text" name="FIRST_NAME" size=30></td></tr> 
    <tr><td><b>LAST NAME: </b> </td><td><input type="text" name="LAST_NAME" size=30></td></tr> 
    <tr><td><b>SOME FIELD: </b> </td><td><input type="text" name="SOME_FIELD" size=20></td></tr>
    <tr><td valign=top><b>Notes: </b> </td><td> <textarea name="NOTES" rows=4 cols=30></textarea></td></tr>
    <tr><th colspan=2><p><input type="submit" value="Add Record"></p></th></tr> 
    </table>
    </form>                                      

</body>
</html>
//save as -> show_last_record.php
PHP Code:
<?
    $usr 
"username";
    
$pwd "password";
    
$db "database_name";
    
$host "localhost";

    
# connect to database
    
$cid mysql_connect($host,$usr,$pwd);
    
mysql_select_db($db);
    if (
mysql_error()) { print "Database ERROR: " mysql_error(); }

?>
<html>
<head>
<title>Your New Entry</title>
</head>
<body>
<?php

function drawtable($qr) {
    
// $qr = results of a mysql_query
    
$rows mysql_num_rows($qr);
    
$toreturn "<table style=\"float: left;\" border=\"2\">\n";
    
$toreturn .= "<tr>\n";
    for (
$i=0$i<mysql_num_fields($qr); $i++) { 
        
$toreturn .= "\t<th>".mysql_field_name($qr,$i)."</th>\n";
        }
    
$toreturn .= "</tr>\n";
    for (
$i=0$i<$rows$i++) { 
        
$row mysql_fetch_row($qr);
        
$cols sizeof($row);
        
$toreturn .= "<tr>\n";
        for (
$x=0$x $cols$x++) {
            if (
$row[$x]==NULL){
                
$row[$x]='&nbsp;';
                }
            
$toreturn .= "\t<td>$row[$x]</td>\n";
            }
        
$toreturn .= "</tr>\n";
        }
    
$toreturn .= '</table>';
    return 
$toreturn;
    }
$q stripslashes($_POST['q']);
if (empty(
$q)) {
    
$q 'select FIRST_NAME, LAST_NAME from your_table order by LAST_NAME desc limit 1;';
    }
mysql_select_db("database_name");
$result mysql_query($q);

print 
"<b><font color=red>Your New Record Has Been Added</font></b>\n";

echo 
drawtable($result);
echo 
'<table>
</table>
</body>
</html>'
;
This is just a basic example. You could always modify the appearance with some html or css.
sandman is offline   Reply With Quote
Sponsored Links