user9151740
user9151740

Reputation: 1

count(): Parameter must be an array or an object that implements Countable, Uninitialized string offset: 0

I have a problem with my code it shows

count(): Parameter must be an array or an object that implements Countable and Uninitialized string offset: 0

$site =  $_POST['site'];
$url  =  $_POST['url'];
$query = '';
for($count = 0; $count<count($site); $count++){
    $site_name = mysqli_real_escape_string($con, $site[$count]);
    $url_name = mysqli_real_escape_string($con, $url[$count]);
    if($site_name != '' && $url_name != ''){
        $query .='INSERT INTO `url` (`site`, `http`) VALUES ("'.$site_name.'", "'.$url_name.'")';
    }
}

The expected out is to insert data into the database

Upvotes: 0

Views: 73

Answers (2)

Dhiraj
Dhiraj

Reputation: 2767

Make sure $_POST['site'] is an array that can implement Countable

$site =  $_POST['site'] ?? []; // php 7
$site =  is_array($_POST['site']) ? $_POST['site'] : [] // php <7

Upvotes: 1

patron
patron

Reputation: 59

your $site variable is not countable, use var_dump($site) to see it's value and type

Upvotes: 0

Related Questions