Google Answers Logo
View Question
 
Q: PHP to MYSQL Webform ( No Answer,   1 Comment )
Question  
Subject: PHP to MYSQL Webform
Category: Computers > Internet
Asked by: steven1010-ga
List Price: $10.00
Posted: 08 Mar 2006 03:32 PST
Expires: 07 Apr 2006 04:32 PDT
Question ID: 704873
How do I set up a webform in PHP sending email to a MYSQL database.
All I know is my webhost has facility for PHP and mYSQL. I want to be
able to review captured data in the database but do not know exactly
how to do this.
Answer  
There is no answer at this time.

Comments  
Subject: Re: PHP to MYSQL Webform
From: jrpereira-ga on 09 Mar 2006 16:16 PST
 
Hope this helps.  I scanned it a few times for errors, but I'm sure
you'll be rewriting the code anyways to suit your needs.


http://www.mysql.com
- is your first start for knowing any msql syntax.  You're going to
need this later to do any queries to retrieve specific information. 
I'm including this as I don't know the exact structure of your tables
nor do I know what you want to retrieve.

http://us3.php.net/manual/en/function.mysql-query.php
- is the documentation for the main function you're going to be making
use of - mysql_query.


Basically it goes like this.  The easiest way I've found to check if a
form's been submitted is to check whether or not the value of the
submit button's name has been submitted $_POST["submitbutton"] ==
"Submit" or similar.  If that rings true, then you first do all of
your validation, then connect to the database and insert the new row
for that user's submission.  It'll look sort of like this (remember
I'm winging this so treat it as psuedocode)...

$host = "localhost"; // almost always localhost, unless you have some
external mysql server
$username = "username"; // The username for that database
$password = "password"; // the password for that user
$database = "mydatabase"; // The database you want to affect
$tablename =  "mytable"; // Table in the database you want to affect

$coumns = "blah,blah2,blah3"; // Columns you want to set values for,
seperated by commas

// These values can be retrieved from the form using
$_POST["fieldname"].  If the register globals setting is turned on,
you can just use $fieldname, but I'd recommend against it (security
issues if it's turned on, and it's not always on anyways).

// See this page for more info on $_POST
// http://us3.php.net/manual/en/reserved.variables.php#reserved.variables.post

$values = "value1, value2, value3"; // Values you want to set for each
column, seperated by commas and following the order set for $columns

// Connect
mysql_connect($host,$username,$password);

// Select db
mysql_select_db($database) or die( "Unable to select database");

// Inserts the new row
mysql_query("INSERT INTO $tablename ($columns) VALUES($values)");

// Close connection
mysql_close();



Now that you've inserted the data from the form into your database,
all that's left is to retrieve it for display.  This is an example of
the most basic of outputs you could do for entries to the form.  This
doesn't go over removing or editing entries, but the mysql and php
sites go over a lot of the functions and query strings you'd need to
do that.


$host = "localhost"; // almost always localhost, unless you have some
external mysql server
$username = "username"; // The username for that database
$password = "password"; // the password for that user
$database = "mydatabase"; // The database you want to affect
$tablename =  "mytable"; // Table in the database you want to affect

// Connect
mysql_connect($host,$username,$password);

// Select db
mysql_select_db($database) or die( "Unable to select database");

// Return rows based on your query (in this case, return all rows)
$result = mysql_query("SELECT * FROM $tablename");

// In this case, we just assume that there's 3 columns in the table
that are named col1, col2, and col3
while ($row = mysql_fetch_array($result);)
{
	// Outputs the values of col1, col2, and col3 -- one per line, and
adding an extra newline at the end to seperate out each column
	echo("Column1: ".$row["col1"]."<br>"."Column2:
".$row["col2"]."<br>"."Column3: ".$row["col3"]."<br><br>");

	// The result looks like the following in the browser (not the
source).  I use "columnxvalue" as just a generic value you might've
set for those when inserting that row into the table

	// Column1: column1value
	// Column2: column2value
	// Column3: column3value
	//
	// Column1: column1value
	// Column2: column2value
	// Column3: column3value
	//
	// (and so on)...
}

// Close connection
mysql_close();

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy