thaidomizil Posted September 29, 2011 Share Posted September 29, 2011 Hello, i'm trying to server-side validate input from POST to insert it into mysql if the requirements for the insert are met. i was using jquery before but this isn't reliable at all, i want the data to get inserted into mysql only if the length is 19 chars and only if it starts with 3227580 the next 12 digits can be any number. So basically: IF chars=19 and data=3227580* insert into mysql -- else show error Thank you for help. Quote Link to comment https://forums.phpfreaks.com/topic/248115-validate-numbers-before-inserting-into-mysql/ Share on other sites More sharing options...
Buddski Posted September 29, 2011 Share Posted September 29, 2011 Try this: if (strlen($_POST['your_field']) != 19 && substr($_POST['your_field'],0,7) != '3227580') { // Throw Error // } else { // Success// } Quote Link to comment https://forums.phpfreaks.com/topic/248115-validate-numbers-before-inserting-into-mysql/#findComment-1274071 Share on other sites More sharing options...
thaidomizil Posted September 29, 2011 Author Share Posted September 29, 2011 Works, thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/248115-validate-numbers-before-inserting-into-mysql/#findComment-1274074 Share on other sites More sharing options...
thaidomizil Posted September 29, 2011 Author Share Posted September 29, 2011 I don't want to open a new thread for this, your code works fine, i'm trying to put if(isset($_POST['submit'])) infront, my final code looks like this: if(isset($_POST['submit'])) { if (strlen($_POST['test']) != 19 && substr($_POST['test'],0,7) != '1234567') { echo "wrong code"; } else { mysql_query("INSERT INTO table(code) VALUES('$_POST[test]')"); Print "Your table has been populated"; }} now the script doesn't do anything, i guess i set the curly braces wrong or can i not put 2x if behind each other ? can you enlighten me on this please Quote Link to comment https://forums.phpfreaks.com/topic/248115-validate-numbers-before-inserting-into-mysql/#findComment-1274091 Share on other sites More sharing options...
xyph Posted September 29, 2011 Share Posted September 29, 2011 Do you have a button/input named 'submit' ? Some clients won't send the submit button if you use {enter} to submit a form, from what I've read. Don't forget to make sure the rest of the string is numeric as well. ctype_digit will help with that. Quote Link to comment https://forums.phpfreaks.com/topic/248115-validate-numbers-before-inserting-into-mysql/#findComment-1274093 Share on other sites More sharing options...
thaidomizil Posted September 29, 2011 Author Share Posted September 29, 2011 Thanks xyph, i just noticed my button name wasn't set to submit only the id. Also, i just saw that this isn't working: if (strlen($_POST['test']) != 19 This is working fine though && substr($_POST['test'],0,7) != '1234567') any idea? Quote Link to comment https://forums.phpfreaks.com/topic/248115-validate-numbers-before-inserting-into-mysql/#findComment-1274095 Share on other sites More sharing options...
xyph Posted September 29, 2011 Share Posted September 29, 2011 <?php $str1 = '1234567890123456789'; $str2 = '12345678'; if( strlen($str1) != 19 ) echo '$str1 is not 19 numbers'; else echo '$str1 is 19 numbers'; echo '<br>'; if( strlen($str2) != 19 ) echo '$str2 is not 19 numbers'; else echo '$str2 is 19 numbers'; ?> Not sure why yours isn't working. Quote Link to comment https://forums.phpfreaks.com/topic/248115-validate-numbers-before-inserting-into-mysql/#findComment-1274158 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.