7

I'm trying to add values to a table in phpmyadmin and I get the error: Unknown column '...' in 'field list'.

Here's my code:

    <?php   

    //preparing the patch to copy the uploaded file
    $target_path = "images/";

    //adding the name of the file, finishing the path
    $target_path = $target_path . basename( $_FILES['image']['name']); 

   //moving the file to the folder
   if(move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
       echo "The file ".  basename( $_FILES['image']['name']). 
       " has been uploaded";
   } else{
      echo "There was an error uploading the file, please try again!";
   }

   //getting input from the form
   $name = $_POST['game'];
   $description = $_POST['beschrijving'];

   //preparing the query to insert the values
   $query = "INSERT INTO tblGames (name, description, image) VALUES ($name,       $description,". $target_path .")";

   //opening connection to db
   $link = mysql_connect('localhost', 'root', '');
if (!$link) {
       die('Could not connect: ' . mysql_error());
}

     //selecting a db
mysql_select_db("BouncingGiani", $link) or die(mysql_error());

//running the query
$result = mysql_query($query) or die (mysql_error());

//closing the connection
mysql_close($link);

?>

so when I enter fds as name in the form on the previous page I get : Unknown column 'fds' in 'field list'. This never happened to me before and I have no idea on what is going on.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Giani Noyez
  • 481
  • 3
  • 8
  • 17
  • Possible duplicate: http://stackoverflow.com/questions/1346209/unknown-column-in-field-list-error-on-mysql-update-query – Karlen Kishmiryan Aug 21 '13 at 21:36
  • Can you show an actual example query that you try to execute? I don;t seen anything that would cause an unknown column issue unless you don't have `name`, `description`, and `image` fields on your table. However you do not seem to be using single quotes around the values you are inserting, which is problematic as well. – Mike Brant Aug 21 '13 at 21:37
  • 1
    By the way, your code is wide open to SQL injections... – PLPeeters Aug 21 '13 at 23:37
  • Does this answer your question? [How to include a PHP variable inside a MySQL statement](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) – Dharman Jun 28 '20 at 21:50

2 Answers2

26

I think this

$query = "INSERT INTO tblGames (name, description, image) VALUES ($name, $description,". $target_path .")";

should be

$query = "INSERT INTO tblGames (name, description, image) VALUES ('$name', '$description', '". $target_path ."')";
Class
  • 3,149
  • 3
  • 22
  • 31
5

It looks like the values should be quoted in your $query statement, i.e. $name, $description, and $target_path.

$query = "INSERT INTO tblGames (name, description, image) 
          VALUES ('$name', '$description', '" . $target_path . "')";
dazed-and-confused
  • 1,293
  • 2
  • 11
  • 19