-1

Following is the code that inserts the form entries into the database. include.php file is for database connectivity

<?php
require "../includes/include.php";
$name=$_POST['name'];
if($name="")
    echo "Name field can't be empty !!!<br>";
$faculty=$_POST['faculty'];
if($faculty="")
    echo "Please select your faculty !!!<br>";
$rating=$_POST['rating'];
if($rating="")
    echo "Please select the respective rating !!!<br>";
$response=$_POST['response'];

$name=mysqli_real_escape_string($con, $name);
$faculty=mysqli_real_escape_string($con, $faculty);
$rating=mysqli_real_escape_string($con, $rating);
$response=mysqli_real_escape_string($con, $response);
$insert_query="insert into feedback(name, faculty, rating, response) values ('$name', '$faculty', '$rating', '$response')";
$query_result=mysqli_query($con, $insert_query);
?>

It shows the error while inserting values

Notice: Undefined index: name in /var/www/html/Mini_Project/php/feedback_script.php on line 12 .

Can someone please help with what exactly has gone wrong?

Shreyasikhar26
  • 455
  • 5
  • 10
  • 1
    `var_dump($_POST)` gives you what? – user3783243 Oct 14 '19 at 03:50
  • 2
    Please make sure your form where you post your value has an input which has `name='name'` – catcon Oct 14 '19 at 03:51
  • 3
    also, your query is open to SQL injection, mysqli_real_escape_string() function is not enough to prevent people with malicious intent, please use prepared statement for better security. – catcon Oct 14 '19 at 03:52

1 Answers1

1

Your assignment of $name, $faculty, $rating, and $response expects your $_POST array to have certain keys in it. If you cannot garantee your $_POST to have all that, it's best to use the null coalesce operator (i.e. double question mark ??) to define a fallback value for assignment:

$name=$_POST['name'] ?? "";
if($name="")
    echo "Name field can't be empty !!!<br>";
$faculty=$_POST['faculty'] ?? "";
if($faculty="")
    echo "Please select your faculty !!!<br>";
$rating=$_POST['rating'] ?? "";
if($rating="")
    echo "Please select the respective rating !!!<br>";
$response=$_POST['response'] ?? "";
Koala Yeung
  • 7,475
  • 3
  • 30
  • 50