I've been working at this and I just can't get this. Can anyone please tell me why this just will not work?

function procNewsStory() {
      global $session, $database;
      $admin = $session->username;
      $date = time();
      $title=$_POST['title'];
      $story=$_POST['story'];
      $story_id = time();
      $q = "INSERT INTO " .TBL_SITE_NEWS. " VALUES ('$admin', '$time', '$title', '$story', '$story_id')";
      $database->query($q);
      header("Location: ".$session->referrer);
      }

This just will not post to my DB and I can't understand why. I'm sure my query() function is right.

Your code seems okay. Is TBL_SITE_NEWS a constant ? Meaning, is it defined somewhere ? Does the values provided in the query match the column count of the table ?

the table name is defined in a file called constants.php. I haven't included this file, but it doesn't matter cause if I just type the table's name in raw it won't work either. the values also match up to the table.

I echoed $q and it's all showing up correctly. I really have no clue what's going on. $database->query(); is:

function query($query){

      return mysql_query($query, $this->connection);

   }

So 2 things. When I try to include the database.php file I get this error.

Fatal error: Cannot redeclare class MySQLDB in /var/www/ripgm/include/database.php on line 14

Also, when I try to do

mysql_query($q,$session->connection);

I get this error

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /var/www/ripgm/admin/adminprocess.php on line 175

I suppose I'm getting this error because my connection to the DB isn't defined in this file? I don't even know, I'm so confused.

I guess $session->connection doesn't hold any value. But anyway, link resource is an optional parameter. Just check if you have a valid connection.
Try printing value of $q inside the function query(). Check if its ok.. Also, try

mysql_query($q) or die(mysql_error());

ie.,

function query($query){
      $result = mysql_query($query) or die(mysql_error());
     return $result;
   }

Apart from that, I can't see anything else.

The weird thing is I have several other $database->query(); 's in this script. And they all work. It's just this one that won't update the table. So I'm sure $session->connection holds a value.The table exists, and the fields all match.

Here's the error die(mysql_error()); gives me.

Unknown column 'myusername' in 'field list'

Okay sorry, I figured it out. I didn't have single quotes around the VALUE($variables). Also, my primary key in my db was set to the admin column, which would mean i couldn't make duplicate entries in that column. I fixed it by setting the primary key to the story id column.

This article has been dead for over six months. Start a new discussion instead.