Warning: Undefined array key "id" in D:\XAMPP\htdocs\iceico\CURD\index.php on line 20

huangapple go评论172阅读模式
英文:

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

&lt;?php
$server = &quot;localhost&quot;;
$user = &quot;root&quot;;
$password = &quot;&quot;;
$db = &quot;student-registration&quot;;
$conn = mysqli_connect($server, $user, $password, $db);
if (!$conn) {
die(&quot;Server not connected&quot; . mysqli_connect_error());
}
if ($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) {
$name = $_POST[&#39;name&#39;];
$email = $_POST[&#39;email&#39;];
$number = $_POST[&#39;number&#39;];
$class = $_POST[&#39;class&#39;];
$id = $_FILES[&#39;id&#39;];
// print_r($_FILES[&#39;id&#39;]);
// $id_loc = $_FILES[&#39;id&#39;][&#39;temp_name&#39;];
// $id_name = $_FILES[&#39;id&#39;][&#39;name&#39;];
// $id_des = &#39;uploadId/&#39; . $id_name;
// move_uploaded_file($id_loc, &#39;uploadId/&#39; . $id_name);
// $sql = &quot;INSERT INTO &#39;student&#39;(&#39;Name&#39;,&#39;Email&#39;,&#39;Contact&#39;,&#39;Class&#39;,&#39;id&#39;) VALUES(&#39;$name&#39;,&#39;$email&#39;,&#39;$number&#39;,&#39;$class&#39;,&#39;$id_des&#39;)&quot;;
// $result = mysqli_query($conn, $sql);
}
?&gt;

html code

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;
integrity=&quot;sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;nav class=&quot;navbar bg-body-tertiary&quot;&gt;
&lt;div class=&quot;container-fluid&quot;&gt;
&lt;a class=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;
Student Registration
&lt;/a&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;form action=&quot;/iceico/CURD/index.php&quot; method=&quot;post&quot; &gt;
&lt;div class=&quot;container my-4 w-50&quot;&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;label for=&quot;name&quot; class=&quot;form-label&quot;&gt;Name&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;name&quot; name=&quot;name&quot;
placeholder=&quot; Prathamesh Manoj Rathod&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;label for=&quot;email&quot; class=&quot;form-label&quot;&gt;Email&lt;/label&gt;
&lt;input type=&quot;email&quot; class=&quot;form-control&quot; id=&quot;email&quot; name=&quot;email&quot;
placeholder=&quot;mailtoprathamesh484@gmail.com&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;label for=&quot;number&quot; class=&quot;form-label&quot;&gt;Contact&lt;/label&gt;
&lt;input type=&quot;number&quot; class=&quot;form-control&quot; id=&quot;number&quot; name=&quot;number&quot; placeholder=&quot;9130*******&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;label for=&quot;class&quot; class=&quot;form-label&quot;&gt;Class&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;class&quot; name=&quot;class&quot;
placeholder=&quot;PHP Developer (ICEICO)&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;mb-3&quot;&gt;
&lt;label for=&quot;id&quot; class=&quot;form-label&quot;&gt;id&lt;/label&gt;
&lt;input type=&quot;file&quot; class=&quot;form-control&quot; id=&quot;id&quot; name=&quot;id&quot;&gt;
&lt;/div&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js&quot;
integrity=&quot;sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;/html&gt;

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=&quot;multipart/form-data&quot; to the form tag. This is required for any form that is uploading a file.

&lt;form action=&quot;/iceico/CURD/index.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; &gt;

huangapple
  • 本文由 发表于 2023年6月29日 17:46:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定