I need to validate textarea in that we are giving mobile number seperated by comma

  if ($opt_selected=="y")
                        $errorMessage = "";
                        $phone = $_POST['phone'];

                        $myString = $phone;
                        // print_r($myString);

                        $myArray = explode(',', $myString);
                        $myArray = array_unique($myArray);
                       // $myArray1=preg_match("/^\?\d[0-9]{12}/", $myArray);
                        foreach ($myArray as $myArrays)
                              if(preg_match("/^\?\d[0-9]{12}/", $myArrays[0])!==false)
                              $ins="INSERT INTO `codexworld`.`valid_num`(`phone`)
                              VALUES ('$myArrays')";
                              $test=mysqli_query($db,$ins) or die(mysqli_error($db));
                                echo "Not valid";


Open in new window

the above is my php code ,I need to validate textarea in that we are giving mobile number separated by comma so i'm validating each number should have 12 digit number(including country code) if it is validated correct then insert into table otherwise it should give error message. so what I'm doing in above code is that correct? or what i should do change in that
shruti Aweb developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
At first glance your code looks good.

Only question relates to your database INSERT, which appears to add all phone numbers provided to a column which can support this type of INSERT.

Since you don't really say what problem you think your code is having, maybe this is the problem.

Simple debug approach, is just to print out all the individual numbers which your code finds as valid.

Also double check your database schema to ensure your INSERT will actually accomplish what you're after.
NerdsOfTechTechnology ScientistCommented:
first let's debug your REGEX:


Open in new window

This present regex expects a ? (question mark) delimiter, a single digit, followed by 12 more digits. I think this is the main problem. Rather, you should be searching for 12 digit sequences

The following matches 12 digits exactly:

Open in new window

You will probably want to use preg_match_all() as well in your case.

Lastly, you could insert all of the phones at once (one INSERT query):

$error = '';
$subject = '123456789012, 123456789013, 123456789014';
$pattern = '/\d{12}/';
preg_match_all($pattern, $subject, $matches);
$ins='INSERT INTO `codexworld`.`valid_num`(`phone`) VALUES ';
 foreach($matches[0] as $value){
  $ins .= "('$value'),";	
 $ins = substr($ins, 0, -1); //remove trailing comma
 $error = 'Error: no matches found';

 $test=mysqli_query($db,$ins) or die(mysqli_error($db));	
 echo $error;

Open in new window

sample resultant SQL of $ins:
INSERT INTO `codexworld`.`valid_num`(`phone`) VALUES ('123456789012'),('123456789013'),('123456789014')

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NerdsOfTechTechnology ScientistCommented:
RegEx provided and OP endorsed
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.