Jump to content

Validate numbers before inserting into MySQL


thaidomizil

Recommended Posts

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

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.