Click here to Skip to main content
15,909,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add data to the database only two records entered correctly (first name, member type), and the ID always be 0 
here's the error:

Notice: Undefined index: memberid in C:\xampp\htdocs\...
Notice: Undefined index: lastname in C:\xampp\htdocs\..
Notice: Undefined index: phonenumber in C:\xampp\htdocs\...
Notice: Undefined index: email in C:\xampp\htdocs\...
Notice: Undefined index: username in C:\xampp\htdocs\...
Notice: Undefined index: password in C:\xampp\htdocs\...
Notice: Undefined index: department in C:\xampp\htdocs\...
Records added successfully.


What I have tried:

here's the code
For HTML
HTML
<pre><form action="test.php" method="post">

			<div class="form-group">
				<label for="element-1" class="control-label">First Name</label>
				<input type="text" id="element-1" placeholder="Enter Your First Name Here" class="form-control" , name="firstname">
			</div>
            
             <div class="form-group">
				<label for="element-2" class="control-label">Last Name</label>
				<input type="text" id="element-2" placeholder="Enter Your Last Name Here" class="form-control",name="lastname">
			</div>
            
			<div class="form-group">
				<label for="element-2" class="control-label">Member ID</label>
				<input type="text" id="element-2" placeholder="Enter Your ID Here" class="form-control" ,name="member_id">
			</div>
            
             <div class="form-group">
				<label for="element-2" class="control-label">User Name</label>
				<input type="text" id="element-2" placeholder="Enter Your User Name Here" class="form-control",name="username">
			</div>
            
			<div class="form-group">
				<label for="element-5" class="control-label"> Password</label>
				<input type="password" id="element-5"  placeholder="Enter Your Password Here" class="form-control",name="password">
			</div>
            <div class="form-group">
				<label for="element-5" class="control-label">confirm Password</label>
				<input type="password" id="element-5"  placeholder="confirm Your Password Here" class="form-control",name="confirmpassword">
			</div>
            
            <div class="form-group">
				<label for="element-5" class="control-label"> Email</label>
				<input type="text" id="element-5"  placeholder="Enter Your Email Here" class="form-control",name="email">
			</div>
            
               <div class="tyled-select blue semi-squar">
                   <label for="element-2" class="control-label">Member Type</label>
                    <select name="membertype">
                          <option value="" selected data-default>select member type</option>
                          <option value="professor">Professor</option>
                          <option value="assistent">Assistent</option>
                          <option value="student">Student</option>
                        </select>
            </div>
            
        <div class="form-group">
				<label for="element-2" class="control-label">Phone Number</label>
				<input type="text" id="element-2"  placeholder="Enter Your Phone Number Here" class="form-control",name="phonenumber">
			</div>
            
            <div class="form-group">
				<label for="element-2" class="control-label">department</label>
				<input type="text" id="element-2"  placeholder="Enter Your Department Here" class="form-control",name="department">
			</div>
            
			<button  class="btn btn-primary" type="submit" name="submit">Add</button>

		</form>
and here is my php code
PHP
if(isset($_POST['submit']))
    {   

// Escape user inputs for security
$id = mysqli_real_escape_string($link, $_REQUEST['member_id']);
$first_name = mysqli_real_escape_string($link, $_REQUEST['firstname']);
$last_name = mysqli_real_escape_string($link, $_REQUEST['lastname']);
$phone= mysqli_real_escape_string($link,$_REQUEST['phonenumber']);
$email=  mysqli_real_escape_string($link,$_REQUEST['email']);
$username=  mysqli_real_escape_string($link,$_REQUEST['username']);
$password=  mysqli_real_escape_string($link,$_REQUEST['password']);
$department=  mysqli_real_escape_string($link,$_REQUEST['department']);


if (isset($_POST['membertype']) ){
   $membertype= $_POST['membertype'];
} 

// attempt insert query execution
$sql = "INSERT INTO `member` (`ID`, `first_name`, `last_name`, `member_type`, `password`, `email`, `phoneNo`, `department`, `user_name`)
VALUES ('$id', '$first_name', '$last_name','$membertype','$password','$email','$phone','$department','$username')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
 }
Posted
Updated 18-Apr-18 21:29pm

1 solution

Your HTML is invalid because you have a comma in front of all name attributes:
HTML
<input type="text" id="element-1" placeholder="Enter Your First Name Here" class="form-control" , name="firstname">
It must be
HTML
<input type="text" id="element-1" placeholder="Enter Your First Name Here" class="form-control" name="firstname">
and similar for all other input tags. I guess that this is the source for the fields not being posted.

There is also an invalid quoted string in the line
HTML
<option value="" selected data-default>select member type</option>

Note also that id attributes should be unique. You have reused some. While that is not relevant for the PHP parts it is also not valid HTML.

I suggest to always validate HTML. The W3C provides an online service to do that: The W3C Markup Validation Service[^].
 
Share this answer
 
Comments
Member 12214576 19-Apr-18 15:15pm    
omg it works now thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900