Showing posts with label Function. Show all posts
Showing posts with label Function. Show all posts

Sunday, March 8, 2009

Number of Rows in Database Function

Have created a function that will query a database and return the number of rows as a result of the query.

I use this function as part of log in access of myweb site. The first section of code is on the log in page.

$username = $_POST['usernamel'];
$password = $_POST['password'];

include "functionfile.php";

$rowqry = "SELECT * FROM tablename WHERE username = '$username' AND password = '$password' ";
if(numofrows($rowqry) == 1)  // calling the fuction and checking that the results equal 1
{
// if result is correct add the info that you want to display here
}
else
{
echo "Your password or username may be incorrect, please try again";
}

This is the function that is called, I have a single fill (functionfile.php) with all my functions that I include in the code.

function numofrows($rowqry)
{
$host = "localhost"; // database host
$un = "username"; // database username
$pw = "password"; // database password
$dbname = "database"; // database name
$dbase = mysql_connect($host, $un, $pw);
if (!$dbase)
die ( "No Connection"); // connecting to database
mysql_select_db($dbname, $dbase)
or die ("Could not open $dbname: ".mysql_error()); // Opening database
$result = mysql_query($rowqry, $dbase); // applying query to database
$rows = mysql_num_rows($result); // counting rows in the applied query
return $rows; // returns result to fuction
}

Thursday, February 19, 2009

Adding a php contact form

Create a form with the action calling itself, and the method is POST

The form should have 2 input text boxes named, name, email and a textarea named question

Have added some JavaScript information to validate the contents of the form, at this post 

Add the folowing to the start of the php file

$name = $_POST['name'];
$email = $_POST['email'];
$question = $_POST['question'];
$emailto = "email@yourwebsite.com";

Include the following to call a function

include "function_contact_form.php";
contact_form($name,$email,$question,$emailto);

Create a file called function_contact_form.php and add the text below.

function contact_form($name,$email,$question,$emailto)
{
$myemail = $emailto;

$subject = "Ask A Question";

$message = "From: $name ($email) \n
Question: $question \n";

$from = "From: $email\r\n";

$spamerrormessage = "A web site URL has been detected, the form submission has been cancelled";
if (preg_match("/http/i", "$name")) 
{
echo " $spamerrormessage"; 
exit();
}
if (preg_match("/http/i", "$email")) 
{
echo " $spamerrormessage";
exit();
}
if (preg_match("/http/i", "$message")) 
{
echo " $spamerrormessage"; 
exit();
}


if ($myemail !="")
mail ($myemail, $subject, $message, $from);
echo "Thank You $name for your inquiry.";
}