Background:
I have never used php before (I am learning Java at uni) I am trying to creating a wcms system by working through a great tutorial I found.
I have a php page which collects data from a form and INSERT INTO my mySQL table. It works when I enter numeric data into the sectionid field, but fails if I add text to it. The field is set to type="text" in the html form and the mySQL database is set to VARCHAR. I don't know why it won't save text. The code from the tutorial is only has basic error handling so the only error I get is "Sorry, there was an error saving to the database".
I would be grateful if anyone could let me know how to get the actual error code so I can understand why is isn't writing to my database. Also if anyone has any ideas what I am doing wrong...?
Thanks,
Here is the code:
Code: Select all
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<?php
// Get the PHP file containing the DbConnector class
require_once('../includes/DbConnector.php');
// Check whether a form has been submitted. If so, carry on
if ($HTTP_POST_VARS){
// Create an instance of DbConnector
$connector = new DbConnector();
// ** Start of add content code: **
// Create an SQL query (MySQL version)
$insertQuery = "INSERT INTO pagecontent(pagename,sectiontitle,sectionid,thecontent) VALUES (".
"'".$HTTP_POST_VARS['pagename']."', ".
"'".$HTTP_POST_VARS['sectiontitle']."', ".
$HTTP_POST_VARS['sectionid'].", ".
"'".$HTTP_POST_VARS['thecontent']."')";
// Save the form data into the database
if ($result = $connector->query($insertQuery)){
// It worked, give confirmation
echo '<center><b>Page content added to the database</b></center><br>';
}else{
// It hasn't worked so stop. Better error handling code would be good here!
exit('<center>Sorry, there was an error saving to the database</center>');
}
// ** End of add content code. **
}
?>
<body>
<form name="form1" method="post" action="newArticle.php">
<p> Page Name:
<input name="pagename" type="text" id="pagename">
</p>
<p> Section Title:
<input name="sectiontitle" type="text" id="sectiontitle">
</p>
<p> Section ID:
<input name="sectionid" type="text" id="sectionid">
</p>
<p> Page Content:
<textarea name="thecontent" cols="50" rows="6" id="thecontent"></textarea>
</p>
<p align="center">
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
Thanks