PDA

View Full Version : PHP/MySQL form tutorial.



Independent
07-07-2008, 12:53 AM
Right, I was bored at the time of making this.



<?php
## CONNECTION TO DATABASE ##
mysql_connect("Your host", "Your username", "Your password"); // Connecting to the allocated server with the credentials inserted. (hostname, user, pass)
mysql_select_db("Your database"); // Selecting the database we'll connect too.
## VARIBLES ##
$table = "lolcopter"; // My table name is lolcopter.. Update this?
$field = "waffle"; // My field name is waffle.. Update this?
## CLEANER ##
function clean($s) { // Function start.
if (get_magic_quotes_gpc()) // Performing the clean, basically.
$s = stripslashes($s); // Stripping the slashes from the public seeing \"lol or so forth.
return mysql_real_escape_string($s); // Returning it..
} // Ending the function. As you see later in the code "clean($_POST..".
## FORM ## - Just to make our code cleaner, I've sectioned it.
if(!$_POST['submit']) {
echo "<form action=\"\" method=\"post\"><input name=\"roflcopter\" type=\"text\" /><input name=\"submit\" type=\"submit\" /></form>"; // Showing the form that will submit the data "roflcopter".
} // End of the !$_POST['Submit'] statement.
else { // If it has been posted, then the "else" will attempt to insert it into the database.
###############################################
$roflcopterpost = clean($_POST['roflcopter']); // We're cleaning the posted message, before it's inserted into the database.
mysql_query("INSERT INTO ". $table ." (". $field .") VALUES('$roflcopterpost') ") or die("Error executing your form."); // Now we're inserting it into the database.
echo "The data has been entered into the database."; // We're stating that it's been inserted.
################################################
}
?>
I've commented it as much as I could;

First, create a database;
Second, enter the connection details;
Third, Create a table & field named Waffle or something. (phpmyAdmin)?;
Forth, Insert the field name into " $field ".

This was a tutorial just to show you basically how it works, you can add-on bits, there are many useful tutorials all over the web.. some other links are listed below.

http://www.tizag.com/mysqlTutorial/
http://www.tizag.com/phpT/
http://www.techtuts.com/forums/index.php?showforum=5

Hope I've helped..
Calon.

iJoe
07-07-2008, 01:08 AM
Works :) Since you changed it :P

Thanks :)

http://nexdana.com/test.php

Protege
07-07-2008, 01:10 AM
Would be nice if even I could understand it lols

This would be a better example on your behalf.


<?php
/* Database connection information */
mysql_connect ( 'localhost', 'username', 'password' ) or
die ( 'Cannot connect to the database, reason: ' . mysql_error () );

mysql_select_db ( 'database_select' );

/* Clean func */
function cleanString ( $str )
{
// You could add more cleaning functions here if you wish.
$_str = mysql_real_escape_string ( trim ( $str ) );
return ( $_str );
}

/* Weird way of confirming post... */
if ( !$_POST [ 'submit' ] )
{
echo ( '<form action="" method="post">
<input name="textfield" type="text">
<input name="submit" value="submit" type="submit">
</form>' );
}
else
{
if ( $_POST[ 'textfield' ] != '' )
{
$mysqlQuery = mysql_query ( 'INSERT INTO `table` ( `field` ) VALUES ( "' . cleanString ( $_POST[ 'textfield' ] ) . '" );' );
if ( $mysqlQuery )
{
echo ( 'Complete' );
}
else
{
echo ( 'Error' );
}
}
else
{
echo ( 'Please do not leave the field blank!' );
}
}
?>

Independent
07-07-2008, 01:12 AM
Would be nice if even I could understand it lols

This would be a better example on your behalf.


<?php
mysql_connect ( 'localhost', 'username', 'password' ) or
die ( 'Cannot connect to the database, reason: ' . mysql_error () );

mysql_select_db ( 'database_select' );

function cleanString ( $str )
{
// You could add more cleaning functions here if you wish.
$_str = mysql_real_escape_string ( trim ( $str ) );
return ( $_str );
}

if ( !$_POST [ 'submit' ] )
{
echo ( '<form action="" method="post">
<input name="textfield" type="text">
<input name="submit" value="submit" type="submit">
</form>' );
}
else
{
if ( $_POST[ 'textfield' ] != '' )
{
$mysqlQuery = mysql_query ( 'INSERT INTO `table` ( `field` ) VALUES ( "' . cleanString ( $_POST[ 'textfield' ] ) . '" );' );
if ( $mysqlQuery )
{
echo ( 'Complete' );
}
else
{
echo ( 'Error' );
}
}
else
{
echo ( 'Please do not leave the field blank!' );
}
}
?>
Thanks, that's a hell of a lot more cleaner.

But I think my tutorial explains a little better. ^_^

Protege
07-07-2008, 01:13 AM
Yes but you don't want to teach people evil hard-to-read code.

iJoe
07-07-2008, 01:14 AM
How would I add another field?

I tried, well just kinda guessed :P


<?php
## CONNECTION TO DATABASE ##
mysql_connect("localhost", "nexdana_test", "????"); // Connecting to the allocated server with the credentials inserted. (hostname, user, pass)
mysql_select_db("nexdana_test"); // Selecting the database we'll connect too.
## VARIBLES ##
$table = "waffle"; // My table name is waffle.. Update this?
$field = "lolcopter"; // My field name is lolcopter.. Update this?
$field2 = "shoe"; // My second field name is shoe
## CLEANER ##
function clean($s) { // Function start.
if (get_magic_quotes_gpc()) // Performing the clean, basically.
$s = stripslashes($s); // Stripping the slashes from the public seeing \"lol or so forth.
return mysql_real_escape_string($s); // Returning it..
} // Ending the function. As you see later in the code "clean($_POST..".
## FORM ## - Just to make our code cleaner, I've sectioned it.
if(!$_POST['submit']) {
echo "<form action=\"\" method=\"post\"><input name=\"roflcopter\" type=\"text\" /><input name=\"shoe\" type=\"text\" /><input name=\"submit\" type=\"submit\" /></form>"; // Showing the form that will submit the data "roflcopter".
} // End of the !$_POST['Submit'] statement.
else { // If it has been posted, then the "else" will attempt to insert it into the database.
###############################################
$roflcopterpost = clean($_POST['roflcopter']); // We're cleaning the posted message, before it's inserted into the database.
mysql_query("INSERT INTO ". $table ." (". $field . $field2 .") VALUES('$roflcopterpost')('$shoepost') ") or die("Error executing your form."); // Now we're inserting it into the database.
echo "The data has been entered into the database."; // We're stating that it's been inserted.
################################################
}
?>

Independent
07-07-2008, 01:14 AM
Yes but you don't want to teach people evil hard-to-read code.
*Shifty looks*

True, but it shows someone how at least. ^_^

I'm going to bed now, hope this helps someone or gets moved to tutorial section :P

Want to hide these adverts? Register an account for free!