0

Is there any reason why this is not working?

<form method="post" action="CorseProject.php">
    <table width="400" border="0" cellspacing="1" cellpadding="2">
        <tr>
            <td width="100">EmployeeID</td>
            <td><input name="employeeid" type="text" id="employeeid" required oninvalid="this.setCustomValidity('You must enter an employee id')" onchange="this.setCustomValidity('')"></td>
        </tr>
        <tr>
            <td width="100">ManagementID</td>
            <td><input name="managementid" type="text" id="managementid" required oninvalid="this.setCustomValidity('You must enter a management id')" onchange="this.setCustomValidity('')"></td>
        </tr>
        <tr>
            <td width="100">First Name</td>
            <td><input name="first_name" type="text" id="first_name" required oninvalid="this.setCustomValidity('You must enter a first name')" onchange="this.setCustomValidity('')"></td>
        </tr>
        <tr>
            <td width="100">Last Name</td>
            <td><input name="last_name" type="text" id="last_name" required oninvalid="this.setCustomValidity('You must enter a last name')" onchange="this.setCustomValidity('')"></td>
        </tr>
        <tr>
            <td width="100">Age</td>
            <td><input name="age" type="text" id="age" required oninvalid="this.setCustomValidity('You must enter an age)" onchange="this.setCustomValidity('')"></td>
        </tr>
        <tr>
            <td width="100">Employment Period in Months</td>
            <td><input name="employment_period" type="text" id="employment_period" required oninvalid="this.setCustomValidity('You must enter an employment period')" onchange="this.setCustomValidity('')"></td>
        </tr>
        <tr>
            <td width="100"> </td>
            <td><input name="find" type="submit" id="add" value="Insert New Record"></td>
        </tr>
    </table>
</form>

My config and opendb file below are working fine, so they are not causing this error.

<?php 
    include 'mod2_config.php';
    include 'mod2_opendb.php';
    $employeeid = (isset($_POST['employeeid'])? $_POST['employeeid']:'');
    $managementid = (isset($_POST['managementid']) ? $_POST['managementid']: '');
    $fname= (isset($_POST['first_name'])    ? $_POST['first_name']   : '');
    $lname= (isset($_POST['last_name'])    ? $_POST['last_name']   : '');
    $age= (isset($_POST['age'])    ? $_POST['age']   : '');
    $employment_period= (isset($_POST['employment_period'])    ? $_POST['employment_period']   : ''); 
    $sql= " INSERT INTO employees (employeeid, managementid, first_name,last_name, age, employement_period)
    VALUES ('$employeeid','$managementid',$fname','$lname','$age', '$employment_period')";
    $result = mysqli_query($con, $sql);
    $sql= " SELECT employeeid, managementid, first_name, last_name, age,employment_period from employees WHERE employeeid = $employeeid LIMIT 1";
    $result = mysqli_query($con, $sql);
    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            echo "<b>Record successfully inserted:</b><br>";
            echo "<b>EmployeeID: " . $row["employeeid"]. "</b><br>";
            echo "<b>ManagementID: " . $row["managementid"]. "</b><br>";
            echo "<b>Name: " . $row["first_name"]. " " . $row["last_name"]. "</b><br>";
            echo "<b>Age: " . $row["age"]. "</b><br>";
            echo "<b>Employment Period In Months: " . $row["employment_period"]. "</b><br>";
        }
    }
    else {
        echo "Sorry there are no matches! Please check your entry and try again.";
    }

    //The else statement above keeps running instead of the form inserting information into the database and showing the results.
    mysqli_close($con);
?>

I have tried this insert script for several hours and I still can't get it to insert into the database.

2
  • You need me to print the value of my variables? This is a very basic php script pulling information from a database with an html form. If you do not know the error it is okay. Commented Apr 14, 2018 at 3:56
  • No, that is not the error. Commented Apr 14, 2018 at 4:20

2 Answers 2

1

Try this.

<?php 
include 'mod2_config.php';
include 'mod2_opendb.php';


$employeeid= (isset($_POST['employeeid'])    ? $_POST['employeeid']   : '');
$managementid= (isset($_POST['managementid'])    ? $_POST['managementid']   : '');
$fname= (isset($_POST['first_name'])    ? $_POST['first_name']   : '');
$lname= (isset($_POST['last_name'])    ? $_POST['last_name']   : '');
$age= (isset($_POST['age'])    ? $_POST['age']   : '');
$employment_period= (isset($_POST['employment_period'])    ? $_POST['employment_period']   : ''); 


$sql_insert = " INSERT INTO employees (employeeid, managementid, first_name, last_name, age, employement_period)
                    VALUES ('$employeeid','$managementid','$fname','$lname','$age', '$employment_period')";
$result_insert = mysqli_query($con, $sql_insert);

$sql_fetch = " SELECT * from employees WHERE employeeid = $employeeid LIMIT 1";

$result_fetch = mysqli_query($con, $sql_fetch);

if (mysqli_num_rows($result_fetch) > 0) {
  // output data of each row
  while($row = mysqli_fetch_assoc($result_fetch)) {
    echo "<b>Record successfully inserted:</b><br>";
    echo "<b>EmployeeID: " . $row["employeeid"]. "</b><br>";
    echo "<b>ManagementID: " . $row["managementid"]. "</b><br>";
    echo "<b>Name: " . $row["first_name"]. " " . $row["last_name"]. "</b><br>";
    echo "<b>Age: " . $row["age"]. "</b><br>";
    echo "<b>Employment Period In Months: " . $row["employment_period"]. "</b><br>";
  }
}   
    else {
    echo "Sorry there are no matches! Please check your entry and try again.";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Why try? Any explanation? Always add an explanation about what changes you made so that code will work properly.
You need to add explanation now in your current answer itself.
I'm sure that explanation no need, because @CodingTime have no idea about code. He got that sample somewhere, he have no idea how it works, and he need decision here and now and for free /thread.
0

The error is in your insert query at $fname

Replace $fname' to '$fname'

$sql= " INSERT INTO employees (managementid, first_name,last_name, age, employement_period)
VALUES ('$managementid','$fname','$lname','$age', '$employment_period')";

Do this

  1. Make an auto-incrementing primary key. it may be employee_id.
  2. While inserting don't add that primary key in the query, because it is auto-increment.
  3. Then write the select query like this.

    $sql= " SELECT employeeid, managementid, first_name, last_name, age,employment_period from employees ORDER BY employeeid DESC LIMIT 1";
    

It will gives you the result you want....

9 Comments

Thanks that helped, but now it tells me that I inserted successfully but the information pulled is that of the person I am trying to insert over, and it does not allow me to make an insert into a number that is not in the list of employee id's. Any thoughts?
Basically, it does not actually insert the information I am trying to insert, it just says it did. and pulls up information of another person.
This is not possible... Check your database... Wait I'm giving you some suggestion..
For example, I have 20 people in my database. When I enter a number 21, I get the else message that tells me that person is not in the database, but I want to insert a new person with the number 21. It's a very weird error.
Now it just shows me the one person that is below the number I put, it still does not insert. By the way, the primary key is employeeid and the foreign key is managementid, if that helps.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.