我的代码有什么问题? [关闭]

Please tell me my error.

The errors are:

Notice: Undefined variable: name in C:\wamp\www\3arabschool\admin\section_insert.php on line 10

Source code is here:

<?php
include("../config/config.php");


if(isset($_POST['name'])){ $name = addslashes($_POST['name']); }
if(isset($_POST['order_sec'])){ $order_sec = addslashes($_POST['order_sec']); }
if(isset($_POST['sub'])){ $sub = $_POST['sub']; }


if ($name == ""){   // this is line 10
echo "<div align='center'>please enter name<ahref='javascript:history.back(1)'>back</a></div>";
}else{
$query = @mysqli_query ($con,"INSERT INTO article_sec (name,sub,order_sec)VALUES ('$name','$sub','$order_sec')") or die ("error query");
echo "<p align='center'>done</p>";
echo "<META HTTP-EQUIV='refresh' CONTENT='1' URL='section.php'>";
}
?>

You are accessing a variable that isn't defined unless $_POST['name'] has been defined.

Change the line

if ($name == ""){

to

if (empty($name)) {

PHP will check to see if the variable is set before trying to access it and see if it's empty using this function. See this doc for more info

Probably isset() is returning false when you check the $_POST array for your index. Thus, $name is never being defined. To make sure it's defined, initialize it to something, maybe $name = ''; before the check.

It means that your if statement is failing and hence $name never gets declared/defined. To solve it you can use:

if(isset($_POST['name']))
{ 
   $name = addslashes($_POST['name']); 
} 
else // making sure $name gets declared if the above condition fails
{ 
   $name = ''; 
}    

OR change

if ($name == "")

to

if(! isset($name))