0

Im trying to add multiple values from array in MySQL with one sql query but Im getting this error:

Notice: Array to string conversion in /var/www/index/test2.php on line 103 Warning: implode(): Argument must be an array in /var/www/index/test2.php on line 103

Here is code part where its wrong:

    $sqlas = array(); 
    foreach( $rezai as $rezas ) 
    { 
          if (!empty($rezas)) {
                    $rezas = str_replace('http://', '', strip_tags($rezas));
          }
          else
          {
                    $rezas = 'empty_rezas';
          }

          $failoID = explode('/', $rezas);

          if (!isset($failoID[2]) || empty($failoID[2])) 
          {
                    $failoID[2] = 'neraID';
          }
          if (isset($rezas) && !empty($rezas)) 
           {
                    $urlr = str_replace('www.mysite.com', '', $rezas);
           }
          $sqlas[] = '("'.mysqli_real_escape_string($conn, $failoID[2]).'", "'.mysqli_real_escape_string($conn, $urlr).'", "'.$uzklausaClean.'")';
    }
   $conn->query('INSERT INTO four_failai2 (id, url, uzklausa) VALUES '.implode(','. $sqlas).'');
   $conn->close();

The line 103 is this one:

$conn->query('INSERT INTO four_failai2 (id, url, uzklausa) VALUES '.implode(','. $sqlas).'');

It says Argument must be an array, but I am defining $sqlas as empty array before foreach loop and then I am adding values inside loop. Any ideas?

1
  • 2
    Other than that, you are open to SQL Injections and should really use Prepared Statements instead of concatenating your queries. mysqli_real_escape_string() isn't as safe as one would hope. Commented Sep 20, 2016 at 12:52

1 Answer 1

1

You have a dot instead of a comma in your implode function.

Change implode(','. $sqlas) to implode(',', $sqlas). As it is now, you're trying to concatenate the string containing the comma with an array (e.i. array to string conversion).

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

Comments

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.