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

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

vicominAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
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
 
cfEngineersCommented:
$query = "select vouchercode, redeemed from ". $tbl_name. " where vouchercode = '".$data."' ";
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.