不断出现错误:注意:未定义的索引

时间:2018-10-14 23:09:25

标签: php

通过公告修复了错误,谢谢。但是我仍然无法在表中添加任何记录。我仍然收到“插入失败”消息

enter image description here


我的php代码出现以下错误:

  

注意:未定义的索引:/Applications/MAMP/htdocs/www/private/add_actor.php中第15行的名字

     

注意:未定义的索引:/Applications/MAMP/htdocs/www/private/add_actor.php中第15行上的姓氏

     

通知:未定义的索引:第15行/Applications/MAMP/htdocs/www/private/add_actor.php中的birth_year

     

插入失败

我做错了什么?我正在尝试使表单将输入的信息提交到数据库。这是我的表单代码:

<html>
  <head>
    <title>Add Actors</title>
    <link href="../public/stylesheets/films.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
      <div>
        <form method="POST" action="add_actor.php" class="form">
          <fieldset>
            <legend>Add Actors Form</legend>
            <div class="container">
              <label for='first_name'>First Name</label><br />
              <input type="name" placeholder="first_name" /><br />
            </div>
            <div class="container">
              <label for="last_name">Last Name</label><br />
              <input type="name" placeholder="last_name" /><br />
            </div>
            <div class="container">
              <label for="gender">Gender</label><br />
              <select name="gender">
                <option value="male">Male</option>
                <option value="female">Female</option>
              </select><br />
            </div>
            <div class="container">
              <label for="birth_year">Year Born</label><br />
              <input type="name" placeholder="year"/><br />
            </div>
            <div class='container'>
              <input type='submit' name='Submit' value='Submit' />
            </div>
          </fieldset>
        </form>
      </div>
  </body>
</html>

这是我的php代码:

<?php
  $servername = "localhost"; $username = "webuser"; $password = "secret1234";
  $dbname = "movies_db";

  // Check connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);

  // Check connection
  if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
  }
  else{
    echo "<h1>Connected successfully</h1>";
    $sql = "INSERT INTO actors ('actors_firstname', 'actors_lastname', 'actors_gender', 'actors_birthyear') VALUES
    ('".$_POST['first_name']."','".$_POST['last_name']."','".$_POST['gender']."','".$_POST['birth_year']."')";

    $res2 = mysqli_query($conn,$sql);

    if($res2 === TRUE){
      echo "New record created successfully";
    }
    else{
      echo "Insert failed";
    }
  }
?>

有人可以告诉我我的错误在哪里吗?

2 个答案:

答案 0 :(得分:0)

HTML标记中没有名称。因此,在$ _POST [...]中没有变量。 尝试:

<input type="text" placeholder="first name" name="first_name" />

答案 1 :(得分:0)

它们是正确的,因为在您的html中,您没有为名字,姓氏和出生年份输入字段指定 name 属性。它向您显示遇到的第一个错误。因此,您的html应该为所有三个输入字段都设置了name属性。

<html>
  <head>
    <title>Add Actors</title>
    <link href="../public/stylesheets/films.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
      <div>
        <form method="POST" action="add_actor.php" class="form">
          <fieldset>
            <legend>Add Actors Form</legend>
            <div class="container">
              <label for='first_name'>First Name</label><br />
              <input type="name" placeholder="first_name" name="first_name" /><br />
            </div>
            <div class="container">
              <label for="last_name">Last Name</label><br />
              <input type="name" placeholder="last_name" name="last_name"/><br />
            </div>
            <div class="container">
              <label for="gender">Gender</label><br />
              <select name="gender">
                <option value="male">Male</option>
                <option value="female">Female</option>
              </select><br />
            </div>
            <div class="container">
              <label for="birth_year">Year Born</label><br />
              <input type="name" placeholder="year" name="birth_year"/><br />
            </div>
            <div class='container'>
              <input type='submit' name='Submit' value='Submit' />
            </div>
          </fieldset>
        </form>
      </div>
  </body>
</html>