Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I do this

Status
Not open for further replies.

zrazzaq

MIS
Apr 13, 2005
102
US
Hi:
I wanted to know how a null statement will go into PHP. I am learning as I go along. What I am trying to say below is if the code is not null then place in a 4 into standardads field otherwise do not do anything. Please check if I wrote it correctly. Thanks
Code:
if(isset($_POST[s1]))
{
	$q1 = "insert into class_members set 
		code = '$_POST[code]',
		if(isnotnull($_POST[code]))
			{
		StandardAds=4
			}
	}
	RegDate = '$t' ";
etc.....
 
Actually let me rephrase it as well...I also need to know how to lookup the promotional code that is in another promo table and check to see if the code exits and then place in a 4 in standardads if it matches.
Thanks
Z
 
To check if a variable is null use the function: is_null().
If you are not just typing pseudo-code for the above post you may also want to read the PHP manual, and go over some PHP/DB tutorials, before continuing.
 
particularly have a look at the use of quotes for identifying array elements.

also - if you're "learning as you go along" i'd recommend that now is the time to grab a copy of the PEAR DB class and start using abstraction from the beginning. It makes your code more portable and also helps fix those silly errors we all make about escaping data and quoting non numeric values.

for your code try something like this. there may be a way to do it all in one query but without your schema i could guess. also it's better posed as a question in the mysql forum:

Code:
<?
if(isset($_POST['s1'])) //nb quotes are needed here
{
   $q1 = 
	"	insert into class_members 
		set 
			code = '".$_POST['code']."'
			RegDate = '".$t."'";
	
   if (isset($_POST['code']) && !is_null($_POST['code']))
   {	
   		if (validcode($_POST['code']))  q1 .= ",standardads = 4"; // tests whether the code is valid and if so appends a bit to the query string
   }
   
   //do something with  the $q1 string
	mysql_query($q1)
	 or die ("mysql error");
}
	
function validcode ($code)
{
	//db connect
	$codetable = ;//insert name of table where codes are stored
	$codefield = ; //
	$sql = "Select count(*) as cnt from $codetable where $codefield = \"$code\"";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	if ($row['cnt'] > 0): //ie if there is one or more records with the code
		return true;
	else:
		return false;
	endif;
}
	
?>
 
Thanks jpadie...What site do I go on for the Pear DB? It sounds helpful.
Z
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top