Jump to content

php !empty and !is_string and !strleng doesn't work with me.


sayedsohail

Recommended Posts

Hi,

 

I am trying to validate post variable (cname) but function doesn't do anything just insert the records in my table.  Any advise.

 

 

if(isset($_POST[submit]))
{

	if ((!empty($_POST[cname])) && (!is_string($_POST[cname])) && (!strlen(trim($_POST[cname])>30))) 
		{
		print 'Please Enter a valid company name';
		exit;
		}
	else
	{

	$q1 = "insert into clients (member_id, name, address_1, p_code, city, l_line, mobile) VALUES
	('".$_SESSION['SESS_MEMID']."', '$_POST[cname]','$_POST[address]','$_POST[postcode]', '$_POST[city]','$_POST[phone]',				'$_POST[mobile]')";
	mysql_query($q1) or die(mysql_error());
	print '<script>self.close()</script>';
	}

}

Link to comment
Share on other sites

As you have noticed, the insert command is nested its just performing the insert wihtout doing the validation, the problem is at the validation, which i changed, still the same problem.

 

if(isset($_POST['submit']))
{

	if ((!empty($_POST['cname'])) && (!is_string($_POST['cname'])) && (!strlen(trim($_POST['cname'])>30))) 
		{
		print 'Please Enter a valid company name';
		exit;
		}
	else
	{

	$q1 = "insert into clients (member_id, name, address_1, p_code, city, l_line, mobile) VALUES
	('".$_SESSION['SESS_MEMID']."', '$_POST[cname]','$_POST[address]','$_POST[postcode]', '$_POST[city]','$_POST[phone]',				'$_POST[mobile]')";
	mysql_query($q1) or die(mysql_error());
	print '<script>self.close()</script>';
	}

}

Link to comment
Share on other sites

it also shouldnt be if(!empty), im guessing that you want to print the error msg if the field is empty and not if it isnt empty... same with strlen.. and whats up with all () you dont have to start with ()

 

so this

 

if ((!empty($_POST['cname'])) && (!is_string($_POST['cname'])) && (!strlen(trim($_POST['cname'])>30)))

 

should be:

if (empty($_POST['cname']) && !is_string($_POST['cname']) && strlen(trim($_POST['cname'])>30))

 

also consider the use of || insted of &&...

Link to comment
Share on other sites

complete code is listed above except the form,  although else nested value doing the job and closing the page and adding a blank record in my sql table.  nesting have no problem, only the validation is not echo any errors, definately something wrong with my nexting or !empty and !is_string, !strlen problems.

 

 

 

if(isset($_POST['submit']))

{

 

if ((!empty($_POST['cname'])) && (!is_string($_POST['cname'])) && (!strlen(trim($_POST['cname'])>30)))

{

print 'Please Enter a valid company name';

exit;

}

else

{

 

$q1 = "insert into clients (member_id, name, address_1, p_code, city, l_line, mobile) VALUES

('".$_SESSION['SESS_MEMID']."', '$_POST[cname]','$_POST[address]','$_POST[postcode]', '$_POST[city]','$_POST[phone]', '$_POST[mobile]')";

mysql_query($q1) or die(mysql_error());

print '<script>self.close()</script>';

}

 

}

Link to comment
Share on other sites

Then are is only one thing left to do, cut down the conditions... like this to check which condition is fcked...

 

if(isset($_POST['submit']))

{

 

      if (empty($_POST['cname'])

        {

        print 'Please Enter a valid company name';

        exit;

        }

      else

      {

 

      $q1 = "insert into clients (member_id, name, address_1, p_code, city, l_line, mobile) VALUES

      ('".$_SESSION['SESS_MEMID']."', '$_POST[cname]','$_POST[address]','$_POST[postcode]', '$_POST[city]','$_POST[phone]',            '$_POST[mobile]')";

      mysql_query($q1) or die(mysql_error());

      print '<script>self.close()</script>';

      }

 

}

 

this should print the error if there is something type in the field...

 

Link to comment
Share on other sites

The empty funtion works fine, !is_string is the problem, when i enter values ie., xyz 123, it doesn't validate and the white space,  any function to padd the white spaces. such as trim(), although trim can only padd left and rigth spaces how do i padd values in between two values i.e., xyz 123.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.