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.

hasan darwish
  • 117
  • 1
  • 6
  • 1
    why here is if($_POST['action'] == "add") condition? – Your Common Sense Sep 02 '22 at 09:18
  • because There are two another pages I will add : view all employers and view only specific employers, the condition ```if($_POST['action'] == "add")``` is to say that I am in the **add employers** page – hasan darwish Sep 02 '22 at 09:28
  • 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? – Your Common Sense Sep 02 '22 at 09:30
  • ```$_POST["action"]``` is the select element in the html page, second the ```$_POST['submitEmployer']``` is the submit of the **employers add** page – hasan darwish Sep 02 '22 at 09:33
  • 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. – Your Common Sense Sep 02 '22 at 09:37
  • the first condition : ```if(isset($_POST['submit']))``` is to detect what page I should use. After selecting the _Add New Employer_ page and click on _submit action_ an invisible form will appear. the next condition : ```if(isset($_POST['submitEmployer']))``` is for the page appeared, when _Submit data_ button is clicked, the data will be sent to the company database=>employees table. Good info ? – hasan darwish Sep 02 '22 at 09:42
  • 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 – Your Common Sense Sep 02 '22 at 09:45
  • THIS : **``` – hasan darwish Sep 02 '22 at 09:48
  • 1
    https://stackoverflow.com/a/5126417 – Your Common Sense Sep 02 '22 at 09:53
  • What.... You redirected me to the _Global Variables Problem In PHP_ but why ?? – hasan darwish Sep 02 '22 at 09:56
  • 1
    Because either $_POST['submit'] and $_POST['action'] do not exist on that page and you need to learn that – Your Common Sense Sep 02 '22 at 09:58
  • Read the upper code not the bottom one.. or what you mean ? where is the scope you are saying ?????? – hasan darwish Sep 02 '22 at 10:01

0 Answers0