英文:
Warning: Undefined array key "id" in D:\XAMPP\htdocs\iceico\CURD\index.php on line 20
问题
警告:在 D:\XAMPP\htdocs\iceico\CURD\index.php 的第 20 行出现未定义的数组键 "id"。
我正在尝试在 PHP 中执行 CURD 操作,但遇到了这个错误。
在前端,我使用了 Bootstrap。
在后端,我使用了 PHP。
数据库使用了 MySQL。
PHP 代码
<?php
$server = "localhost";
$user = "root";
$password = "";
$db = "student-registration";
$conn = mysqli_connect($server, $user, $password, $db);
if (!$conn) {
die("服务器未连接" . mysqli_connect_error());
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$class = $_POST['class'];
$id = $_FILES['id'];
// print_r($_FILES['id']);
// $id_loc = $_FILES['id']['temp_name'];
// $id_name = $_FILES['id']['name'];
// $id_des = 'uploadId/' . $id_name;
// move_uploaded_file($id_loc, 'uploadId/' . $id_name);
// $sql = "INSERT INTO 'student'('Name','Email','Contact','Class','id') VALUES('$name','$email','$number','$class','$id_des')";
// $result = mysqli_query($conn, $sql);
}
?>
HTML 代码
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<title>Document</title>
</head>
<body>
<nav class="navbar bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">
学生注册
</a>
</div>
</nav>
<form action="/iceico/CURD/index.php" method="post">
<div class="container my-4 w-50">
<div class="mb-3">
<label for="name" class="form-label">姓名</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Prathamesh Manoj Rathod">
</div>
<div class="mb-3">
<label for="email" class="form-label">电子邮件</label>
<input type="email" class="form-control" id="email" name="email" placeholder="mailtoprathamesh484@gmail.com">
</div>
<div class="mb-3">
<label for="number" class="form-label">联系方式</label>
<input type="number" class="form-control" id="number" name="number" placeholder="9130*******">
</div>
<div class="mb-3">
<label for="class" class="form-label">班级</label>
<input type="text" class="form-control" id="class" name="class" placeholder="PHP Developer (ICEICO)">
</div>
<div class="mb-3">
<label for="id" class="form-label">ID</label>
<input type="file" class="form-control" id="id" name="id">
</div>
<button type="submit" class="btn btn-primary">提交</button>
</div>
</form>
</body>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"
integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</html>
我尝试更改名称,但仍然遇到问题。
英文:
Warning: Undefined array key "id" in D:\XAMPP\htdocs\iceico\CURD\index.php on line 20
I am trying to make a curd operation in php but this error I am faced .
Here i am used bootstrap for front end .
for backend i was used php.
and mysql database.
php code
<?php
$server = "localhost";
$user = "root";
$password = "";
$db = "student-registration";
$conn = mysqli_connect($server, $user, $password, $db);
if (!$conn) {
die("Server not connected" . mysqli_connect_error());
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$class = $_POST['class'];
$id = $_FILES['id'];
// print_r($_FILES['id']);
// $id_loc = $_FILES['id']['temp_name'];
// $id_name = $_FILES['id']['name'];
// $id_des = 'uploadId/' . $id_name;
// move_uploaded_file($id_loc, 'uploadId/' . $id_name);
// $sql = "INSERT INTO 'student'('Name','Email','Contact','Class','id') VALUES('$name','$email','$number','$class','$id_des')";
// $result = mysqli_query($conn, $sql);
}
?>
html code
<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<title>Document</title>
</head>
<body>
<nav class="navbar bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">
Student Registration
</a>
</div>
</nav>
<form action="/iceico/CURD/index.php" method="post" >
<div class="container my-4 w-50">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name"
placeholder=" Prathamesh Manoj Rathod">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email"
placeholder="mailtoprathamesh484@gmail.com">
</div>
<div class="mb-3">
<label for="number" class="form-label">Contact</label>
<input type="number" class="form-control" id="number" name="number" placeholder="9130*******">
</div>
<div class="mb-3">
<label for="class" class="form-label">Class</label>
<input type="text" class="form-control" id="class" name="class"
placeholder="PHP Developer (ICEICO)">
</div>
<div class="mb-3">
<label for="id" class="form-label">id</label>
<input type="file" class="form-control" id="id" name="id">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</body>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"
integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</html>
I was try to change the name but still face issue.
答案1
得分: 1
Add an attribute enctype="multipart/form-data"
to the form tag. This is required for any form that is uploading a file.
<form action="/iceico/CURD/index.php" method="post" enctype="multipart/form-data">
英文:
Add an attribute enctype="multipart/form-data"
to the form tag. This is required for any form that is uploading a file.
<form action="/iceico/CURD/index.php" method="post" enctype="multipart/form-data" >
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论