Parse error: syntax error, unexpected '$query'

I have a MySql DB and I’m trying to insert new data using PHP via HTML input fields


<form method="post" autocomplete="off" id="assing2Porject">
    <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>
    <input style="width:250px;" type="text" id="taskStatus" placeholder="Description" name="taskStatus" required="" value="Not started"><br>

    		$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'>";
    			var projectDesc2table= document.getElementById("projectDesc").value;
    			var projectDur2table= document.getElementById("projectDur").value;
    			var dateTime2table= document.getElementById("myDatetimeField").value;
    			var status2table= document.getElementById("taskStatus").value;



    <select name="projectList" id="projectList">
    <option value="">---Please choose a project---</option>
    		 $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>';
    <button type="submit" id="updatePorject" name="updatePorject" class="buttonSmall">Assign employee</button>

    		        url: "assignProject.php",
    		        success: function(data){

And using PHP, I tried to insert the new data

	$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
	 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){
	$husername = "root";
	$hpassword = "";
	$conn = new mysqli($host,$husername,$hpassword,$hdatabase);

	$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";
      echo  "Error: " . $sql . "<br>" . mysqli_error($db);

	//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)

My suggestion would to be able to insert, read, update and delete in PHP itself instead of trying to do it with javascript (jQuery). That’s what I do though I just use vanilla javascript and FETCH (I think it’s $.get in jQuery) to do it. Anyways, it is then easier to convert to Ajax/Fetch. I would also try to keep as much PHP at the top and the HTML/CSS/Javascript at the bottom of the file. I would recommend switching over to PDO instead of mysqli - here’s a good link that I even still use ->

There’s also another good link, but I can’t remember it right now.

Hey, thanks for the reply.
Actually, i can’t use POD since it an assignment and I have to solve this particular code problem

syntax error means the PHP interpreter can’t understand you’re code. unexpected ‘$query’ means the interpreter has found $query when it expected to find something else. This can help you narrow down your search.

You have this in your code:


$query = "INSERT  INTO `e_p` VALUES ( ...

The line $conn->set_charset("utf8") is missing a semicolon from the end. The interpreter gets here and expects you to continue the statement or finish it with a semicolon. When it instead sees the start of a new statement (the $query = ...) it stops and tells you it’s found something unexpected.

Sponsor our Newsletter | Privacy Policy | Terms of Service