0

I want to insert an array ($array) into a Mysql table (notification), I tried this but nothing is entering. How do I solve this?

$select = "SELECT * FROM addclique WHERE adder_id = :session_id";
$param1 = array ('session_id' => $_SESSION['id']);
$cliques = $db->query($select, $param1);

foreach($cliques as $key)
{
        $array[] = $key['clique_id'];        
}
$array[] = $key['clique_id']; 
$notijfy = new Notification();
$notijfy->addCircle($array);

function addCircle($id_involve){
    $escaped_values = array_map('mysql_real_escape_string', array_values($array));

    $sql2 = "INSERT INTO notification(id_involve) VALUES (:id_involve)";
    $param2 = array ('id_involve' => implode(", ", $escaped_values));
    $result2 = $this->db->query ($sql2, $param2);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
uchejava
  • 1
  • 1
  • 5

1 Answers1

0

There are several ways. I would just do something simple like this:

foreach($cliques as $key){

        $array[] = $key['clique_id'];   

}

$notijfy = new Notification();    
$notijfy->addCircle($array);    
function addCircle($array){
$insert_string = '';

$count = 0;
foreach ($array as $k => $v){

$count++;


${$k} = mysqli_real_escape_string($this->db, $v);

$insert_string .= "(" . ${$k} . ")";

if ($count < sizeof($array)){

$insert_string .= ",";

}

      }
$sql2 = "INSERT INTO notification(id_involve) VALUES $insert_string;";

$result2= $this->db->query ($sql2, $param2);

}

Your major error is that you are trying to pass ($passing an array when calling the function, but in the function itself your argument is listed as $id_involve, when you obviously need an $array variable that you are using in the function itself. I also can't understand why are you trying to add an element to the $array variable outside of foreach loop in the beginning. Doesn't make any sense.

Instead of $this->db just use your connection variable.

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
AR.
  • 1,935
  • 1
  • 11
  • 14