stmt->execute()(数组到字符串转换错误)

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

团队,我正在尝试在 mysql 数据库中插入一个寄存器,但在使用 mysqli 时,我在 execute()

上遇到错误

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

我不确定为什么它会尝试进行这种转换,但无论如何,这是代码。请尽可能告诉我更多数据。

<?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();


    }


}
?>

这里是调用 InsertSiteDb 的地方:

<?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']);


?>

1) 来自 Fred-ii-

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

2) 问题是 $_SESSION['url']$_SESSION['nomesite'] 是数组而不是字符串。添加一个代码块 ABOVE 你的 $query = 调用状态:

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

错误 "Array to string conversion" 是因为这两个 $_SESSION 值之一是数组值,并且 PHP 试图将这些数组值放入 bind_param 方法中定义的字符串中.

更新:

查看更新后的代码视图,稍后在 $_SESSION['url'] 中使用的 $sites 变量显然是一个数组,因此请使用数组中的一个值或内爆数组或使用类似的方法将其转换为字符串。