0

I'm trying to store the 2 array values in single row. However, I got unexpected output which return extra row in the datatable.

My php:

 $checkbox1=$_POST['name'];
        $checkbox2=$_POST['id'];        
           foreach ($checkbox1 as $pop )  
                {
                   $addsql = "insert into referral () values ('$pop','{$_POST['id']}')";
                   $addresult = mysql_query($addsql, $link);    
                }

My table (ERROR):

Name   ID
Alex   Array
1      Array
Alice  Array
2      Array

Expected Result:

Name   ID
Alex   1
Alice  2
Lee
  • 181
  • 1
  • 3
  • 22
  • can you try changing {$_POST['id']} to $checkbox2 in your insert statement – Satya Jun 19 '15 at 03:46
  • 4
    See https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php; you’re vulnerable to SQL injection and you should fix that before moving on. – Ry- Jun 19 '15 at 03:47

2 Answers2

1

Hope this work!

$checkbox1=$_POST['name'];
$checkbox2=$_POST['id'];
foreach ($checkbox1 as $k=>$pop){
    $addsql = "insert into referral () values ('$pop','$checkbox2[$k]')";
    $addresult = mysql_query($addsql, $link);    
}
Alghi Fari
  • 440
  • 3
  • 12
  • why my ID records all missing, when i check the checkbox start from the middle row. – Lee Jun 23 '15 at 07:23
  • simple way to make a post with different value but same type on a checkbox you can use **multidimension** like ` Alex 1
    Alice 2` after that try to `print_r($_POST);` .. the result must be array..
    – Alghi Fari Jun 23 '15 at 22:27
  • check this one [http://stackoverflow.com/questions/12493440/posting-multidimensional-array-from-checkbox](http://stackoverflow.com/questions/12493440/posting-multidimensional-array-from-checkbox) – Alghi Fari Jun 23 '15 at 22:30
0

Use mysqli instead of mysql to prevent hijacking...

Use instead $checkbox2 of '{$_POST['id']}'

Modify your code to

$checkbox1=$_POST['name'];
        $checkbox2=$_POST['id'];        
           foreach ($checkbox1 as $pop )  
                {
                   $addsql = "insert into referral () values ('$pop','$checkbox2')";
                   $addresult = $mysqli->query($addsql, $link);    
                }
Bruce
  • 1,647
  • 4
  • 20
  • 22