PHP throws errors when submitting the form or registering the user. The array key is already set in the HTML form for gender and hobbies; then it also throws errors when submitting the form. Also, I don’t know how to solve the type error implode function is throwing. The following list of errors is coming.
-
Warning: Undefined array key “gender” in C:xampphtdocsmainoldProjectsdemoTestinsertData.php on line 35
-
Warning: Undefined array key “hobbies” in C:xampphtdocsmainoldProjectsdemoTestinsertData.php on line 36
-
Fatal error: Uncaught TypeError: implode(): Argument #1 ($array) must be of type array, string given in C:xampphtdocsmainoldProjectsdemoTestinsertData.php:
index.php HTML code
<?php 'config.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo Test</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<body>
<section class="vh-100 gradient-custom">
<div class="container py-3 h-100">
<div class="row justify-content-center align-items-center h-100">
<div class="col-12 col-lg-9 col-xl-7">
<div class="card shadow-2-strong card-registration" style="border-radius: 15px;">
<div class="card-body p-4 p-md-5">
<h3 class="mb-2 pb-md-0 mb-md-5">Registration Form</h3>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6 mb-4">
<div class="form-outline">
<input type="file" name="userImage" class="form-control" />
</div>
</div>
<div class="col-md-6 mb-4">
<img src="" alt="" height="100px" width="100px" class="float-end" />
</div>
</div>
<div class="row">
<div class="col-md-6 mb-2">
<div class="form-outline">
<input type="text" name="userName" class="form-control form-control-lg" />
<label class="form-label">User Name</label>
</div>
</div>
<div class="col-md-6 mb-2">
<div class="form-outline">
<input type="email" name="email" class="form-control form-control-lg" />
<label class="form-label">Email</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-2">
<div class="form-outline">
<input type="text" name="password" class="form-control form-control-lg" />
<label class="form-label">Password</label>
</div>
</div>
<div class="col-md-6 mb-2">
<div class="form-outline">
<input type="tel" name="phoneNumber" class="form-control form-control-lg" />
<label class="form-label">Phone Number</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-2">
<h6 class="mb-2 pb-1">Gender: </h6>
<div class="form-check form-check-inline">
<input type="radio" name="gender" class="form-check-input" value="Male" />
<label class="form-check-label">Male</label>
</div>
<div class="form-check form-check-inline">
<input type="radio" name="gender" class="form-check-input" value="Female" />
<label class="form-check-label">Female</label>
</div>
</div>
<div class="col-md-6 mb-2">
<h6 class="mb-2 pb-1">Hobbies: </h6>
<div class="form-check form-check-inline ">
<input class="form-check-input" type="checkbox" name="hobbies[]" value="Reading" />
<label class="form-check-label">Reading</label>
</div>
<div class="form-check form-check-inline ">
<input class="form-check-input" type="checkbox" name="hobbies[]" value="Writing" />
<label class="form-check-label">Writing</label>
</div>
<div class="form-check form-check-inline ">
<input class="form-check-input" type="checkbox" name="hobbies[]" value="Traveling" />
<label class="form-check-label">Traveling</label>
</div>
</div>
</div>
<div class="mt-2 pt-2 d-grid gap-2 col-6 mx-auto">
<button type="submit" name="submit" class="btn btn-primary btn-lg">Register</button>
</div>
<?php include 'insertData.php'; ?>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3"
crossorigin="anonymous"></script>
</body>
</html>
insert.php PHP code
<?php
include('config.php');
#file upload
if (isset($_FILES['userImage'])) {
$errors = array();
#set file attributes
$fileName = $_FILES['userImage']['name'];
$fileTmpName = $_FILES['userImage']['tmp_name'];
$fileSize = $_FILES['userImage']['size'];
$fileType = $_FILES['userImage']['type'];
$tmpExt = explode('.', $fileName);
$fileExt = strtolower(end($tmpExt));
$extensions = array("jpg", "png", "jpeg");
if (in_array($fileExt, $extensions) === FALSE) {
$errors[] = "Only JPG, PNG files are allowed !";
} elseif ($fileSize > 2097152) {
$errors[] = "File size must be 2MB or lower !";
} elseif (empty($errors) == TRUE) {
move_uploaded_file($fileTmpName, "upload/" . $fileName);
} else {
print_r($errors);
die();
}
}
if (isset($_POST['submit'])) {
$userName = $_POST['userName'];
$email = $_POST['email'];
$password = $_POST['password'];
$phoneNumber = $_POST['phoneNumber'];
> $gender = $_POST['gender'];
> $tmpHobbies = $_POST['hobbies'];
> $hobbies = implode(',', $tmpHobbies);
if (empty($userName) || empty($email) || empty($password) || empty($phoneNumber) || empty($gender) || empty($hobbies)) {
echo "<div class='col-sm-4 text-bg-danger p-2 mt-2'>All the fields are required !</div>";
} else {
$insertQuery = "INSERT INTO `users`(`username`, `email`, `password`, `gender`, `hobbies`, `phoneNumber`, `userPhoto`) VALUES ('$userName','$email','$password','$gender','$hobbies','$phoneNumber','$fileName')";
if ($conn->query($insertQuery) === TRUE) {
echo "<div class='col-sm-4 text-bg-success p-2 mt-2'>Registration Success.</div>";
} else {
echo "<div class='col-sm-4 text-bg-danger p-2 mt-2'>Registration failed.</div>";
}
}
}
?>