sayedsohail Posted February 17, 2007 Share Posted February 17, 2007 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 https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/ Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 try adding quotes to all $_POST[' ']'s quotes mean strings after all i mean just make em all $_POST['cname'] Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187344 Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 I applied the $_post['phone'], still the same problem. Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187348 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 sure you didn't miss $_POST[submit]? needs quotes too ah but i see it gets in that nested if statement anyways just make sure all of them have quotes Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187351 Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 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 https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187358 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 ya but your checks fail so..well... try to add if not isset if ((!isset($_POST['cname'])) && (!empty($_POST['cname'])) && (!is_string($_POST['cname'])) && (!strlen(trim($_POST['cname'])>30))) Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187385 Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 still the same. Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187401 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 omg lol of course u using alot of AND operators use OR's repalce all &&'s with ||'s with AND operators they all have to pass to make it go inside wait i dont know im confused.. i lost what i was about to type Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187408 Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 all changed to or || statement. stillt he same. Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187410 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 try leaving the cname blank in your form and click submit Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187413 Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 doe that still the same, here is my modified code if (!isset($_POST['cname'])) || (!is_string($_POST['cname'])) ||(strlen(trim($_POST['cname'])>30))) Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187415 Share on other sites More sharing options...
JJohnsenDK Posted February 17, 2007 Share Posted February 17, 2007 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 https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187417 Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 excatly copied the same still the sam problem. using || statement here is the code if(empty($_POST['cname']) || !is_string($_POST['cname']) || strlen(trim($_POST['cname'])>30)) Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187421 Share on other sites More sharing options...
JJohnsenDK Posted February 17, 2007 Share Posted February 17, 2007 plz post all your code, including the form script... Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187425 Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 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 https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187435 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 a $POST can still be not empty and not set always use isset() first on all of em thats why they enter as blanks in your db Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187437 Share on other sites More sharing options...
JJohnsenDK Posted February 17, 2007 Share Posted February 17, 2007 i would like to see the form plz.. Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187441 Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 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> Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187446 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 your missing a echo at your PHP_SELF Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187449 Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 echo added, still the same. Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187451 Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 what can i say that shit is fucked up bro its beyond understanding wait just a second i dont even see a submit button rofl Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187453 Share on other sites More sharing options...
JJohnsenDK Posted February 17, 2007 Share Posted February 17, 2007 i dont see a submit button in your form script? if you dont have a submit button named submit you cant use: if(isset($_POST['submit'])) .... Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187455 Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 sorry for missing submit. it was due to copy paste stuff. actually submit button exists in my form. Link to comment https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187461 Share on other sites More sharing options...
JJohnsenDK Posted February 17, 2007 Share Posted February 17, 2007 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 https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187476 Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 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 https://forums.phpfreaks.com/topic/38947-php-empty-and-is_string-and-strleng-doesnt-work-with-me/#findComment-187493 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.