Parse error: syntax error, unexpected '$query'

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)

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 -> https://phpdelusions.net/pdo

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:

$conn->set_charset("utf8")

$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