Marcelo Moraes
Marcelo Moraes

Reputation: 69

stmt->execute() (Array to string conversion error)

Team, I am trying to insert a register in a mysql db, and while using mysqli I am getting an error on execute()

Notice: Array to string conversion in C:\xampp\htdocs\InsereSite.php on line 19 Call Stack execute ( ) ..\InsereSite.php:19

I am not sure why it tries to make this conversion but, at any case, here is the code. Please tell me more datails as you can.

<?php
function InsertSiteDb($site_novo){

    if(!isset($_POST['sites'])) {
        echo " Voce precisa estar logado para acessar essa pagina";
        header('Location:sellsite_login.html');

    }else{

        $mysqli = new mysqli('localhost', 'root', '', 'sellsites');


        $query = "INSERT INTO pages (url,file,public,price) VALUES (?,?,0,0)";
        $stmt = $mysqli->prepare($query);

        $stmt->bind_param('ss',$_SESSION['url'],$_SESSION['nomesite']);
        //$stmt->execute();

        $stmt->close();


    }


}
?>

Here is where the InsertSiteDb is called:

<?php

    session_start();
    $sites = $_POST['sites']; //o que foi digitado no form será postado
    var_dump($sites);
    $sites = explode(";",$sites);


foreach ($sites as $site) {
        $recebesite_html[] = file_get_contents($site); 
}                                                   

$contador = 0;

 foreach ($recebesite_html as $site_novo){
    $pos = strpos($site_novo, "<title>"); 
    $pos_final = strpos($site_novo,"</title>");
    echo $pos.$pos_final;

    $pos += 7; 
    $tamanho = $pos_final -$pos;

    $nome=substr($site_novo,$pos,$tamanho);
    $_SESSION['nomesite'] = $nome;
    $_SESSION['url'] = $sites;
    $retorno = file_put_contents($nome.".html",$site_novo);
    echo $retorno;
    var_dump($site);
 }

include ("InsereSite.php");
$registro_site=InsertSiteDb($_SESSION['nomesite']);


?>

Upvotes: 2

Views: 3320

Answers (1)

Martin
Martin

Reputation: 22760

1) From Fred-ii-

     if(!$stmt->execute()){
trigger_error("there was an error.:".$mysqli->error, E_USER_WARNING);
    }

2) The issue is that either $_SESSION['url'] or $_SESSION['nomesite'] is an array rather than a string. Add a code block ABOVE your $query = call to state:

if (is_array($_SESSION['url']) || is_array($_SESSION['nomesite'])){
print "Data is an array:<br>";
var_dump($_SESSION);
} 

The error "Array to string conversion" is because one of these two $_SESSION values are array values and the PHP is trying to fit these array values into strings as defined in the bind_param method.

UPDATE:

Looking at your updated code view, the $sites variable which is later used in $_SESSION['url'] is clearly an array, so look to use one value from the array or imploding the array or using a similar method to transform it into a string.

Upvotes: 6

Related Questions