Hey,
I have a MySql DB and I’m trying to insert new data using PHP via HTML input fields
HTML and PHP:
<form method="post" autocomplete="off" id="assing2Porject">
<div>
<input style="width:250px;" type="text" id="projectDesc" placeholder="Description" name="projectDesc" required=""><br>
<input style="width:250px;" type="text" id="projectDur" placeholder="Duration" name="projectDur" onkeypress="return onlyNumberKey(event)" required=""><br>
<lable><b>Status</b></lable><br>
<input style="width:250px;" type="text" id="taskStatus" placeholder="Description" name="taskStatus" required="" value="Not started"><br>
<?php
date_default_timezone_set('Asia/Qatar');
$date = new DateTime(); // Date object using current date and time
$dt= $date->format('Y-m-d\TH:i:s');
echo "<br><input type='datetime-local' id='myDatetimeField' name='myDatetimeField' value='$dt'>";
?>
<br>
<script>
var projectDesc2table= document.getElementById("projectDesc").value;
var projectDur2table= document.getElementById("projectDur").value;
var dateTime2table= document.getElementById("myDatetimeField").value;
var status2table= document.getElementById("taskStatus").value;
</script>
<?php
$projectDesc='projectDesc2table';
$projectDur='projectDur2table';
$myDatetimeField='dateTime2table';
$taskStatus='status2table';
?>
<br>
<select name="projectList" id="projectList">
<option value="">---Please choose a project---</option>
<?php
$query3= 'select * from project';
$result3 = $conn->query($query3);
if ($result3->num_rows > 0)
{
while($row3 = $result3->fetch_array())
{
$data = json_decode($row3['p_data']);
echo '<option value="'.$row3['p_id'].'">'.$data->name.'</option>';
}
}
$result3->free();
?>
</select>
</div>
<br>
<button type="submit" id="updatePorject" name="updatePorject" class="buttonSmall">Assign employee</button>
</form>
<script>
$(document).on('submit','#assing2Porject',function(e){
e.preventDefault();
$.ajax({
method:"POST",
url: "assignProject.php",
data:$(this).serialize(),
success: function(data){
$('#msg').html(data);
$('#assing2Porject').find('input').val('')
}});
});
</script>
And using PHP, I tried to insert the new data
<?php
session_start();
include("inc_db.php");
$e_id= $_SESSION['e_id'];
$projectDesc= $_REQUEST['projectDesc'];
$projectDur= $_REQUEST['projectDur'];
$myDatetimeField= $_REQUEST['myDatetimeField'];
$taskStatus= $_REQUEST['taskStatus'];
$projectDesc= legal_input($_POST['projectDesc']);
$projectDur= legal_input($_POST['projectDur']);
$myDatetimeField= legal_input($_POST['myDatetimeField']);
$projectList= legal_input(($_POST['projectList']));
$selected_p_id = $_REQUEST['projectList'];
$taskStatus= legal_input(($_POST['taskStatus']));
/* echo $projectDesc;
echo "<br>";
echo $projectDur;
echo "<Br>";
echo $myDatetimeField;
echo "<Br>";
echo $selected_p_id;
echo "<Br>";
echo $e_id;
echo "<Br>";
echo $taskStatus;
*/
if(!empty($projectDesc) && !empty($projectDur)){
// Sql Query to insert user data into database table
Insert_data($projectDesc,$projectDur,$myDatetimeField,$projectList,$taskStatus);
}else{
echo "All fields are required";
}
function legal_input($value) {
$value = trim($value);
$value = stripslashes($value);
$value = htmlspecialchars($value);
return $value;
}
function insert_data($projectDesc,$projectDur, $myDatetimeField, $projectList){
$host="localhost";
$husername = "root";
$hpassword = "";
$hdatabase="gk2020c";
$conn = new mysqli($host,$husername,$hpassword,$hdatabase);
$conn->set_charset("utf8")
$query = "INSERT INTO `e_p` VALUES ( $e_id,$selected_p_id,"{'tasks':[$projectDesc,$myDatetimeField,$projectDur,$taskStatus]}")";
$result= $conn->query($query) or die('failed!');
if($result ->num_rows == 0)
{
echo "User data was inserted successfully";
}
else{
echo "Error: " . $sql . "<br>" . mysqli_error($db);
}
}
$conn->close();
//header("assignProject: Main.php");
?>
I’m not sure what’s wrong with the query I wrote, even though I’m using variables.
They are defined (I checked with echo to see if I get any values)