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);
}
1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to format your code before posting it :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.