Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more

Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Web Programming > PHP Development
User Name
Password

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-06-2005, 11:12 AM   #1 (permalink)
vvaldir
Junior Member
 
Join Date: Jul 2005
Posts: 18
Default PHP form for entering data to MySQL database

Hello,

I am a newbie to php & mysql. I have added a database with my web host. Using phpmyadmin I have created five fields for learning. The fields are named. (rec_id, first_name, last_name, some_field, notes.)

rec_id = primary key with auto_increment
first_name, last_name, some_field = VarChar with length 60
notes = text

What I want to do now is create a php form that will allow me to enter data into the database and then view the record added to verify it using php script also. I would like a script to view the last entry into the database.
Any help is appreciated.
vvaldir is offline   Reply With Quote
Sponsored Links
Old 08-06-2005, 04:15 PM   #2 (permalink)
sandman
Junior Member
 
Join Date: Jul 2005
Posts: 36
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
Old 08-11-2005, 08:21 PM   #3 (permalink)
angie
Member
 
Join Date: Jul 2005
Location: Nebraska
Posts: 33
Default

Thanks for that ~ sandman nice post. - I bookmarked this thread for now...
angie is offline   Reply With Quote
Old 09-22-2005, 06:16 AM   #4 (permalink)
kamal
Junior Member
 
Join Date: Sep 2005
Posts: 3
Default

Wow this is very informative!
I will take note of this too Could be helpful in the near future.
kamal is offline   Reply With Quote
Old 08-21-2006, 11:12 AM   #5 (permalink)
penguinsrule
Junior Member
 
Join Date: Aug 2006
Posts: 19
Default Re: PHP form for entering data to MySQL database

thats good i like it, 2 questions how do you deisplay data in like pages of 50 and the newest first. How can you get it to also display multi line like this

entered this

hello
what
are
you

Displayed like this

hello
what
are
you

thanks i used the top one excelent
penguinsrule is offline   Reply With Quote
Old 08-21-2006, 07:50 PM   #6 (permalink)
O~Snapple
Moderator Of The Year
 
O~Snapple's Avatar
 
Join Date: Jul 2006
Location: Cali Breeze
Posts: 716
Send a message via MSN to O~Snapple Send a message via Skype™ to O~Snapple
Default Re: PHP form for entering data to MySQL database

When it comes to calling Scripts i always remember that the Form itself is HTML and it has a .php action,

It can get confusing when you start thinking of it as a PHP form

~Snapple
__________________
The Real Truth
Epademik Emcee Esq. New Album The 3rd Finger is in Stores Now!
Snap Marketing Management and Consulting
O~Snapple is offline   Reply With Quote
Old 08-21-2006, 09:29 PM   #7 (permalink)
penguinsrule
Junior Member
 
Join Date: Aug 2006
Posts: 19
Default Re: PHP form for entering data to MySQL database

if im going to do it i like every thing a certain file type
penguinsrule is offline   Reply With Quote
Old 08-22-2006, 01:55 AM   #8 (permalink)
O~Snapple
Moderator Of The Year
 
O~Snapple's Avatar
 
Join Date: Jul 2006
Location: Cali Breeze
Posts: 716
Send a message via MSN to O~Snapple Send a message via Skype™ to O~Snapple
Default Re: PHP form for entering data to MySQL database

That is understandable penguin
but even though the extension is .PHP the page is HTML code

~O-SNAP
__________________
The Real Truth
Epademik Emcee Esq. New Album The 3rd Finger is in Stores Now!
Snap Marketing Management and Consulting
O~Snapple is offline   Reply With Quote
Old 08-22-2006, 08:12 AM   #9 (permalink)
penguinsrule
Junior Member
 
Join Date: Aug 2006
Posts: 19
Default Re: PHP form for entering data to MySQL database

well i know but it means if i want to include somthing like a hit counter i dont have o usr an iframe or have to convert the file
penguinsrule is offline   Reply With Quote
Old 08-22-2006, 12:29 PM   #10 (permalink)
No0oB
Junior Member
 
Join Date: May 2006
Location: Near Frankfurt
Posts: 26
Send a message via ICQ to No0oB Send a message via MSN to No0oB
Default Re: PHP form for entering data to MySQL database

Quote:
Originally Posted by penguinsrule
thats good i like it, 2 questions how do you deisplay data in like pages of 50 and the newest first. How can you get it to also display multi line like this

entered this

hello
what
are
you

Displayed like this

hello
what
are
you

thanks i used the top one excelent

Theres an easy function in PHP called nl2br()
So basically, when you call $row['notes'] (or whatever your variable for the MySQL output is) you use the function to do so
PHP Code:
echo nl2br($row['notes']); 
No0oB is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Points Per Thread View: 1.00
Points Per Thread: 11.00
Points Per Reply: 5.00


Similar Threads
Thread Thread Starter Forum Replies Last Post
Display mysql table fields and data goldfish PHP Development 6 05-14-2008 03:49 AM
Build a web database without any programming experience required xacto Advertise or Request 0 06-29-2005 08:25 PM
Listing mysql data using php kayla Other Programming - Perl, C++, Java, ASP, .NET Development 1 06-26-2005 09:07 PM


» Sponsors

» Links

» Affiliates
Web Hosting
Marketing Find
Merchant Select
SiteMap Builder
Host Compare
Dedicated Servers

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 08:20 AM.



LinkBacks Enabled by vBSEO 3.0.0 RC8
Webmaster Forums