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>';
	}

}

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>';
	}

}

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 &&...

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>';

}

 

}

Here is the form

 

<form action="<?php  $_SERVER['PHP_SELF'] ?>" method="post" >

<TR>

<td>Company Name </td>

    <td><input type='text' name='cname' value='<?php echo $_POST['cname']?>'> </td>

 

</tr>

<tr><input type='submit' name ='submit' value='Save!'/></tr>

</form>

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...

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.