简体   繁体   中英

how to rectify Undefined index: firstname in C:\wamp64\www\Form\addtodatabase.php on line 12 error

There is a page called form.html .It directs to the page addtodatabase.php at the end there is submit button .

    <form action="addtodatabase.php" method="post">
    <form class="form-inline">
    <fieldset>
    <legend>Security Department User Registration</legend>
    <div class="form-group">
    <label for="Firstname">First Name</label>
    <input type="text" class="form-control" id="Firstname" name="firstname" placeholder="Text input"><br/>
    </div>

    <div class="form-group">
    <label for="Secondname">Second Name</label>
    <input type="text" class="form-control" id="Secondname" name="secondname" placeholder="Text input"><br/>
    </div>
    </form>

My addtodatabase.php page .

    $connect=mysqli_connect('localhost','root','','form_db');
    if(mysqli_connect_errno($connect))
    {
    echo 'Failed to connect:'.mysqli_connect_error();
    }
    $firstname=""; 
    $secondname="";
    if (isset($_POST)) { 
    $firstname  = isset($_POST['firstname']) ? $_POST['firstname'] : '';
    $secondname = isset($_POST['secondname']) ? $_POST['secondname'] : '';
    echo 'Your first name is ' .$firstname. '<br>'; 
    echo 'Your second name is ' .$secondname. '<br>'; 
    }

There are three errors

it doesn't get to the page addtodatabase.php . http://localhost:8080/form/form.html ?
Notice: Undefined index: firstname in C:\\wamp64\\www\\Form\\addtodatabase.php on line 12. nothing is being added to database. only id is increment

Thanks in advance.

Note- Check isset before get data.

Try this

 $connect=mysqli_connect('localhost','root','','form_db');

    if(mysqli_connect_errno($connect))
    {
    echo 'Failed to connect:'.mysqli_connect_error();
    }

    if (isset($_POST)) { 
    $firstname=$_POST['firstname'];
    $secondname=$_POST['secondname'];


    echo 'Your first name is ' .$firstname. '<br>'; 
    echo 'Your second name is ' .$secondname. '<br>'; 
    }

is addtodatabase.php in the same folder as form.html?

if html is in c:\\wamp64\\www and addtodatabase.php is in C:\\wamp64\\www\\Form\\ then change the html to;

<form action="Form/addtodatabase.php" method="post">
<form class="form-inline">
<fieldset>
<legend>Security Department User Registration</legend>
<div class="form-group">
<label for="Firstname">First Name</label>
<input type="text" class="form-control" id="Firstname" name="firstname" placeholder="Text input"><br/>
</div>

<div class="form-group">
<label for="Secondname">Second Name</label>
<input type="text" class="form-control" id="Secondname" name="secondname" placeholder="Text input"><br/>
</div>
</form>
<input type="submit" value="submit">
</form>

Before you access the post value, you had better check if it exists. So use

$firstname  = isset($_POST['firstname']) ? $_POST['firstname'] : '';
$secondname = isset($_POST['secondname']) ? $_POST['secondname'] : '';

to replace

$firstname=$_POST['firstname'];
$secondname=$_POST['secondname'];

edit:

$connect=mysqli_connect('localhost','root','','form_db');

if(mysqli_connect_errno($connect))
{
echo 'Failed to connect:'.mysqli_connect_error();
}


$firstname  = isset($_POST['firstname']) ? $_POST['firstname'] : '';
$secondname = isset($_POST['secondname']) ? $_POST['secondname'] : '';

echo 'Your first name is ' .$firstname. '<br>'; 
echo 'Your second name is ' .$secondname. '<br>'; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM