Argument #1 ($value) must be of type Countable|array, string given in [closed]
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 hours ago.
Improve this questionI'm creating a registration form. It is declared as an array on the beginning, but it says 'string given in'. I also want to know if the code is generally OK. Here is my server.php code
<?php
session_start();
$username = "";
$email= "";
$errors= array();
if (isset($_POST['reg-user'])) {
include('db.php');
$username = mysqli_real_escape_string($linkDB, $_POST['username']);
$email= mysqli_real_escape_string($linkDB, $_POST['email']);
$password=mysqli_real_escape_string($linkDB, $_POST['password']);
$password2=mysqli_real_escape_string($linkDB, $_POST['password2']);
if(empty($username)) {$errors= "Username is required"; }
if(empty($email)) {$errors= "Email is required"; }
if(empty($password_1)) {$errors= "Password is required"; }
if($password != $password2) {
$errors= &qu开发者_开发知识库ot;The two passwords do not match";
}
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($linkDB, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) {
if ($user['username'] === $username) {
$errors= "Username already exists";
}
if ($user['email'] === $email) {
$errors= "email already exists";
}
}
if (count($errors)==0) {
$password = md5($password2);
$query = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($linkDB, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location:dashboard.php');
}
}
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($linkDB, $_POST['username']);
$password = mysqli_real_escape_string($linkDB, $_POST['password']);
if (empty($username)) {
$errors= "Username is required";
}
if (empty($password)) {
$errors="Password is required";
}
if (count($errors) ==0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($linkDB, $query);
if (mysqli_num_rows($results) == 1) {
$logged_in_user = mysqli_fetch_assoc($results);
if ($_SESSION['username'] = 'admin') {
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location: index_for_admin.php');
}else{
$_SESSION['user'] = $logged_in_user;
$_SESSION['success'] = "You are now logged in";
header('location:dashboard.php');
}
}else {
$errors="Wrong username/password combination";
}
}
}
?>
and there's also the same problem in error.php file. Foreach can't be used with string
<?php
if (count($errors) >0 ):?>
<div class= "error">
<?php foreach ($errors as $error) :?>
<p><?php echo $error ?></p>
<?php endforeach ?>
</div>
<?php endif ?>
I tried using count_char. It works on server.php but in error.php it says "foreach() argument must be of type array|object, string given".
精彩评论