08-06-2005, 11:12 AM
|
#1 (permalink)
|
|
Junior Member
Join Date: Jul 2005
Posts: 18
|
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.
|
|
|
08-06-2005, 04:15 PM
|
#2 (permalink)
|
|
Junior Member
Join Date: Jul 2005
Posts: 36
|
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]=' ';
}
$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. 
|
|
|
08-11-2005, 08:21 PM
|
#3 (permalink)
|
|
Member
Join Date: Jul 2005
Location: Nebraska
Posts: 33
|
Thanks for that ~ sandman nice post. - I bookmarked this thread for now...
|
|
|
09-22-2005, 06:16 AM
|
#4 (permalink)
|
|
Junior Member
Join Date: Sep 2005
Posts: 3
|
Wow this is very informative!
I will take note of this too  Could be helpful in the near future.
|
|
|
08-21-2006, 11:12 AM
|
#5 (permalink)
|
|
Junior Member
Join Date: Aug 2006
Posts: 19
|
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
|
|
|
08-21-2006, 07:50 PM
|
#6 (permalink)
|
|
Moderator Of The Year
Join Date: Jul 2006
Location: Cali Breeze
Posts: 716
|
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
|
|
|
08-21-2006, 09:29 PM
|
#7 (permalink)
|
|
Junior Member
Join Date: Aug 2006
Posts: 19
|
Re: PHP form for entering data to MySQL database
if im going to do it i like every thing a certain file type
|
|
|
08-22-2006, 01:55 AM
|
#8 (permalink)
|
|
Moderator Of The Year
Join Date: Jul 2006
Location: Cali Breeze
Posts: 716
|
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
|
|
|
08-22-2006, 08:12 AM
|
#9 (permalink)
|
|
Junior Member
Join Date: Aug 2006
Posts: 19
|
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
|
|
|
08-22-2006, 12:29 PM
|
#10 (permalink)
|
|
Junior Member
Join Date: May 2006
Location: Near Frankfurt
Posts: 26
|
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']);
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
Points Per Thread View: 1.00
Points Per Thread: 11.00
Points Per Reply: 5.00
|
|
|
|
|