?
Solved

check if row exists and value of another field if it does exist

Posted on 2011-05-04
2
Medium Priority
?
209 Views
Last Modified: 2012-05-11
i need to query a table to see if a row exists and if it does check the value of a field. below is my code and unfortunately my error.

my call function searchTable($data)

error:  no db selected.
<?php
//declare variable and set to empty
$message = "";
//db settings
$host="pcname"; // Host name 
$username="admin"; // Mysql username 
$password="admin!"; // Mysql password 
$db_name="testdb"; // Database name 
$tbl_name="vouchercodes"; // Table name 

//get postback
if(isset($_POST['btnSubmit']))
{
    //get form data and clean it
    $voucherCode = check_input($_POST['txtbxVCode']);
    //populate variable with form data
    $message = $voucherCode;    //echo "voucher code: ".$message;
    //search for the value and if redeemed
    searchTable($voucherCode);
}
//run process to clean form data
function check_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
//search for data
function searchTable($data)
{
	mysql_connect("$host", "$username", "$password")or die("cannot connect ".mysql_error()); 
	mysql_select_db("$db_name")or die("cannot select DB - "); //errors here -- cannot select DB - No database selected

	$query = "select vouchercode, redeemed from $tbl_name where vouchercode = '$data' ";
	$result = mysql_query($query)or die(mysql_error());
 	$row = mysql_fetch_array( $result );
	$voucherCode = $row['vouchercode'];
	$redeemed = $row['redeemed'];
 
	if (mysql_num_rows($result) && $redeemed = "N")
	{
	    $message = "voucher is in the table and not redeemed";
	}
	else
	{
	    $message = "voucher is not in the table or it has been redeemed.";
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<script type="text/javascript">
//validate the form
function ValidateForm()
{
	//validate txtbxVCode
	if(document.vcodeform.txtbxVCode.value == "")
   	{ 
    	alert('You must indicate a voucher code.') 
      	document.vcodeform.txtbxVCode.focus(); 
      	return false; 
   	}
return true; 
}
</script>
</head>
<body onload="document.vcodeform.txtbxVCode.focus();">
<form name="vcodeform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table cellpadding="0" cellspacing="0" style="width: 15%">
	<tr>
		<td>Redeem Voucher</td>
	</tr>
	<tr>
		<td>
			<input name="txtbxVCode" type="text" id="txtbxVCode">
		</td>
	</tr>
	<tr>
		<td>
			<input name="btnSubmit" type="submit"  id="btnSubmit" onclick="return ValidateForm()" value="Submit">
		</td>
	</tr>
	<tr>
		<td>
		<?php 
		  //print form data
		      echo $message; 
		?>
		</td>
	</tr>
</table>
</form>
</body>
</html>

Open in new window

0
Comment
Question by:vicomin
2 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 35690857
those variables outside the function need to be declared as global within the function:
...
function searchTable($data)
{
global $host, $username, $password,$db_name,$tbl_name;
...
}

Open in new window

0
 
LVL 6

Expert Comment

by:cfEngineers
ID: 35690860
$query = "select vouchercode, redeemed from ". $tbl_name. " where vouchercode = '".$data."' ";
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question