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.