Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php

require 'header.php';

$name = '';
$email = '';
$mobile = '';
$address = '';
$birthday = '';
$id=' ';


if (isset($_GET['id'])) {

    $id = mysqli_real_escape_string($conn, $_GET['id']);
//$sql="SELECT *  FROM `department` WHERE id='$id'";
    $query = mysqli_query($conn, "SELECT *  FROM `employee` WHERE id='$id'");
    $row = mysqli_fetch_assoc($query);
    $name = $row['name'];
    $email = $row['email'];
    $mobile = $row['mobile'];
    $department_id = $row['department_id'];
    $address = $row['address'];
    $birthday = $row['birthday'];
}

if(isset($_POST['submit'])) {
    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
    $department_id = mysqli_real_escape_string($conn, $_POST['department_id']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);
    $address = mysqli_real_escape_string($conn, $_POST['address']);
    $birthday = mysqli_real_escape_string($conn, $_POST['birthday']);
  
    if ($_POST['id']>0) 
    {
        $id=$_POST['id'];
        $sql = "update employee SET name='$name',email='$email', mobile='$mobile', department_id='$department_id', password='$password',  $address='address',$birthday='birthday', where id='$id'";

    } else {

  $sql = "insert into employee(`name`,  `email`, `mobile`, `password`, `department_id`, `address`, `birthday`) values ('$name','$email','$mobile', '$password','$department_id','$address',$birthday')";

    }

    $query = mysqli_query($conn, $sql);

    // header('location:employee.php');
    die();
}

?>

<div class="content pb-0">
    <div class="animated fadeIn">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">


                    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                        <div class="card-header">Employee Type<small> Form</small></div>
                        <input type="hidden" value="<?php  echo $id?>" class="form-control" name="id"></div>
                        <div class="card-body card-block">
                            <div class="form-group">
                                <label for="employee Type" class=" form-control-label">Name</label>
                                <input type="text" value="<?php echo $name ?>" placeholder="Enter Employee Name"
                                    class="form-control" name="name" required>
                            </div>

                            <div class="form-group">
                                <label for="employee Type" class=" form-control-label">Email</label>
                                <input type="email" value="<?php echo $email ?>" placeholder="Enter Employee Email"
                                    class="form-control" name="email" required>
                            </div>
                            <div class="form-group">
                                <label for="employee Type" class=" form-control-label">Mobile</label>
                                <input type="text" value="<?php echo $mobile ?>" placeholder="Enter Employee Mobile"
                                    class="form-control" name="mobile" required>
                            </div>
                            <div class="form-group">
                                <label for="employee Type" class=" form-control-label">Password</label>
                                <input type="password" value="<?php echo $password ?>"
                                    placeholder="Enter Employee Password" class="form-control" name="password" required>
                            </div>

                            <div class="form-group">
                                <label for="employee Type" class=" form-control-label">Department </label>


                                <select name="department_id" class="form-control">


                                    <?php
$sql = "SELECT * FROM `department` ";
$query = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($query)) {
    ?>
                                    <option value="<?php $row['id']?>"> <?php echo $row['department']; ?></option>

                                    <?php
}?>

                                </select>

                            </div>

                            <div class="form-group">
                                <label for="employee Type" class=" form-control-label">Address</label>
                                <input type="text" value="<?php echo $address ?>" placeholder="Enter Employee Address"
                                    class="form-control" name="address" required>
                            </div>
                            <div class="form-group">
                                <label for="employee Type" class=" form-control-label">Birthday</label>
                                <input type="date" value="<?php echo $birthday ?>" placeholder="Enter Employee Birthday"
                                    class="form-control" name="birthday" required>
                            </div>

                            <button type="submit" name="submit" class="btn btn-lg btn-info btn-block">
                                <span>Submit</span>
                            </button>
                    </form>

                </div>
            </div>
        </div>
    </div>
</div>
</div>

</div>
<div class="clearfix"></div>

<?php
require 'footer.php';
?>


What I have tried:

please help me solve these error

Notice: Undefined variable: sql in C:\xampp\htdocs\empoyee_registration\add_employeee.php on line 47

Warning: mysqli_query(): Empty query in C:\xampp\htdocs\empoyee_registration\add_employeee.php on line 47
Posted
Updated 31-Oct-21 0:39am

1 solution

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
See here: PHP: Prepared statements and stored procedures - Manual[^]
 
Share this answer
 

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