This question already has an answer here:
undefined variables(name, email, adm,add) on line 16 ....error is coming.....kindly help me to remove error here is my code
<?php
$con = mysqli_connect('localhost','root','');
if(!$con) {
echo 'not connected to database';
}
if(!mysqli_select_db($con,'student')) {
echo 'database not selected';
}
if (isset($_POST['name'])){$name = $_POST['name'];}
if (isset($_POST['email'])){$email= $_POST['email'];}
if (isset($_POST['address'])){$add = $_POST['address'];}
if (isset($_POST['admission'])){$adm = $_POST['admission'];}
/*line:16*/
$sql = "INSERT INTO student_record (name,email,address,joining_date) VALUES('$name','$email','$add','$adm')";
if ( !mysqli_query($con,$sql)) {
echo 'not inserted';
} else {
echo'inserted';
}
?>
</div>
This is due to you have not initialize your variable before the if conditions . if POST data not found then all 4 variables not set then error come. Plz try this
if($_POST){
$con = mysqli_connect('localhost','root','');
if(!$con)
{
echo 'not connected to database';
}
if(!mysqli_select_db($con,'student'))
{
echo 'database not selected';
}
$error = true;
$errorMessage = '';
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['address']) || empty($_POST['admission'])){
$error = false;
if(empty($_POST['name'])){
$errorMessage = 'Please enter name <br/>';
}
if(empty($_POST['email'])){
$errorMessage .= 'Please enter email <br/>';
}
if(empty($_POST['address'])){
$errorMessage .= 'Please enter address <br/>';
}
if(empty($_POST['admission'])){
$errorMessage .= 'Please enter admission <br/>';
}
}
if($error){
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$add = mysql_real_escape_string($_POST['address']);
$adm = mysql_real_escape_string($_POST['admission']);
/*line:16*/
$sql = "INSERT INTO student_record (name,email,address,joining_date) VALUES('$name','$email','$add','$adm')";
if ( !mysqli_query($con,$sql))
{
echo 'not inserted';
}else{
echo'inserted';
}
}else{
echo $errorMessage;
}
}
I think you are not getting any post data. Just add a if condition before the execution
if($_POST){ /*Do stuff check for the post['name'] run your SQL query */
as the post is not defined your POST['name']
is not set and it does not go the if block and remains not set.