cancel
Showing results for 
Search instead for 
Did you mean: 

MYSQL/php Error - what have I done wrong?

Amalea
Dabbler
Posts: 24
Registered: ‎13-12-2007

MYSQL/php Error - what have I done wrong?

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!
2 REPLIES 2
Peter_Vaughan
Grafter
Posts: 14,469
Registered: ‎30-07-2007

Re: MYSQL/php Error - what have I done wrong?

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().
Amalea
Dabbler
Posts: 24
Registered: ‎13-12-2007

Re: MYSQL/php Error - what have I done wrong?

I knew it would be something simple, the single quote marks around the data fixed it! Even managed to get the date in correctly.  Grin
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!