-5
if (isset($_POST['sub']))
{
    $grpm_phno = $_POST['grpm_phno'];
    $grmid = $_POST['groupid'];
    $grpm_name = $_POST['grpm_name'];
    $name = explode(',', "$grpm_name");
    $phone = explode(',', "$grpm_phno");
    $countt = count($name);
    for ($i = 0; $i <= $countt; $i++)
    {
        $x = $name[$i];
        $y = $phone[$i];
        $dt = date('Y-m-d h:i:s');

        // Insert Query of SQL

        $query = mysql_query("insert into grp_mst(grpm_name, grpm_phno, grpm_grpcatm_id,grpm_typ,grpm_crtdon) values ('$x', '$y', '$grmid', 'b', '$dt')");
    }
}

input given:

name:raj,mohan

number:61231,3618372

output:

raj :61231

mohan:3618372

and empty row any help

mega6382
  • 9,211
  • 17
  • 48
  • 69
jayaram
  • 9
  • 5
  • 3
    instead of `for` loop try `foreach` loop http://php.net/manual/en/control-structures.foreach.php – Pragnesh Chauhan Dec 15 '17 at 06:44
  • 5
    `mysql_*` is deprecated as of [tag:php-5.5] and removed in [tag:php-7]. So instead use `mysqli_*` or `PDO`. https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189 – mega6382 Dec 15 '17 at 06:44
  • 3
    Possible duplicate of [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Mathews Sunny Dec 15 '17 at 06:49
  • @While youre perfectly right that mysql_* is deprecated and should not be used for more then two reasons this isnt a duplicate of that question you linked at all. – Fabian S. Dec 15 '17 at 07:10

4 Answers4

3

Why won't you use foreach()(as it take care of indexes itself):-

foreach($name as $key=>$value){
 $x = $value;
 $y = $phone[$key];
 $dt = date('Y-m-d h:i:s');

 $query = mysql_query("insert into grp_mst(grpm_name, grpm_phno, grpm_grpcatm_id,grpm_typ,grpm_crtdon) values ('$x', '$y', '$grmid', 'b', '$dt')");
}

Important notes:-

1.mysql_* library is deprecated in php-5.5 and removed in php-7. Turn towards mysqli_* OR PDO along with latest php 7 version.

2.Always use prepared statements of mysqli_* or PDO library to prevent Your code from SQL INJECTION.

REFERENCE:-

php mysqli prepare

php PDO prepare

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1

You have 2 items in your array, index 2 is the third one (and does not exists given your input)

$countt = count($name);
for ($i = 0; $i <= $countt; $i++)
/* => */
for ($i = 0; $i < $countt; $i++)
Oulalahakabu
  • 504
  • 3
  • 6
0
$name = explode(',', "$grpm_name");
$phone = explode(',', "$grpm_phno");

rewrite it as

$name = explode(',', $grpm_name);
$phone = explode(',', $grpm_phno);
mega6382
  • 9,211
  • 17
  • 48
  • 69
Satish51
  • 119
  • 6
0

either use foreach loop or modify your for loop like this:

for ($i = 0; $i < $countt; $i++)
mehrdadep
  • 972
  • 1
  • 15
  • 37