Turn on suggestions
Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
Showing results for
MYSQL/php Error - what have I done wrong?
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Help with my Plusnet services
- :
- Everything else
- :
- MYSQL/php Error - what have I done wrong?
MYSQL/php Error - what have I done wrong?
13-12-2007 12:45 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
I'm trying to input some data from a form into my mysql database, but for some reason the query keeps failing. The error I get is:
Unknown column 'news' in 'field list'
And the code I'm trying to execute is below:
include 'db.inc';
$connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect!");
mysql_select_db($databaseName) or die ("Unable to select database!");
$author = $_SESSION["name"];
$news = $_POST["news"];
$state = $_POST["state"];
$department = $_POST["department"];
$date = $_POST["dateevent"];
list($day, $month, $year) = split('[/.-]', $date);
$query = "INSERT INTO News (Content, CreateDate, Author, Department, Status) VALUES ($news, $year-$month-$day, $author, $department, $state)";
mysql_query($query) or die (__FILE__.":".__LINE__."<br>".__FUNCTION__."".mysql_error()."<br>");
echo "New news item added";
I've been fighting with this for hours now and I cannot understand why it doesn't work, or what the error is trying to tell me. If anyone could help it'd be very much appreciated!
Unknown column 'news' in 'field list'
And the code I'm trying to execute is below:
include 'db.inc';
$connection = mysql_connect($hostname, $username, $password) or die ("Unable to connect!");
mysql_select_db($databaseName) or die ("Unable to select database!");
$author = $_SESSION["name"];
$news = $_POST["news"];
$state = $_POST["state"];
$department = $_POST["department"];
$date = $_POST["dateevent"];
list($day, $month, $year) = split('[/.-]', $date);
$query = "INSERT INTO News (Content, CreateDate, Author, Department, Status) VALUES ($news, $year-$month-$day, $author, $department, $state)";
mysql_query($query) or die (__FILE__.":".__LINE__."<br>".__FUNCTION__."".mysql_error()."<br>");
echo "New news item added";
I've been fighting with this for hours now and I cannot understand why it doesn't work, or what the error is trying to tell me. If anyone could help it'd be very much appreciated!
Message 1 of 3
(1,422 Views)
2 REPLIES 2
Re: MYSQL/php Error - what have I done wrong?
13-12-2007 1:21 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Is this a PHP error or MySQL error?
Does the error give a line number and if so what line is it referring to?
Can you post the structure of table News?
Does $_POST['news'] actually exist?
echo "<span>\n";
print_r($_POST);
echo "</span>\n";
will show all posted values.
You can do the same for $_SESSION and see if what you have is what you expect.
Does $news contain any commas?
When writing data to a database you should enclose the data in ' ' (single quotes) which will thn stop any commas causing syntax errors in the MySQL statement.
To prove if this is the case, echo out $query.
Finally you need to alter any text variables to escape special characters like single quotes to stop MySQL interpreting the string itself. look at mysql_escape_string().
Does the error give a line number and if so what line is it referring to?
Can you post the structure of table News?
Does $_POST['news'] actually exist?
echo "<span>\n";
print_r($_POST);
echo "</span>\n";
will show all posted values.
You can do the same for $_SESSION and see if what you have is what you expect.
Does $news contain any commas?
When writing data to a database you should enclose the data in ' ' (single quotes) which will thn stop any commas causing syntax errors in the MySQL statement.
To prove if this is the case, echo out $query.
Finally you need to alter any text variables to escape special characters like single quotes to stop MySQL interpreting the string itself. look at mysql_escape_string().
Message 2 of 3
(324 Views)
Re: MYSQL/php Error - what have I done wrong?
13-12-2007 10:41 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
I knew it would be something simple, the single quote marks around the data fixed it! Even managed to get the date in correctly. 
I'd echo'd all the values out (individually, didn't know you could do them all at once) so knew it wasn't that. And I'll remember to add in the mysql_escape_string(). Very useful bits of info.
Thanks a lot!

I'd echo'd all the values out (individually, didn't know you could do them all at once) so knew it wasn't that. And I'll remember to add in the mysql_escape_string(). Very useful bits of info.
Thanks a lot!
Message 3 of 3
(324 Views)
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Help with my Plusnet services
- :
- Everything else
- :
- MYSQL/php Error - what have I done wrong?