-2

Fixed the error with Notice, thank you for this. But I'm still unable to add any record to my table. I'm still getting the Insert Failed message

enter image description here


I'm getting the following error in my php code:

Notice: Undefined index: first_name in /Applications/MAMP/htdocs/www/private/add_actor.php on line 15

Notice: Undefined index: last_name in /Applications/MAMP/htdocs/www/private/add_actor.php on line 15

Notice: Undefined index: birth_year in /Applications/MAMP/htdocs/www/private/add_actor.php on line 15

Insert failed

What I'm doing wrong? I'm trying to make my form submit entered information to my database. Here is my form code:

<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>

This is my php code:

<?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";
    }
  }
?>

Can someone tell me where is my error?

Arina
  • 63
  • 3
  • 13
  • Because `$_POST` doesn't have those indexes. You need to have an attribute `name=` in your HTML elements in order to access `$_POST[]`. – Spencer Wieczorek Oct 14 '18 at 23:12
  • Thanks for the help! I was able to fix the Notice error, but I'm still getting the error with inserting data into the database. It still says Insert Failed. How do I fix this? – Arina Oct 14 '18 at 23:15
  • See [How to get MySQLi error information in different environments](https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments) and learn to use [prepared statements](http://php.net/manual/mysqli.quickstart.prepared-statements.php) – Phil Oct 14 '18 at 23:35
  • @Phil I added that link ^ to the original duplicate, as well as another. New coders such as the OP, probably have no idea what to do/use. I feel it's always best to try and add more possible duplicates, that way it gives them something to go on. The [identifier qualifiers](https://dev.mysql.com/doc/refman/8.0/en/identifier-qualifiers.html) is a big issue here. – Funk Forty Niner Oct 15 '18 at 01:57
  • You're vulnerable to SQL Injection attacks with that code – SpacePhoenix Oct 15 '18 at 02:37

2 Answers2

0

In your HTML tags there is not a name. Therefore in $_POST[...] there is not a variable. try so:

<input type="text" placeholder="first name" name="first_name" />
Ferdinando
  • 964
  • 1
  • 12
  • 23
  • I've fixed that error. Now I'm still unable to add records to my database. – Arina Oct 14 '18 at 23:23
  • @Arina if you want Add record into database table you can read on w3schools site. The link https://www.w3schools.com/php/php_mysql_insert.asp. There are many tutorial... – Ferdinando Oct 15 '18 at 07:06
0

They are right because in your html you did not specify the name attribute for your firstname, lastname, birth year input fields. It shows you the first error encountered. So your html should be the following with name attribute set for all three input fields.

<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>
Gratien Asimbahwe
  • 1,606
  • 4
  • 19
  • 30