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>'; } } Quote Link to comment 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'] Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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>'; } } Quote Link to comment 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))) Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 still the same. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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))) Quote Link to comment 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 &&... Quote Link to comment 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)) Quote Link to comment 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... Quote Link to comment 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>'; } } Quote Link to comment 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 Quote Link to comment 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.. Quote Link to comment 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> Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
sayedsohail Posted February 17, 2007 Author Share Posted February 17, 2007 echo added, still the same. Quote Link to comment 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 Quote Link to comment 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'])) .... Quote Link to comment 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. Quote Link to comment 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... Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.