I am trying to set up a CMS that will allow an administrator to add new pages to a web site. When PHP reads the added text I insert it into a MySQL database using placeholders. PHP correctly gets the added text, but when it inserts them into the database the text is preceded by an amount of white space that corrupts the display of the saved text. Any ideas about what might be causing this?
Hereâs a code extract that sets up the form from which the added text is read :
<div id = "admin_heading">
<label for = "title">Heading</label>
<textarea id = "title" name = "title" cols = "60" >
<?php if(isset($title)):
htmlout($title);
endif;?>
</textarea>
</div>
<div id = "admin_date">
<label for = "lecturedate">Date</label>
<textarea id = "lecturedate" name = "lecturedate" cols = "60" >
<?php if(isset($lecturedate)):
htmlout($lecturedate);
endif;?>
</textarea>
</div>
<div id = "admin_sub_heading">
<label for = "sub_heading">Sub Heading</label>
<textarea id = "sub_heading" name = "sub_heading" rows = "2" cols = "60"></textarea>
</div>
<div id = "admin_content">
<label for = "content">Content</label>
<textarea id = "content" name = "content" rows = "2" cols = "80">
<?php if(isset($text)):
htmlout($text);
endif;?>
</textarea>
</div>
<div id = "form1">
<input type = "submit" name = "action" id = "action" value = "New">
<input type = "submit" name = "action" id = "action" value = "Edit">
<input type = "submit" name = "action" id = "action" value = "Delete">
<input type = "submit" name = "action" onClick = "myFunction" value = "Clear">
</div>
</form>
<script type = "text/javascript">
function myFunction()
{
document.getElementById("myForm").reset();
document.getElementById("tableform").reset();
}
</script>
And hereâs an extract of the code for the database update:
if (isset($_POST[âactionâ]) and $_POST[âactionâ] == âNewâ)
{ htmlout(âNew visitâ);
//include $_SERVER[âDOCUMENT_ROOTâ] . â/includes/db.inc.phpâ;
try{
$sql = 'INSERT INTO lectures SET lecturedate = :lecturedate, title = :title, description = :description';
$s = $pdo->prepare($sql);
$s->bindvalue(':lecturedate', $_POST['lecturedate']);
$s->bindvalue(':title', $_POST['title']);
$s->bindvalue(':description', $_POST['content']);
$s->execute();
}
catch(PDOException $e){
$error = 'Error inserting data into table';
include 'error.html.php';
exit();
}
// Check all set up correctly
$text = $_POST['content'];
$lecturedate = $_POST['lecturedate'];
$title = $_POST['title'];
htmlout($title);
htmlout($lecturedate);
htmlout($text);