So I'm writing HTML code for form inside my PHP block like below: Add New Record in PostgreSQL Database .error {color: #FF0000;}
$con= pg_connect("host=$host port= $port dbname=$db user=$user password=$pass")
or die ("Could not connect to server
");
if(empty($_POST['title']))
{
$titleErr= "Title is required";
displayForm();
}
else
{
processForm();
}
}
else
{
displayForm();
}
function processForm(){
$title = $_POST['title'];
$author = $_POST['author'];
$status = $_POST['status'];
$remark = $_POST['remark'];
$story = $_POST['story'];
$art = $_POST['art'];
if($story=='')
{
$story=0;
}
if($art=='')
{
$art=0;
}
$query = "INSERT INTO rating ".
"(title,author,story,art, status, remarks) ".
"VALUES('$title','$author',$story, $art,'$status','$remark')";
$rs= pg_query($con, $query) or die ("Cannot execute query:$query because".pg_last_error()."
");
/************************************************
Save uploaded image in server
************************************************/
echo "Entered data successfully
";
pg_close($con);
}
function displayForm()
{
print<<<END
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" enctype="multipart/form-data">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Title</td>
<td><input name="title" type="text" id="title"><span class="error">* <?php echo $titleErr;?></span></td>
</tr>
<tr>
<td width="100">Author</td>
<td><input name="author" type="text" id="author"></td>
</tr>
<tr>
<td width="100">Story Rating</td>
<td><input name="story" type="text" id="story"></td>
</tr>
<tr>
<td width="100">Art Rating</td>
<td><input name="art" type="text" id="art"></td>
</tr>
<tr>
<td width="100">Remarks</td>
<td><input name="remark" type="text" id="remark"></td>
</tr>
<tr>
<td width="100">Status</td>
<td><input name="status" type="text" id="status"></td>
</tr>
<tr>
<td width="100">Image</td>
<td><input name="file" type="file" id="file"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Entry">
</td>
</tr>
</table>
</form>
END;
}
?>
</body>
</html>
In the function displayForm(), I keep getting this error: Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING), specifically in this line: " enctype="multipart/form-data">
I made sure already there's no leading space at the closing of the Heredoc and the beginning of the Heredoc, and I can't find any solution on the net. What I'm trying to achieve is displaying the error message besides the required fields after the user click submit (display the form once again, now with error message). Please suggest me a solution on this as well in case the structure of my code is wrong.