0

the PHP PDO MySQL is not working with the insert ... I am making a company application, the part I am working with, is the employers add and view part. This is my code :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Employers page</title>
    <link rel="stylesheet" href="style.css">
    <script src="app.js" defer></script>
</head>
<body>
    <div class="header"><button class="menu" style="float:right;">menu</button></div>
    <div class="black" id="black"></div>
    <div id="side" style="background:white;"></div><br><br>

    <?php 
        $username = "root" ;
        $password = "" ;
        $database = new PDO("mysql:host=localhost;dbname=company;charset=utf8;", $username, $password);
    ?>

    <center><form method="post" id="form">
        Select Action : <select name="action" id="action">
            <option value="viewAll">See all employers</option>
            <option value="viewSpecific">See defined group or employers</option>
            <option value="add">Add new Employers</option>
        </select><br><br>

        <button type="submit" name="submit">Submit Action</button>
    </form></center>
    <div>
        <center><form method="POST" id="addData" style="margin:3px;padding:1px;display:none;">
            <table id="employersAddTable">
                <tr><td>Name of Employer : </td><td><input type="text" name="nameEmployer"></td></tr>
                <tr><td>Date of Work : </td><td><input type="date" name="dateEmployer"></td></tr>
                <tr><td>Image of Employer : </td><td><input type="file" name="imageEmployer" accept="image/*"></td></tr>
            </table>
            <button type="submit" name="submitEmployer">Submit Data</button>
        </form></center>
    </div>

    <?php 
        if(isset($_POST['submit'])){
            if($_POST['action'] == "add"){
                echo '<script>document.getElementById("addData").style.display = "inline-block"</script>' ;
                if(isset($_POST['submitEmployer'])){
                    $file = "data:".$_FILES["imageEmployer"]["type"].";base64,".file_get_contents($_FILES["imageEmployer"]["tmp_name"]);
                    $insert = $database->prepare("INSERT INTO `employees`(name,date,image) VALUES(:name, :date, :image);");
                    $insert->bindParam("name", $_POST["nameEmployer"]);
                    $insert->bindParam("date", $_POST["dateEmployer"]);
                    $insert->bindParam("image", $file);
                    $insert->execute();
                    if($insert->execute()) {
                        echo "<script>alert('Successfully command executed !')</script>" ;
                    }
                }
            }
        }
    ?>

</body>
</html>

I am new to the back-end developing. The problem is that the command in the $insert variable is not executed, I don't know why, tried var_dump($insert->errorinfo()) but showing that there is no error, checked for the sql command but it is good syntax and it is executed, so the error is in this part :

if(isset($_POST['submitEmployer'])){
                    $file = "data:".$_FILES["imageEmployer"]["type"].";base64,".file_get_contents($_FILES["imageEmployer"]["tmp_name"]);
                    $insert = $database->prepare("INSERT INTO `employees`(name,date,image) VALUES(:name, :date, :image);");
                    $insert->bindParam("name", $_POST["nameEmployer"]);
                    $insert->bindParam("date", $_POST["dateEmployer"]);
                    $insert->bindParam("image", $file);
                    $insert->execute();
                    if($insert->execute()) {
                        echo "<script>alert('Successfully command executed !')</script>" ;
                    }
                }

can anyone help me in this part ?????? thanks.

12
  • 1
    why here is if($_POST['action'] == "add") condition? Commented Sep 2, 2022 at 9:18
  • 1
    1. there is no $_POST['action'] on this page. 2. There is already if(isset($_POST['submitEmployer'])). Why do you need that $_POST['action'] at all? Commented Sep 2, 2022 at 9:30
  • 1
    There are two statements in my last comment. It's not questions, you don't have to answer them. It's the information for you to learn from. There is only one question there but you didn't answer it. Commented Sep 2, 2022 at 9:37
  • 1
    No. Bad logic. You already have if(isset($_POST['submitEmployer'])). You don't need any other condition. Not to mention that either $_POST['submit'] and $_POST['action'] do not exist on that page at all Commented Sep 2, 2022 at 9:45
  • 1
    stackoverflow.com/a/5126417 Commented Sep 2, 2022 at 9:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.