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